Connecting Tech Pros Worldwide Help | Site Map

how to use private inheritance

zhangyefei.yefei@gmail.com
Guest
 
Posts: n/a
#1: Aug 22 '08
i read book <effective c++>,it tell me that public inheritance means
is-a ,and private inheritance means is-implemented-in-terms-of.
but today i am puzzled by some strange codes.

the following program can not pass compiling , bailing :
g++ d.cpp -o d
d.cpp: In function `int main()':
d.cpp:27: error: `a' is an inaccessible base of `b'

it is obviously okay to understand,because private inheritance is-
implemented-in-terms-of.

#include <iostream>
using namespace std;

class a
{
public:
virtual void doit() {cout<<"a\n";};
};

class b: private a
{
public:
void doit() {cout<<"b\n";}

};
class c
{
public:
void set(a * pa) { m_a =pa;m_a->doit();};
a * m_a;

};

int main ()
{
c cc;
cc.set(new b);
return 0;
}



but when i change source code sightly ,still with private
inheritance, everything is ok,this surprise me.

#include <iostream>
using namespace std;

class a
{
public:
virtual void doit() {cout<<"a\n";};
};

class c
{
public:
void set(a * pa) { m_a =pa;m_a->doit();};
a * m_a;

};

class b: a
{
public:
void doit() {cout<<"b\n";}
void go() { c cc;cc.set(this);};

};
int main ()
{
b bb;
bb.go();
return 0;
}

the above two program seem same to me,but the results arte complete
different.
why ? can anyone do me a favor of giving any hints ?
thanks.
guxiaozhu1@163.com
Guest
 
Posts: n/a
#2: Aug 22 '08

re: how to use private inheritance


On Aug 22, 2:59*pm, "zhangyefei.ye...@gmail.com"
<zhangyefei.ye...@gmail.comwrote:
Quote:
i read book <effective *c++>,it tell me that public inheritance means
is-a *,and *private inheritance means is-implemented-in-terms-of.
but today i am puzzled by some strange codes.
>
the following program can not pass compiling , bailing :
g++ * * d.cpp * -o d
d.cpp: In function `int main()':
d.cpp:27: error: `a' is an inaccessible base of `b'
>
it is obviously okay to understand,because *private inheritance is-
implemented-in-terms-of.
>
#include <iostream>
using namespace std;
>
class a
{
public:
* * * * virtual void doit() {cout<<"a\n";};
>
};
>
class b: private a
{
public:
* * * * void doit() {cout<<"b\n";}
>
};
>
class c
{
public:
* * * * void set(a * pa) { m_a =pa;m_a->doit();};
* * * * a * m_a;
>
};
>
int main ()
{
* * * * c cc;
* * * * cc.set(new b);
* return 0;
>
}
>
but when *i change source code sightly ,still with *private
inheritance, everything is ok,this surprise me.
>
#include <iostream>
using namespace std;
>
class a
{
public:
* * * * virtual void doit() {cout<<"a\n";};
>
};
>
class c
{
public:
* * * * void set(a * pa) { m_a =pa;m_a->doit();};
* * * * a * m_a;
>
};
>
class b: a
{
public:
* * * * void doit() {cout<<"b\n";}
* * * * void go() { *c cc;cc.set(this);};
>
};
>
int main ()
{
* * * * b bb;
* * * * bb.go();
* return 0;
>
}
>
the above two program seem *same to me,but the results arte complete
different.
why ? can anyone do me a favor of *giving *any hints ?
thanks.
For private inheritage, all public and protected members of base class
automatically become private members of derived class.

Compile code below, you'll get same result.
class A
{
public:
A()
{
}
virtual void Test2(){}
};

class B
{
public:
B()
{
}
private:
void Test2()
{
}
};
int main( )
{

A* a = new B;
a->Test2();
return 0;
}
prasadshetty@gmail.com
Guest
 
Posts: n/a
#3: Aug 22 '08

re: how to use private inheritance


