Discussion Locked

Question in C++

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have one class a and I am deriving class b from a and i have one pointer to b .
I have one function which i will be calling and in turn it will change the address of base class in the same time it should not change class b.

Can anyone give a solution to this.

7 replies

by davidandgoliath

Even though class B is derived from A when you instantiate B there is only one object. That is the pointer to B is also a pointer to A so there is no way to do what you are asking if I understand the quesiton correctly.

by rodsherer

I agree with what the last person said. You don't actually have two objects. You made a pointer to a single object which has teh attributes of class a, as well as the attributes of class b (which may over-ride or add to the attributes of class a). If you want an a-class object to be manipulated, then you need to create a second object. If you want to manipulate data held by your class-b object, with out actually affecting it, then make a second object which is a copy the first object. Make sure you don't just copy the address of your initial object though.

by mpdcsup

Classes don't have addresses, so what exactly does the address your changing contain? And how are you going to create a pointer to something that has no address?

Now, if (a) and (b) are really instances of classes A and B (and class B is derivedfrom A) then the only address you can change is (a) without affecting (b).

Just remember, (a) and (b) are instantiated from definitions A and B, respectively; the definitions are consumed at compile-time and the instantiation occurs at run-time.Once the program is off and running, there are no *handles* to A and B to manipulate.

This discussion is closed to new replies.