473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

protected member

REH
Can someone please tell me what is wrong with this snippet of code?
GCC is telling me that "foo2 is protected within this context" (that
I cannot use it with BBB).
class AAA {
protected:
virtual int foo2() {return 0;}
};

class BBB : public AAA {
public:
int foo(AAA& a) {return a.foo2();}
protected:
virtual int foo2() {return 1;}
};

May 19 '07 #1
6 2464
On May 19, 4:32 pm, REH <spamj...@stny.rr.comwrote:
Can someone please tell me what is wrong with this snippet of code?
GCC is telling me that "foo2 is protected within this context" (that
I cannot use it with BBB).

class AAA {
protected:
virtual int foo2() {return 0;}

};

class BBB : public AAA {
public:
int foo(AAA& a) {return a.foo2();}
protected:
virtual int foo2() {return 1;}};
BBB has access to AAA::foo2 only as a part of itself, i.e. you can do
this->AAA::foo2() inside BBB::foo, but you cant do it on a separate
object. You can however befirend BBB in AAA, and thus gain access to
separate objects too. Also, if you're certain the dynamic type of your
'a' parameter is BBB, you can do "static_cast<BBB&>(a).foo2();" , this
would probably compile to non-virtual foo2 call though.

May 19 '07 #2
BBB has access to AAA::foo2 only as a part of itself, i.e. you can do
this->AAA::foo2() inside BBB::foo, but you cant do it on a separate
object. You can however befirend BBB in AAA, and thus gain access to
separate objects too. Also, if you're certain the dynamic type of your
'a' parameter is BBB, you can do "static_cast<BBB&>(a).foo2();" , this
would probably compile to non-virtual foo2 call though.
when you see 'separate object' in this post, read it as a 'separate
object of type AAA' of course ;]

May 19 '07 #3
On May 19, 10:32 am, REH <spamj...@stny.rr.comwrote:
Can someone please tell me what is wrong with this snippet of code?
GCC is telling me that "foo2 is protected within this context" (that
I cannot use it with BBB).

class AAA {
protected:
virtual int foo2() {return 0;}

};

class BBB : public AAA {
public:
int foo(AAA& a) {return a.foo2();}
protected:
virtual int foo2() {return 1;}

};
The member function int BBB::foo(AAA&) is attempting to access the
interface provided by the parameter's type, not that instance of BBB.
the parameter AAA& a is *not* the base entity so only the public
interface is available.

Why should the program give you access to a part of the object you
didn't provide access to?
May 19 '07 #4
* REH:
Can someone please tell me what is wrong with this snippet of code?
GCC is telling me that "foo2 is protected within this context" (that
I cannot use it with BBB).
class AAA {
protected:
virtual int foo2() {return 0;}
};

class BBB : public AAA {
public:
int foo(AAA& a) {return a.foo2();}
protected:
virtual int foo2() {return 1;}
};
Consider

class Foo
{
protected:
void doDangerousStuff() {}
Foo() {}
public:
void doSafeStuff() {}
};

class UsefulFoo: public Foo
{
public:
UsefulFoo() {}
};

struct FooHacked: Foo
{
void doThatDangerousStuff( Foo& o )
{ o.doDangerousStuff(); }
};

int main()
{
UsefulFoo o;
FooHacked::doThatDangerousStuff( o );
}

If C++ allowed this, then "protected" wouldn't yield much protection,
now would it? You could then easily, inadvertently, do something like
FooHacked. I'm not sure whether this is in the FAQ or not (if not it
should be there, it's certainly frequently asked about): a quick
skimming of the FAQ didn't find it.

Of course you can override the protection by casting, but typically that
invokes formally Undefined Behavior.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 19 '07 #5
* Alf P. Steinbach:
void doThatDangerousStuff( Foo& o )
Add "static".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 19 '07 #6
On Sat, 19 May 2007 07:50:28 -0700, antonov84 wrote:
On May 19, 4:32 pm, REH <spamj...@stny.rr.comwrote:
>Can someone please tell me what is wrong with this snippet of code? GCC
is telling me that "foo2 is protected within this context" (that I
cannot use it with BBB).

class AAA {
protected:
virtual int foo2() {return 0;}

};

class BBB : public AAA {
public:
int foo(AAA& a) {return a.foo2();}
protected:
virtual int foo2() {return 1;}};

BBB has access to AAA::foo2 only as a part of itself, i.e. you can do
this->AAA::foo2() inside BBB::foo, but you cant do it on a separate
object. You can however befirend BBB in AAA, and thus gain access to
separate objects too.
Member access is based on type not object identity. Access to AAA::foo2()
is allowed for a separate object but only through a BBB reference (or
pointer).
Also, if you're certain the dynamic type of your
'a' parameter is BBB, you can do "static_cast<BBB&>(a).foo2();" , this
would probably compile to non-virtual foo2 call though.
The call is still virtual and "static_cast<BBB&>(a).AAA::foo2();" is also
valid.

--
Markus Schoder
May 19 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Piotre Ugrumov | last post by:
I have tried to modify my exercise about the simulation of the life in the savannah. Now I have only 2 errors but I don't comprehend how resolve these errors. If I try to call the method getX() and...
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
4
by: Siemel Naran | last post by:
Can Derived class static member access protected member from base class? class Base { protected: void setvariable(int); }; class Derived : Base { public: static std::auto_ptr<Base> out(new...
5
by: Andy Lomax | last post by:
Can anyone tell me why the code below doesn't compile? The code has a simple hierarchy of publically-derived classes: A -> B -> C. A declares a protected member 'foo'. C declares an object of...
1
by: karolszk | last post by:
Hi! The following program gcc returns error: class Z { int a; int b; };
16
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
4
by: Joseph Paterson | last post by:
Hi all, I'm having some trouble with the following code (simplified to show the problem) class Counter { protected: int m_counter; }
12
by: MiG | last post by:
Hello, Consider the following code snippet: class B { protected: B() {} ~B() {} };
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.