Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Private Inheritance is not subtyping

Reply
Thread Tools

Private Inheritance is not subtyping

 
 
Ragnar
Guest
Posts: n/a
 
      11-07-2003
Hello

If there a derived class privately inheriting from a base class, there
is no subtyping relationship between them. So a funtion that expects a
base class pointer should not accept a derived class pointer.

However, if this same function is called from within a derived class
function, passing 'this', then the function accepts it. Why is that. I
enclose some test code:

#include <iostream>

using std::cout;

class Base {
friend class FriendClass;
private:
virtual void print() const { cout << "In Base\n"; }
};

class FriendClass {
public:
void print(Base * pb) { pb->print(); }
};

class Derived : private Base { // note private
void print() const { cout << "In Derived 2\n"; }
public:
void printEx(FriendClass & rf, Derived * pd) {
rf.print(pd); // why does this work ?
rf.print(this); // why does this work ?
}
};

int main()
{
FriendClass of;
Derived od;
//Base * pb = new Derived; // will not work, base inaccessible
//of.print(&od); // does not work
od.printEx(of, &od); // works

return 0;
}

Here, if print is called from main, being passed &od, it does not
compile. If print is called from printEx, being passed this, it
compiles. Why ?

I used g++ 3.2.

Regards & Thanks

--
Ragnar
 
Reply With Quote
 
 
 
 
Grzegorz Sakrejda
Guest
Posts: n/a
 
      11-07-2003
On 7 Nov 2003 01:31:40 -0800, Ragnar <(E-Mail Removed)> wrote:

> Hello
>
> If there a derived class privately inheriting from a base class, there
> is no subtyping relationship between them. So a funtion that expects a
> base class pointer should not accept a derived class pointer.
>
> However, if this same function is called from within a derived class
> function, passing 'this', then the function accepts it. Why is that. I
> enclose some test code:
>
> #include <iostream>
>
> using std::cout;
>
> class Base {
> friend class FriendClass;
> private:
> virtual void print() const { cout << "In Base\n"; }
> };
>
> class FriendClass {
> public:
> void print(Base * pb) { pb->print(); }
> };
>
> class Derived : private Base { // note private
> void print() const { cout << "In Derived 2\n"; }
> public:
> void printEx(FriendClass & rf, Derived * pd) {
> rf.print(pd); // why does this work ?
> rf.print(this); // why does this work ?


conversion Derived* -> Base* is private in Derived, will not be inherited
but exist in Derived.
Function print in FriendClass calls print through the pointer to Base,
that is Derived:rint will be called.


> }
> };
>
> int main()
> {
> FriendClass of;
> Derived od;
> //Base * pb = new Derived; // will not work, base inaccessible
> //of.print(&od); // does not work
> od.printEx(of, &od); // works
> return 0;
> }
>
> Here, if print is called from main, being passed &od, it does not
> compile. If print is called from printEx, being passed this, it
> compiles. Why ?
>
> I used g++ 3.2.
>
> Regards & Thanks
>
> --
> Ragnar
>




--
grzegorz
 
Reply With Quote
 
 
 
 
Dan Cernat
Guest
Posts: n/a
 
      11-07-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Ragnar) wrote in message news:<(E-Mail Removed). com>...
> Hello
>
> If there a derived class privately inheriting from a base class, there
> is no subtyping relationship between them. So a funtion that expects a
> base class pointer should not accept a derived class pointer.
>
> However, if this same function is called from within a derived class
> function, passing 'this', then the function accepts it. Why is that. I
> enclose some test code:


see in code

>
> #include <iostream>
>
> using std::cout;
>
> class Base {
> friend class FriendClass;
> private:
> virtual void print() const { cout << "In Base\n"; }
> };
>
> class FriendClass {
> public:
> void print(Base * pb) { pb->print(); }
> };
>
> class Derived : private Base { // note private
> void print() const { cout << "In Derived 2\n"; }
> public:
> void printEx(FriendClass & rf, Derived * pd) {
> rf.print(pd); // why does this work ?

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pd is Derived* so it calls the Derived:rint

> rf.print(this); // why does this work ?

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is Derived* so it calls the Derived:rint

> }
> };
>
> int main()
> {
> FriendClass of;
> Derived od;
> //Base * pb = new Derived; // will not work, base inaccessible
> //of.print(&od); // does not work
> od.printEx(of, &od); // works
>
> return 0;
> }
>
> Here, if print is called from main, being passed &od, it does not
> compile. If print is called from printEx, being passed this, it
> compiles. Why ?


the output of the program is:

In Derived 2
In Derived 2


>
> I used g++ 3.2.
>
> Regards & Thanks


It does what is supposed to do. Check again your code.

/dan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subtyping iterators Michael Le Barbier Grünewald C++ 5 05-14-2012 08:23 AM
Subtyping a non-builtin type in C/C++ johan2sson@gmail.com Python 3 08-13-2006 07:23 PM
subtyping a builtin type in a C extension shawn Python 2 10-17-2005 04:43 PM
Subtyping issue valentin tihomirov VHDL 1 06-30-2005 01:42 PM
Should 'public virtual' always become 'private virtual'? & using private inheritance qazmlp C++ 19 02-04-2004 12:37 AM



Advertisments