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

nested classes accessing inherited protected members of parents

There's probably a simple explanation for this and it's just too late
for my brain to work. Does anybody know a good reason why class B2
compiles but class B1 doesn't?

class A {
public:
class Nested_A {
protected:
A* ptrA;
};
protected:
int X;
};

class B1 : public A {
public:
class Nested_B : public Nested_A {
int foo() {return(ptrA->X);}
// error: protected member "A::X" is not accessible through a "A"
pointer or object
};
};

class B2 : public A {
public:
class Nested_B : public Nested_A {
int foo() {return(ptrB->X);} // Happy compiler

protected:
B* ptrB;
};
};
Thanks!!

Dec 1 '05 #1
2 2034
Mr Dyl wrote:
There's probably a simple explanation for this and it's just too late
for my brain to work. Does anybody know a good reason why class B2
compiles but class B1 doesn't? ....


It's because A::X is being accessed through a pointer other than a
pointer that the scope has visibility. i.e. Nested_B does not have
access to protected parts of A, but Nested_A does. B1 and B2 does have
visibility into the protected parts of A.

What's weird (and logically a hole in the standard IMHO), is that you
can take a member pointer to B1::X - which is exactly the same as A::X,
but since B1::Nested_B has visibility into B1, it can take B1::X which
the allows you to directly access A::X from an A&. See below.

Probably the best thing is to provide an accessor in Nested_A that
promotes the visibility to classes derived from Nested_A (see GetAX()
below).

class A {
public:
class Nested_A {
protected:
int & GetAX()
{
return ptrA->X;
}
A* ptrA;
};
protected:
int X;
};

class B1 : public A {
public:
class Nested_B : public Nested_A {
int foo() {return(ptrA->X);}
// error: protected member "A::X" is not accessible through a "A"
pointer or object
int foo1() {return(ptrA->*(&B1::X));}
int foo2() {return(GetAX());}
};
};

class B2 : public A {
public:
class Nested_B : public Nested_A {
int foo() {return(ptrB->X);} // Happy compiler
int foo1() {A* ptrA=ptrB; return(ptrA->X);} // UNhappy compiler

protected:
B2* ptrB;
};
};
Dec 1 '05 #2
It does seem a bit odd that "ptrA->*(&B1::X)" is allowed eh?

Thanks for helping to clear the fog ;)

Dec 1 '05 #3

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

Similar topics

5
by: Daniel Aarno | last post by:
Can anyone provide a good explanation to why the following does not work? After all since A is a public base of B objects of type B ISA object of type A. class A { protected: void foo() {} };...
2
by: JH | last post by:
I am attempting to enable double buffering for a panel control. The SetStyle method of the panel control is protected, so I am attempting to access it by writing a special panel class that...
4
by: Dan | last post by:
I have a need to make a set of classes that all share the same public methods, some implementation and some data. So, I made an abstract base (BaseClass) with an interface (IBaseClass) and a...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
5
by: Chris Szabo | last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream using the XmlSerializer. A stored procedure returns the following from SQL Server: <Student StudentId="1" Status="1"...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
5
by: Jake K | last post by:
What purpose does nesting a class inside another class typically server? Are there conditions where this would be beneficial? Thanks a lot.
3
by: tonvandenheuvel | last post by:
Hi all, the following code does not compile in gcc 4.0.2: 3 class Outer 4 { 5 class B; 6 7 template<typename T>
3
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.