Solved

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

Posted on 2014-12-26
9
353 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Setting nameservers after res_init fails doing res_query 2 99
C++ question 3 65
sorting efficency of sorting algorithm 30 113
max float value 3 41
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to implement Singleton Design Pattern in Java.

803 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