Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What's happened when allocating different objects to the same pointer with dynamic memory allocation technic???

What's happened when allocating different objects to the same pointer with dynamic memory allocation technic???

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 719 Views
  • 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.
  • L Offline
    L Offline
    loveplay1983
    wrote on last edited by
    #1

    I know it sounds a little bit of awkard, and sorry for my bad English skill due to "Not a native English speaker", c++ beginner and qt newbie here.
    What I want to express and ask for is that I notice there are a lot of tutorials use dynamic memory allocation and sometimes they allocate the different class objects to the same pointer within the same application which is hard for me to comprehend.
    If anyone can help me out, it would be very nice and I appreciate it.

    jsulmJ 1 Reply Last reply
    0
    • jsulmJ jsulm

      @loveplay1983 I guess in these tutorials they are creating instances of classes which are in same hierarchy?
      Like:

      class A {};
      class B : public A {};
      class C : public B {};
      
      A *object = new A();
      object = new B();
      object = new C();
      

      All these assignments are valid because B is A, C is B is A. It is like a BMW is a car and a car is a vehicle.
      If you mean something else then please post the code examples from these tutorials.

      L Offline
      L Offline
      loveplay1983
      wrote on last edited by
      #3

      @jsulm Thanks my friend, I think you have solved my question, so those different objects point to the same pointer actually meant to make the objects become identical, am I right??

      jsulmJ 1 Reply Last reply
      0
      • L loveplay1983

        I know it sounds a little bit of awkard, and sorry for my bad English skill due to "Not a native English speaker", c++ beginner and qt newbie here.
        What I want to express and ask for is that I notice there are a lot of tutorials use dynamic memory allocation and sometimes they allocate the different class objects to the same pointer within the same application which is hard for me to comprehend.
        If anyone can help me out, it would be very nice and I appreciate it.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @loveplay1983 I guess in these tutorials they are creating instances of classes which are in same hierarchy?
        Like:

        class A {};
        class B : public A {};
        class C : public B {};
        
        A *object = new A();
        object = new B();
        object = new C();
        

        All these assignments are valid because B is A, C is B is A. It is like a BMW is a car and a car is a vehicle.
        If you mean something else then please post the code examples from these tutorials.

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

        L 1 Reply Last reply
        4
        • jsulmJ jsulm

          @loveplay1983 I guess in these tutorials they are creating instances of classes which are in same hierarchy?
          Like:

          class A {};
          class B : public A {};
          class C : public B {};
          
          A *object = new A();
          object = new B();
          object = new C();
          

          All these assignments are valid because B is A, C is B is A. It is like a BMW is a car and a car is a vehicle.
          If you mean something else then please post the code examples from these tutorials.

          L Offline
          L Offline
          loveplay1983
          wrote on last edited by
          #3

          @jsulm Thanks my friend, I think you have solved my question, so those different objects point to the same pointer actually meant to make the objects become identical, am I right??

          jsulmJ 1 Reply Last reply
          0
          • L loveplay1983

            @jsulm Thanks my friend, I think you have solved my question, so those different objects point to the same pointer actually meant to make the objects become identical, am I right??

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @loveplay1983 An object does not point to a pointer. The objects in my example are not identical.

            A *a = new A(); // a points to an instance of A
            B *b = new B(); // b points to an instance of B
            C *c = new C(); // c points to an instance of C
            
            A *ptr = c; // ptr points to an instance of C
            

            In this example ptr points to an instance of C. BUT: ptr is of type A* not C*. That means: using ptr you can only access methods and members of C which are defined in A.

            class A
            {
            public:
                void methodA() {}
            }
            
            class C : public A
            {
            public:
                void methodC() {}
            }
            
            A *a = new A();
            C *c = new C();
            A *ptr = c; // ptr points to an instance of C
            ptr->methodA(); // Works
            ptr->methodC(); // Does NOT work!
            c->methodC(); // Works
            

            "make the objects become identical" - objects are still different here, but the type of the pointer defines how you can use these objects through the pointer.
            You should read about polymorphism in object oriented languages.

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

            L 1 Reply Last reply
            2
            • jsulmJ jsulm

              @loveplay1983 An object does not point to a pointer. The objects in my example are not identical.

              A *a = new A(); // a points to an instance of A
              B *b = new B(); // b points to an instance of B
              C *c = new C(); // c points to an instance of C
              
              A *ptr = c; // ptr points to an instance of C
              

              In this example ptr points to an instance of C. BUT: ptr is of type A* not C*. That means: using ptr you can only access methods and members of C which are defined in A.

              class A
              {
              public:
                  void methodA() {}
              }
              
              class C : public A
              {
              public:
                  void methodC() {}
              }
              
              A *a = new A();
              C *c = new C();
              A *ptr = c; // ptr points to an instance of C
              ptr->methodA(); // Works
              ptr->methodC(); // Does NOT work!
              c->methodC(); // Works
              

              "make the objects become identical" - objects are still different here, but the type of the pointer defines how you can use these objects through the pointer.
              You should read about polymorphism in object oriented languages.

              L Offline
              L Offline
              loveplay1983
              wrote on last edited by
              #5

              @jsulm many thanks, sorry for the delayed response, I finally got your points :) and sorry for my crapy English again.

              1 Reply Last reply
              0

              • Login

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