Advertisement

C++/Inheritance problem

Started by March 28, 2000 07:13 AM
2 comments, last by Starfall 24 years, 5 months ago
I''m a bit rusty on my C++, and am having one problem in particular that has me flummoxed. Basically I have 2 classes, A and B. A needs to access some member functions and variables of B, and vice versa. I have a pointer to A in B, and a pointer to B in A, which I link up when I create the objects. The problem comes in when I have 2 more classes A2 and B2, derived from A and B respectively. I want A2 to not only be able to access the functions declared in B, but also the new functions in B2. If I leave things as they are, I cannot access the new functions because my pointer in A2 is still of type B, and not B2, therefore the functions are not available. However, if I change the type of the pointer in A2 to type B2, the functions derived from A into A2 see this new pointer''s value as being null when I call them from an A2 object. As I see it my best option is to make a separate pointer of type B2 in my A2 object and use that to call the new functions declared in B2. However, before I do this, I thought I''d ask here to see if there was an easier/better solution. I hope my explanation has not been too confusing, and I''d appreciate any suggestions anyone might have. Thanks! Starfall
Okay, first up, I don''t understand why this happens...

"However, if I change the type of the pointer in A2 to type B2, the functions derived from A into A2 see this new pointer''s value as being null when I call them from an A2 object."

How do you _change_ the type of the pointer in A2?
A pointer or instantiation of an object B2 inherited from B should be able to access all public member functions and variables of B.
It won''t be able to access private member functions and variables but you said it failed on the public ones.
Could I see an example of this please i.e. a cut down version of the classes with a call that doesn''t work.

Secondly can you explain what you are trying to achieve here because it''s possible there''s a better design for your problem. Not necessarily, I know, I''ve recently had to contend with an inheritance based linked-list problem which involved me either carrying out a class cast (bad) or duplicating multiple functions in all the derived classes. After 6 hours staring at a single line of code I eventually convinced myself there was no better way (maybe I''ll post the problem on this board and be proved wrong).

Anyway Starfall, more detail please

Mike
Advertisement
You could always do a dynamic cast.... I don''t know the exact syntax, and it seems to be frowned upon by other programmers, but I have used it once or twice and gotten satisfactory results.

--------------------


You are not a real programmer until you end all your sentences with semicolons;
www.trak.to/rdp

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

You either have to a) explicitly cast the "pointer to A" to a "pointer to A2" (and for B/B2 the same), or declare the new functions as virtual (possibly abstract) in A2 in A as well.
The reason is that the compiler can never know what new functions any derived class may declare if you use a pointer to the base class. So you must use an explicit cast (if you know what type should be casted to), or declare the functions in the base class as virtual (possibly abstract).

Erik

This topic is closed to new replies.

Advertisement