C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

protected by instance, not by type?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gerd Schmitt
Guest





PostPosted: Tue Sep 28, 2004 12:37 pm    Post subject: protected by instance, not by type? Reply with quote



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;
}
Back to top
John Harrison
Guest





PostPosted: Tue Sep 28, 2004 12:58 pm    Post subject: Re: protected by instance, not by type? Reply with quote




"Gerd Schmitt" <gschmitt (AT) it-studio (DOT) de> wrote

Quote:
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.

No, that's not right. But a protected member can only be accessed by a
pointer of the same type or derived type as the accessing class.

class A
{
protected:
int x;
};

class B : public A
{
public:
void f(A* a, B* b)
{
a->x = 1; // error, A is not the same as or derived from B
b->x = 2; // ok
}
};

Quote:
I allway thought the C++ type system only relies on static types. Am I
wrong?

I don't see the relevance of that. We are talknig about the access system
not the type system. In any case the C++ type system has dynamic typing,
e.g. virtual functions and dynamic_cast.

john



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.