What's happened when allocating different objects to the same pointer with dynamic memory allocation technic???
-
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. -
@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.@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??
-
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.@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. -
@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.@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??
-
@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??
@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 CIn 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. -
@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 CIn 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.@jsulm many thanks, sorry for the delayed response, I finally got your points :) and sorry for my crapy English again.