Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. when to use casting in c++ instead class global pointer?
Forum Updated to NodeBB v4.3 + New Features

when to use casting in c++ instead class global pointer?

Scheduled Pinned Locked Moved Solved C++ Gurus
6 Posts 4 Posters 728 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on 29 Nov 2021, 13:46 last edited by Qt embedded developer
    #1

    I have seen that we can access other class by taking global pointer in other class. And i also have seen that we can use that class by casting . so i want to know what i should prefer and when ?

    for example for casting

    this->pTransactionData = dynamic_cast<app::transaction::CustomTxnDataContainer*>(pTransactionData);
    

    and example for class global pointer

    TransactionDataContainer *pTransactionData = new TransactionDataContainer ;
    
    J J 2 Replies Last reply 29 Nov 2021, 14:15
    0
    • Q Qt embedded developer
      29 Nov 2021, 13:46

      I have seen that we can access other class by taking global pointer in other class. And i also have seen that we can use that class by casting . so i want to know what i should prefer and when ?

      for example for casting

      this->pTransactionData = dynamic_cast<app::transaction::CustomTxnDataContainer*>(pTransactionData);
      

      and example for class global pointer

      TransactionDataContainer *pTransactionData = new TransactionDataContainer ;
      
      J Offline
      J Offline
      JonB
      wrote on 29 Nov 2021, 14:15 last edited by JonB
      #3

      @Qt-embedded-developer
      These are two unrelated concepts, and are not interchangeable.

      dynamic_cast<Something *> means you already have a pointer to the object, created elsewhere, and you just want to test whether it points to a Something here.

      new Something means you are creating the object here, and receiving a pointer to it.

      1 Reply Last reply
      1
      • Q Qt embedded developer
        29 Nov 2021, 13:46

        I have seen that we can access other class by taking global pointer in other class. And i also have seen that we can use that class by casting . so i want to know what i should prefer and when ?

        for example for casting

        this->pTransactionData = dynamic_cast<app::transaction::CustomTxnDataContainer*>(pTransactionData);
        

        and example for class global pointer

        TransactionDataContainer *pTransactionData = new TransactionDataContainer ;
        
        J Online
        J Online
        jsulm
        Lifetime Qt Champion
        wrote on 29 Nov 2021, 14:15 last edited by
        #2

        @Qt-embedded-developer said in when to use casting in c++ instead class global pointer?:

        and example for class global pointer
        TransactionDataContainer *pTransactionData = new TransactionDataContainer ;

        Where is this code located?
        If inside a class (this->pTransactionData suggests that), then it is not a global pointer.

        "so i want to know what i should prefer and when ?" - I am not really sure I understand your question. Casting is only needed if the type of the pointer is not exact same as the type of the pointer you're assigning. And you can have a pointer to an instance of a class if you need to access it. So, all depends on your needs.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • Q Qt embedded developer
          29 Nov 2021, 13:46

          I have seen that we can access other class by taking global pointer in other class. And i also have seen that we can use that class by casting . so i want to know what i should prefer and when ?

          for example for casting

          this->pTransactionData = dynamic_cast<app::transaction::CustomTxnDataContainer*>(pTransactionData);
          

          and example for class global pointer

          TransactionDataContainer *pTransactionData = new TransactionDataContainer ;
          
          J Offline
          J Offline
          JonB
          wrote on 29 Nov 2021, 14:15 last edited by JonB
          #3

          @Qt-embedded-developer
          These are two unrelated concepts, and are not interchangeable.

          dynamic_cast<Something *> means you already have a pointer to the object, created elsewhere, and you just want to test whether it points to a Something here.

          new Something means you are creating the object here, and receiving a pointer to it.

          1 Reply Last reply
          1
          • Q Offline
            Q Offline
            Qt embedded developer
            wrote on 29 Nov 2021, 15:01 last edited by
            #4

            @jsulm @JonB is it right for any type of casting ?

            J 1 Reply Last reply 29 Nov 2021, 15:06
            0
            • Q Qt embedded developer
              29 Nov 2021, 15:01

              @jsulm @JonB is it right for any type of casting ?

              J Online
              J Online
              jsulm
              Lifetime Qt Champion
              wrote on 29 Nov 2021, 15:06 last edited by
              #5

              @Qt-embedded-developer said in when to use casting in c++ instead class global pointer?:

              is it right for any type of casting ?

              What do you mean?
              https://en.cppreference.com/w/cpp/language/static_cast
              https://en.cppreference.com/w/cpp/language/dynamic_cast

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • K Offline
                K Offline
                Kent-Dorfman
                wrote on 4 Dec 2021, 00:14 last edited by
                #6

                global pointers to members of another glass are generally a bad idea because accessing the class without going thru the MOC can have undesired behaviour. inter-class comms should be via signal/slot rather than direct handle to the target class instance.

                1 Reply Last reply
                0

                1/6

                29 Nov 2021, 13:46

                • Login

                • Login or register to search.
                1 out of 6
                • First post
                  1/6
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved