473,605 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing a protected member of a member of type BaseClass????

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 in Rectangle, the compiler
tells me Point::x is protected. I would have expected Rectangle to see the
protected members of any Point. Compiling the following code give me this
error:
g++ -o rectangle main.cc
main.cc: In member function `size_t Rectangle::dx() ':
main.cc:22: error: `size_t Point::y' is protected
main.cc:32: error: within this context

#include <iostream>
#include <cstddef> //size_t

class Point {

public:
Point(const size_t& x_=0,
const size_t& y_=0)
: x(x_),
y(y_)
{}

Point& operator+(const Point& p)
{
this->x += p.x;
this->y += p.y;
return *this;
}

protected:
size_t x;
size_t y; //line 22
};

class Rectangle : public Point {
public:
Rectangle(const Point& xy, const Point& dxdy_)
: Point(xy),
dxdy(dxdy_)
{}
size_t X() const { return this->x; }
size_t dx() const { return this->dxdy.y; } //line 32
protected:
Point dxdy;
};
int main(){

}

ISO/IEC 14882 says this:

11.2 Accessibility of base classes and base class members

[class.access.ba se]

"If a class is declared to be a base class (clause 10) for another class
using the public access specifier, the public members of the base class are
accessible as public members of the derived class and protected members of
the base class are accessible as protected members of the derived class."

The way I read that, I should be able to access Rectangle::dxdy .y from
within Rectangle. Am I missing something, or is g++ wrong?
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #1
2 2293
Steven T. Hatton wrote:
I find the surprising. If I derive Rectangle from Point, I can access the

....

This looks like an answer. I'm not accessing Point::dxdy through a pointer
to Rectangle. I would need to get really brutal with a cast to do so, and
I'm not sure that would even work(correctly/reliably).

http://publib.boulder.ibm.com/infoce...ns.cplr162.htm

A protected nonstatic base class member can be accessed by members and
friends of any classes derived from that base class by using one of the
following:

* A pointer to a directly or indirectly derived class
* A reference to a directly or indirectly derived class
* An object of a directly or indirectly derived class

If a class is derived privately from a base class, all protected base class
members become private members of the derived class.

If you reference a protected nonstatic member x of a base class A in a
friend or a member function of a derived class B, you must access x through
a pointer to, reference to, or object of a class derived from A. However,
if you are accessing x to create a pointer to member, you must qualify x
with a nested name specifier that names the derived class B. The following
example demonstrates this:

class A {
public:
protected:
int i;
};
class B : public A {
friend void f(A*, B*);
void g(A*);
};

void f(A* pa, B* pb) {
// pa->i = 1;
pb->i = 2;

// int A::* point_i = &A::i;
int A::* point_i2 = &B::i;
}

void B::g(A* pa) {
// pa->i = 1;
i = 2;

// int A::* point_i = &A::i;
int A::* point_i2 = &B::i;
}

void h(A* pa, B* pb) {
// pa->i = 1;
// pb->i = 2;
}

int main() { }

Class A contains one protected data member, an integer i. Because B derives
from A, the members of B have access to the protected member of A. Function
f() is a friend of class B:

* The compiler would not allow pa->i = 1 because pa is not a pointer to
the derived class B.
* The compiler would not allow int A::* point_i = &A::i because i has
not been qualified with the name of the derived class B.

Function g() is a member function of class B. The previous list of remarks
about which statements the compiler would and would not allow apply for g()
except for the following:

* The compiler allows i = 2 because it is equivalent to this->i = 2.

Function h() cannot access any of the protected members of A because h() is
neither a friend or a member of a derived class of A.
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #2
On Mon, 16 Aug 2004 07:15:24 -0400, "Steven T. Hatton"
<su******@setid ava.kushan.aa> wrote:
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 in Rectangle, the compiler
tells me Point::x is protected.
Yes, that's correct behaviour, and is required to maintain
encapsulation.
I would have expected Rectangle to see the
protected members of any Point.
But that would allow it to break instances of completely unrelated
classes that just happen to also derive from Point. For example,
perhaps Trapezium derives from point; we don't want Rectangle to be
able to modify the Point subobject of a Trapezium.
ISO/IEC 14882 says this:

11.2 Accessibility of base classes and base class members

[class.access.ba se]

"If a class is declared to be a base class (clause 10) for another class
using the public access specifier, the public members of the base class are
accessible as public members of the derived class and protected members of
the base class are accessible as protected members of the derived class."

The way I read that, I should be able to access Rectangle::dxdy .y from
within Rectangle. Am I missing something, or is g++ wrong?


See 11.5/1

Tom
Jul 22 '05 #3

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

Similar topics

5
2406
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
1
1774
by: Nick | last post by:
Why is it that in the example below I get an error saying that I "cannot access protected member 'i' via a qualifier of type 'Base'; the qualifier must be of type 'Derived'"?? but when i make m_i public it works fine and i can access the member. class Base { protected int m_i; //doesn't work //public int m_i; //works
4
1581
by: hweekuan | last post by:
Hi, I got an error with gnu C++ but not with intel C++ (icc). The distilled code is given below, a variable "T" is stored as a const T& in the base class and when the derived class try to access it, the compiler gives an error. Compile error with gnu C++ and version of C++ compiler is also given below. I don't know if its compiler bug. Any help and comments will be very much appreciated. Thank you.
5
5852
by: MrJim | last post by:
How should variables be declared and referenced in both the base and derived form so they can be accessed?
2
2631
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh class BaseClass {
4
8496
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; }
13
2124
by: Tom Baxter | last post by:
Hi everyone, Has anyone looked at section 18.1.1 of the C# spec? It indicates 'new' and 'protected' are valid modifiers on struct declarations. First, how can 'protected' be valid on a struct, since structs cannot be inherited? The compiler gives an error (as I expect it should) if you try this: protected struct MyStruct {} so I'm wondering if the spec is wrong when it says 'protected' is a valid
12
4798
by: Robert Fuchs | last post by:
Hello, This example: public class BaseC { public int x; public void Invoke() {} } public class DerivedC : BaseC
6
8149
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a situation where I am implementing base class functions by including a pointer to subclass member in base class. Reason being functionality is common for subclasses but the members are common within subclass only (static member of subclass) but...
0
8001
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7934
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8424
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
6742
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5886
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3912
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1537
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1270
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.