473,406 Members | 2,259 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,406 software developers and data experts.

Protected members

Why doesn't this work in g++?

class Base {
public:
Base() {}
~Base() {}
protected:
int foo;
}

class Derived : public Base {
public:
Derived(Base other) { foo = other.foo } // error : foo is protected
in this context
Derived() : foo(1) {} // error : no field foo in Base
Derived() { foo = 1; } // this works (obviously)
~Derived() {}
}

Is there any way to get around this?

Thanks,
Boaz

Aug 2 '05 #1
6 1763
ba****@gmail.com wrote:
Why doesn't this work in g++?

class Base {
public:
Base() {}
~Base() {}
protected:
int foo;
}

class Derived : public Base {
public:
Derived(Base other) { foo = other.foo } // error : foo is protected
in this context
You cannot access protected members of other objects than this.
Derived() : foo(1) {} // error : no field foo in Base
Derived cannot initialize foo because Base already did: Base owns it.
Derived() { foo = 1; } // this works (obviously)
~Derived() {}
}

Is there any way to get around this?


1) use member functions
2) make Base initialize foo, pass an argument if necessary
Jonathan

Aug 2 '05 #2
<ba****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com
Why doesn't this work in g++?

class Base {
public:
Base() {}
~Base() {}
protected:
int foo;
}

class Derived : public Base {
public:
Derived(Base other) { foo = other.foo } // error : foo is protected
in this context
Protected members are accessible to Derived only when part of a Derived
object's base. They are not accessible when part of a free standing Base
object or a Base component of some other type of object.

You can get access with a proper copy constructor:

Derived(const Derived & other) { foo = other.foo }
Derived() : foo(1) {} // error : no field foo in Base
An initialisation list of a derived class can only include

1. calls to base class constructors
2. initialisation of a class's own data members.

Direct initialisation of base class data members is not allowed. You should
define a constructor for the base class. If it is a default constructor
(i.e., has no arguments), then it will be called automatically. Otherwise,
you need to call it explicitly in the initialisation list.
Derived() { foo = 1; } // this works (obviously)
~Derived() {}
}

Is there any way to get around this?

Thanks,
Boaz

--
John Carson
Aug 2 '05 #3
* ba****@gmail.com:
Why doesn't this work in g++?

class Base {
public:
Base() {}
~Base() {}
protected:
int foo;
}

class Derived : public Base {
public:
Derived(Base other) { foo = other.foo } // error : foo is protected
in this context
Derived() : foo(1) {} // error : no field foo in Base
Derived() { foo = 1; } // this works (obviously)
~Derived() {}
}
Because the C++ language, not the g++ compiler, is defined that way.

To ease the explanation of why the language is defined that way, I'll first
improve the constructor declaration:

Derived( Base const& other ) { foo = other.foo; }

which is still not valid, but at least has pass-by-reference and the
required semicolon.

Now consider that 'other' may be of 'Base'-derived class 'Sensitive', where
the 'foo' member is always assumed to specify the size of a buffer. If
'Derived' could change 'foo' at will it could invalidate that extra
assumption in 'Sensitive', which 'Sensitive' otherwise is free to make based
on knowing that 'Base' doesn't meddle. It's not a very strong argument, but
it's basically the reason why C++ 'protected' only grants you access to that
member in objects of the class of the current object, or of classes derived
from that class; the relevant paragraph in the standard is §11.5/1.

Is there any way to get around this?


Provide a protected 'Base' constructor that takes a value for 'foo' as
argument, and make 'foo' private in 'Base'. Or, better, redesign.

--
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?
Aug 2 '05 #4
* Jonathan Mcdougall:

You cannot access protected members of other objects than this.


class Derived;

class Base
{
protected:
int foo;
public:
void hm( Derived const& );
};

class Derived: public Base {};

void Base::hm( Derived const& other )
{
foo = other.foo;
}

int main() { Derived o; o.hm( o ); }

--
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?
Aug 2 '05 #5
Thanks for the answers! I figured that this was standard, but I just
wanted to know why. I see why this could be a problem in the
"Sensitive" case. However, I still have an issue, since I am writing a
program in which my base class needs to set itself up using an instance
of the derived class. I could provide accessors, but I feel like I'm
breaking encapsulation just for a derived class. I could also provide
some type of (protected) constructor in the base class, but I think
that the derived class's constructor really doesn't belong there.

This seems to me like a "feature" that makes inheritance work
inconsistently . . .

--Boaz

Aug 2 '05 #6
Then again, I guess I don't really care if there's a read-only property
visible . . .

Aug 2 '05 #7

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

Similar topics

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...
6
by: away | last post by:
Why some classes are seen to has their constructors declared as "protected"? Thanks!
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: Jordan | last post by:
AFAIK there are two ways to expose members of a base class to a derived or child class: 1. declare the members public in the base class 2. declare them as 'protected' in the base class Is...
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
14
by: mlimber | last post by:
In an article on the safe bool idiom (http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives the following code (slightly modified): class safe_bool_base { protected: typedef...
6
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected...
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; }
2
by: t | last post by:
Lippman's C++ Primer, 4th ed., p562, dicussion of protected members seems to be wrong, unless I am misinterpreting things. He says: "A derived class object may access the protected members of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.