473,387 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Private Inheritance is not subtyping

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
Jul 19 '05 #1
2 2213
On 7 Nov 2003 01:31:40 -0800, Ragnar <at*******@yahoo.com> 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::print 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
Jul 19 '05 #2
at*******@yahoo.com (Ragnar) wrote in message news:<9c**************************@posting.google. 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::print
rf.print(this); // why does this work ? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is Derived* so it calls the Derived::print
}
};

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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
4
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error...
1
by: Tony Johansson | last post by:
Hello! Private inheritance is sometimes called implementation inheritance. If you use this private inheritance how is with the usage of overriding then. Is overriding used less often when...
6
by: alf | last post by:
is there any way to tell the class the base class during runtime? a.
25
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
6
by: karthikbalaguru | last post by:
Hi, Could someone here tell me some links/pdfs/tutorials to know about the difference between Private Inheritance and Public Inheritance ? I am unable to get info w.r.t it. Thx in advans,...
8
by: puzzlecracker | last post by:
The statement is taken from FAQ . What about non-virtual functions? Can they be overriden? I still don't see a good justification to prefer private inheritance over composition. In fact, I have...
7
by: snewman18 | last post by:
In learning about design patterns, I've seen discussion about using inheritance when an object's relationship to another object is 'is-a' and composition when the relationship is 'has-a'. Since...
4
by: Jay Dee | last post by:
Hi all. I have bean studying the use of Interfaces, and from what I understand if you create a class that inherits from an interface, that class must poses a member that represents each member...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.