In C++ I have a base class A for example which looks like this:
class A {
enum type {B, C, D, E};
....
....
}
For each enumerated type I have another class which is derived from the base A.
For example I have one class like this:
class B : public A {
int x;
....
....
};
My question is: With an A object and from it for which I know that is type B so it corresponds to class B is it possible to have access to variable x for example and if yes how? I know that it is a bit tricky to gain access to derived class from base but I suppose that is something different not like this.
If you have an A *a
or A &a
and know for certain that it actually points/refers to an object of type B
, you can use a cast:
A *a = ...;
B *b = static_cast<B*>(a);
or
A &a = ...;
B &b = static_cast<B&>(a);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.