I have two classes:
class A
{
public:
int i;
};
class B : public A
{
public:
int i;
};
Suppose that I created an object for class B
B b;
Is it possible to access A::i
using b
?
I have two classes:
class A
{
public:
int i;
};
class B : public A
{
public:
int i;
};
Suppose that I created an object for class B
B b;
Is it possible to access A::i
using b
?
Is it possible to access A::i using b?
Yes!
How about b.A::i
? ;)
Yes you can. To find out read this
An override method provides a new implementation of a member inherited from a base class. The method overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method.
From within the derived class that has an override method, you still can access the overridden base method that has the same name by using the base keyword. For example, if you have a virtual method MyMethod(), and an override method on a derived class, you can access the virtual method from the derived class by using the call:
base.MyMethod()
Yes:
int main()
{
B b;
b.i = 3;
b.A::i = 5;
A *pA = &b;
B *pB = &b;
std::cout << pA->i << std::endl;
std::cout << pB->i << std::endl;
}
Two ways:
struct A{
A():i(1){}
int i;
};
struct B : A{
B():i(0), A(){}
int i;
};
int main(){
B b;
cout << b.A::i;
cout << (static_cast<A&>(b)).i;
}