Solved

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

Posted on 2014-12-26
9
438 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

628 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