Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > protected by instance, not by type?

Reply
Thread Tools

protected by instance, not by type?

 
 
Gerd Schmitt
Guest
Posts: n/a
 
      09-28-2004
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;
}
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-28-2004

"Gerd Schmitt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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
}
};

> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Make sure disk is not full or write-protected" error???? Ric Wireless Networking 4 09-16-2005 10:42 AM
File Types not protected by Forms Authentication MatthewRoberts ASP .Net 4 06-17-2005 04:50 PM
is not accessible in this context because it is 'Protected' Murphy ASP .Net 6 04-19-2004 11:44 PM
Have not all the pages protected by the login page Philippe Meunier ASP .Net 1 10-24-2003 10:50 PM
Difference between "Protected WithEvents myClassName" And "Protected myClassName" ? Andreas Klemt ASP .Net 2 07-05-2003 12:51 AM



Advertisments