G
Gerd Schmitt
Hi gurus et al.
My collegues and I encouter a somewhat strange behaviour of our
compilers and are unsure whether or not the compiler is right.
It seems that anything declared protected in a base class can only be
accessed by the child class if it is the same instance.
I allway thought the C++ type system only relies on static types. Am I
wrong?
It would be nice if someone could enligthen me
Gerd
Here the sample code wich does not compile:
class A
{
int privA;
protected:
int protA;
void protMethA(A* a) { privA = a->privA; }
public:
int pub;
void pubMethA(A* a) { privA = a->privA; }
};
class B : public A
{
int privB;
protected:
void protMethB(A* a)
{
protA = 5;
a->protMethA(this);
int tmp = a->protA;
}
public:
void pubMethB(A* a)
{
protA = 3;
a->protMethA(this);
int tmp = a->protA;
}
void pubMethB2(B* rhs)
{
rhs->privB = 5;
}
};
int main()
{
A a;
B b;
a.pubMethA(&b);
b.pubMethB(&a);
return 0;
}
My collegues and I encouter a somewhat strange behaviour of our
compilers and are unsure whether or not the compiler is right.
It seems that anything declared protected in a base class can only be
accessed by the child class if it is the same instance.
I allway thought the C++ type system only relies on static types. Am I
wrong?
It would be nice if someone could enligthen me
Gerd
Here the sample code wich does not compile:
class A
{
int privA;
protected:
int protA;
void protMethA(A* a) { privA = a->privA; }
public:
int pub;
void pubMethA(A* a) { privA = a->privA; }
};
class B : public A
{
int privB;
protected:
void protMethB(A* a)
{
protA = 5;
a->protMethA(this);
int tmp = a->protA;
}
public:
void pubMethB(A* a)
{
protA = 3;
a->protMethA(this);
int tmp = a->protA;
}
void pubMethB2(B* rhs)
{
rhs->privB = 5;
}
};
int main()
{
A a;
B b;
a.pubMethA(&b);
b.pubMethB(&a);
return 0;
}