Solved

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

Posted on 2014-12-26
9
411 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
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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Header of docx file 17 145
Microsoft C++ code failing in executable that worked 9 144
Beginner to Unreal Engine 4 5 120
c++, dynamic object by json 1 65
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

751 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