On Aug 22, 11:59*am, "zhangyefei.ye...@gmail.com"
<zhangyefei.ye...@gmail.comwrote:
Quote:
i read book <effective *c++>,it tell me that public inheritance means
is-a *,and *private inheritance means is-implemented-in-terms-of.
but today i am puzzled by some strange codes.
>
the following program can not pass compiling , bailing :
g++ * * d.cpp * -o d
d.cpp: In function `int main()':
d.cpp:27: error: `a' is an inaccessible base of `b'
>
it is obviously okay to understand,because *private inheritance is-
implemented-in-terms-of.
>
#include <iostream>
using namespace std;
>
class a
{
public:
* * * * virtual void doit() {cout<<"a\n";};
>
};
>
class b: private a
{
public:
* * * * void doit() {cout<<"b\n";}
>
};
>
class c
{
public:
* * * * void set(a * pa) { m_a =pa;m_a->doit();};
* * * * a * m_a;
>
};
>
int main ()
{
* * * * c cc;
* * * * cc.set(new b);
* return 0;
>
}
>
but when *i change source code sightly ,still with *private
inheritance, everything is ok,this surprise me.
>
#include <iostream>
using namespace std;
>
class a
{
public:
* * * * virtual void doit() {cout<<"a\n";};
>
};
>
class c
{
public:
* * * * void set(a * pa) { m_a =pa;m_a->doit();};
* * * * a * m_a;
>
};
>
class b: a
{
public:
* * * * void doit() {cout<<"b\n";}
* * * * void go() { *c cc;cc.set(this);};
>
};
>
int main ()
{
* * * * b bb;
* * * * bb.go();
* return 0;
>
}
>
the above two program seem *same to me,but the results arte complete
different.
why ? can anyone do me a favor of *giving *any hints ?
thanks.

In the first program you are
1. Creating an object of type b.
2. Using type b to access function of type a via the inheritance .
Now since a has been privately inherited the compiler does not
allow you to do so.

In the second program you are
1. Creating an object of type b.
2. Calling a public member of class b (i.e. go())
3. In member go() you have created an object of type c (i.e. cc) and
calling member set() for this object of cc.
4. When the object of cc is created it already has a pointer to type a
and the compiler will copy the contents of
object b (i.e. formal argument of set()) to the actual argument
(i.e pointer to a).
Here you are not trying to access the contents of a via the
inheritance and hence the compiler is not complaining.

I hope my explanation was clear.

Regards,
Prasad
Markus Moll
Guest
 
Posts: n/a
#4: Aug 22 '08

re: how to use private inheritance


Hi

zhangyefei.yefei@gmail.com wrote:
Quote:
#include <iostream>
using namespace std;
>
class a
{
public:
virtual void doit() {cout<<"a\n";};
};
>
class b: private a
{
public:
void doit() {cout<<"b\n";}
>
};
class c
{
public:
void set(a * pa) { m_a =pa;m_a->doit();};
a * m_a;
>
};
>
int main ()
{
c cc;
cc.set(new b);
return 0;
}
>
>
>
but when i change source code sightly ,still with private
inheritance, everything is ok,this surprise me.
[...]
Quote:
class b: a
{
public:
void doit() {cout<<"b\n";}
void go() { c cc;cc.set(this);};
>
};
[...]
Quote:
the above two program seem same to me,but the results arte complete
different.
why ? can anyone do me a favor of giving any hints ?
The question is who is converting the pointer to b to a pointer to a.
In the first program, the function main is trying to do so, in the second,
it's a member function of b. Just like private members can only be accessed
by members of the same class, private base-classes can only be accessed by
members of the class. This means that any member of b is allowed to convert
a pointer to b to a pointer to a.

Markus

zhangyefei.yefei@gmail.com
Guest
 
Posts: n/a
#5: Aug 25 '08

re: how to use private inheritance


On Aug 22, 7:36*pm, Markus Moll <markus.m...@esat.kuleuven.ac.be>
wrote:
Quote:
Hi
>
>
>
zhangyefei.ye...@gmail.com wrote:
Quote:
#include <iostream>
using namespace std;
>
Quote:
class a
{
public:
* * * * virtual void doit() {cout<<"a\n";};
};
>
Quote:
class b: private a
{
public:
* * * * void doit() {cout<<"b\n";}
>
Quote:
};
class c
{
public:
* * * * void set(a * pa) { m_a =pa;m_a->doit();};
* * * * a * m_a;
>
Quote:
};
>
Quote:
int main ()
{
* * * * c cc;
* * * * cc.set(new b);
* return 0;
}
>
Quote:
but when *i change source code sightly ,still with *private
inheritance, everything is ok,this surprise me.
>
[...]
>
Quote:
class b: a
{
public:
* * * * void doit() {cout<<"b\n";}
* * * * void go() { *c cc;cc.set(this);};
>
Quote:
};
>
[...]
>
Quote:
the above two program seem *same to me,but the results arte complete
different.
why ? can anyone do me a favor of *giving *any hints ?
>
The question is who is converting the pointer to b to a pointer to a.
In the first program, the function main is trying to do so, in the second,
it's a member function of b. Just like private members can only be accessed
by members of the same class, private base-classes can only be accessed by
members of the class. This means that any member of b is allowed to convert
a pointer to b to a pointer to a.
>
Markus

it seems to be best answer resolving questions about this usage of
private inheritance .
my last question is , who is converting the pointer to b to a pointer
to a ? i thinks it is compiler doing this ,is not it ?

thanks.
Closed Thread