Problem type-casting to a derived class

R

Rui Maciel

Consider that I have the following classes: Base, A, B, Derived

Class A inherits class Base and class Derived inherits both class A and class B. So, the
inheritance diagram would be something like (mind the ASCII art)

Base
|
A B
-------
|
Derived


Let's assume that I get a pointer to an object of type Base. Yet, I need to access attributes which
are defined in class B.

The thing is, a dynamic_cast<> from Base to B fails to materialize and a reinterpret_cast<> also
appears to fail.

So, is this possible? If so, what's the secret?


Thanks in advance,
Rui Maciel
 
R

Rui Maciel

Rui said:
Consider that I have the following classes: Base, A, B, Derived

Class A inherits class Base and class Derived inherits both class A and
class B. So, the inheritance diagram would be something like (mind the
ASCII art)

Base
|
A B
-------
|
Derived


Let's assume that I get a pointer to an object of type Base. Yet, I need
to access attributes which are defined in class B.

The thing is, a dynamic_cast<> from Base to B fails to materialize and a
reinterpret_cast<> also appears to fail.

As added info, class A and class Derived may be any class from a given set of classes. To be more
specific, class A is a standard GUI widget, class B is a class intended to store attributes and
methods which must be common to a set of widgets and class Derived is a customized widget.


Rui Maciel
 
I

Ian Collins

Consider that I have the following classes: Base, A, B, Derived

Class A inherits class Base and class Derived inherits both class A and class B. So, the
inheritance diagram would be something like (mind the ASCII art)

Base
|
A B
-------
|
Derived


Let's assume that I get a pointer to an object of type Base. Yet, I need to access attributes which
are defined in class B.

So you have to cast to something derived from B, which in this case
would be a Derived.
The thing is, a dynamic_cast<> from Base to B fails to materialize and a reinterpret_cast<> also
appears to fail.

Obviously, there isn't a polymorphic relationship between Base and B.
 
R

Rui Maciel

Ian said:
So you have to cast to something derived from B, which in this case
would be a Derived.

No exactly. I need to cast an object of type Base to an object of type B, which is a common base
class of Derived but which isn't a derived class of Base.


Rui Maciel
 
P

Paul Bibbings

Rui Maciel said:
Consider that I have the following classes: Base, A, B, Derived

Class A inherits class Base and class Derived inherits both class A
and class B. So, the inheritance diagram would be something like
(mind the ASCII art)

Base
|
A B
-------
|
Derived


Let's assume that I get a pointer to an object of type Base. Yet, I
need to access attributes which are defined in class B.

The thing is, a dynamic_cast<> from Base to B fails to materialize and
a reinterpret_cast<> also appears to fail.

In what way does the dynamic_cast said:
So, is this possible? If so, what's the secret?

10:23:05 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/CLCPP $cat casting_to_derived.cpp // file: casting_to_derived.cpp

#include <cassert>

class Base {
public:
virtual ~Base() { }
};

class A : public Base { };

class B { };

class Derived : public A, public B { };

int main()
{
Derived d;
Base *b_ptr = &d;

assert(dynamic_cast<B*>(b_ptr));
}


10:23:10 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/CLCPP $g++ -static -o casting_to_derived
casting_to_derived.cpp

10:23:15 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/CLCPP $./casting_to_derived

10:23:23 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/CLCPP $

Regards

Paul Bibbings
 
P

Paul Bibbings

Paul Bibbings said:
In what way does the dynamic_cast<B*> fail?

That was a stupid thing to ask, of course. What I /meant/ to ask was,
how are you structuring your classes in an example that illustrates it
failing?

Regards

Paul Bibbings
 
I

Ian Collins

No exactly. I need to cast an object of type Base to an object of type B, which is a common base
class of Derived but which isn't a derived class of Base.

Why?

An "object of type Base" is a base, do you mean a Base pointer or
reference? If so, what was assigned to said pointer or reference? If
it was a Derived, just dynamic_cast to a Derived and access the data in
B through that.
 
R

Rui Maciel

Rui said:
Consider that I have the following classes: Base, A, B, Derived

Class A inherits class Base and class Derived inherits both class A and
class B. So, the inheritance diagram would be something like (mind the
ASCII art)

Base
|
A B
<snip/>

Scratch that. I've managed to get it to work. A dynamic_cast<> worked as expected after I've fixed
a small problem regarding the inheritance of class B, which was being derived by class Derived
through private inheritance instead of public. Fixing this, the dynamic_cast<> makes it possible to
pick up a pointer to an object of class Base, dynamic_cast<>ing it to class B and then access class
B's methods. Nice.


Thanks for the help.
Rui Maciel
 
R

Rui Maciel

Paul said:
That was a stupid thing to ask, of course. What I /meant/ to ask was,
how are you structuring your classes in an example that illustrates it
failing?

While producing a working example I've managed to find out where I screwed up and proceed to fix it.
Good stuff.


Thanks for the help,
Rui Maciel
 
R

Rui Maciel

Pete said:
That's why you should post code and not hand-waving descriptions of
what you think your code is doing.

Do you get any pleasure from posting comments that make you sound a tad arrogant?


Rui Maciel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top