Solved

C++: can't access public member of grandfather?

Posted on 2014-12-26
9
318 Views
Last Modified: 2014-12-26
So when i derive from a derived class, the child class can access the parents public members, but the grandchild class cannot access the grandparents public members?  To illustrate:

class cA
{
public:
int x;
}
class cB:cA
{
void foo() {x++;}
}
class cC:cB
{
void fum() {x++;}
}
The function foo is ok, but the function fum gives me a "member not accessible" error.

Wow, guess after all these years I never tried this.  This is surprising since my c class seems like should be a type of my a class, just like my b class.

Could someone confirm this and possibly let me know a way to achieve the intended effect?

Thanks very much for any help.
0
Comment
Question by:RonMexico
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40518696
The problem is that you are using the default inheritance, which is 'private'., your compiler should acutlly tell you that:
cA::x' not accessible because 'cB' uses 'private' to inherit from 'cA'

Specifying 'public' solves this issue:

class cA
{
public:
int x;
};
class cB: public cA
{
void foo() {x++;}
};
class cC: public cB
{
void fum() {x++;}
};

Open in new window

0
 

Author Comment

by:RonMexico
ID: 40518705
But I have "public" before the definition of 'x', shouldny that do it??
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40518719
No, that 'publc' specifies the acceibilty from 'outside'. The inheritance specifier defaults to 'private' if you don't provide any, see http://www.cplusplus.com/doc/tutorial/inheritance/

If no access level is specified for the inheritance, the compiler assumes private for classes declared with keyword class and public for those declared with struct.

Open in new window


More on that at http://www.parashift.com/c++-faq-lite/access-rules-with-priv-inherit.html ("What are the access rules with private and protected inheritance?")
0
 
LVL 86

Expert Comment

by:jkr
ID: 40518727
BTW, just to make that clear: Declaring a class member 'oublic' affects instances (objects) of that very class, inheritance access specifiers apply to a class' hierarchy. They are completely different beasts.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:RonMexico
ID: 40518732
My read of the first article (see table halfway down) is exactly what i understood: that "public" grants access to both external and derived, "protected " grants access to derived but not external, and "private" grants access to neither (only instances of that class)

Which is very different than your last post above.  Are you talking about c++?  Can you please give me a specific example of an "inheritance access specifier"?
0
 

Author Comment

by:RonMexico
ID: 40518739
Sorry, your keywords helped me to the answer... Inheritance access specifiers are given at the class definition line.  That makes sense, trying now...
0
 
LVL 86

Expert Comment

by:jkr
ID: 40518741
Well, in that article (http://www.cplusplus.com/doc/tutorial/inheritance/), scroll down a little, specifically to the table the is labeled 'Access':


This public keyword after the colon (:) denotes the most accessible level the members inherited from the class that follows it (in this case Polygon) will have from the derived class (in this case Rectangle). Since public is the most accessible level, by specifying this keyword the derived class will inherit all the members with the same levels they had in the base class.

With protected, all public members of the base class are inherited as protected in the derived class. Conversely, if the most restricting access level is specified (private), all the base class members are inherited as private and thus cannot be accessed from the derived class.
0
 

Author Comment

by:RonMexico
ID: 40518746
Thanks very much!  Seemed to fix it...
0
 
LVL 86

Expert Comment

by:jkr
ID: 40518758
You're most welcome, happy holidays! ;o)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigDiff challenge 17 75
has77  challenge 9 68
SQL400 max size 5 54
Problem to page 4 23
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now