473,320 Members | 1,846 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,320 software developers and data experts.

Private base class conversion from base?

Can a private Base class method convert to/from a Derived* using
static_cast? The CPL book says that a Derived method is allowed to
convert to/from Base, but says nothing about whether a Base method may
do the same thing.

My embedded compiler and g++ disagree on whether this is legal.
Example:

class Base { public: void foo(); };
class Derived : private Base { public: void foo(); };

void Base::foo() {
static_cast<Base*>((Derived*)0); // ok in g++, but legal?
static_cast<Derived*>((Base*)0); // ok in g++, but legal?
}
void Derived::foo() {
static_cast<Base*>((Derived*)0); // legal
static_cast<Derived*>((Base*)0); // legal
}

int main()
{
static_cast<Base*>((Derived*)0); // illegal
static_cast<Derived*>((Base*)0); // illegal
return 0;
}
Sep 11 '08 #1
3 2116
I'm sorry; in my tests at home I used PROTECTED inheritance, and in my
post I used PRIVATE. If you change to protected inheritance, then the
"ok with g++" does compile (at least on 4.0.1 and 4.1.2 I tried)
However, my embedded compiler complains for both protected & private
inheritance.

Which way is correct, or is it one of those "undefined" areas?

Jared

On Sep 11, 9:02*am, Zeppe
<ze...@remove.all.this.long.comment.yahoo.itwrot e:
For the last statement, since Base is a private base of Derived, a
public member in Base is private in Derived, and a private member in
Derived can be accessed only from Derived (and friends). So, the class
Base is accessible only from Derived (and friends), that is the only
place in which such a cast is correct.

BTW, after writing this I tried your code with g++ (3.4, 4.0, 4.1, 4.2)
and it raises error when you say "ok with g++". So I don't know why with
you it worked.
Sep 12 '08 #2
JaredGrubb wrote:
I'm sorry; in my tests at home I used PROTECTED inheritance, and in my
post I used PRIVATE. If you change to protected inheritance, then the
"ok with g++" does compile (at least on 4.0.1 and 4.1.2 I tried)
However, my embedded compiler complains for both protected & private
inheritance.

Which way is correct, or is it one of those "undefined" areas?

If you consider protected inheritance, the thing works like that: all
the public and protected members of B are protected in D. Protected
members in D can be accessed by D and derived classes. B is not derived
from D, so again B is not a visible base of D in a B method and g++
should be wrong in this case.

Best wishes,

Zeppe
Sep 12 '08 #3
JaredGrubb wrote:
I'm sorry; in my tests at home I used PROTECTED inheritance, and in my
post I used PRIVATE. If you change to protected inheritance, then the
"ok with g++" does compile (at least on 4.0.1 and 4.1.2 I tried)
However, my embedded compiler complains for both protected & private
inheritance.

Which way is correct, or is it one of those "undefined" areas?

If you consider protected inheritance, the thing works like that: all
the public and protected members of B are protected in D. Protected
members in D can be accessed by D and derived classes. B is not derived
from D, so again B is not a visible base of D in a B method and g++
should be wrong in this case.

Best wishes,

Zeppe
Sep 12 '08 #4

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

Similar topics

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...
2
by: Ragnar | last post by:
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...
8
by: Dave | last post by:
Hello all, Suppose that derived inherits privately from base. A base pointer may not be made to point at a derived object in this case. I understand that is exactly what is supposed to happen...
3
by: seesaw | last post by:
Compared to "public", how does "private/protected" in inheritance change relationship between classes what is the purpose to define constructor as "private/protected"? is there any usage to...
2
by: MJ | last post by:
Hi I have a following sample code class base and class derived. I have inherited the base class as private and tried to compile the code its giving an error "conversion from 'class derived *' to...
4
by: Bangalore | last post by:
Hi, I am not understanding the problem in the below program, #include <iostrema.h> class Base { public: virtual void display() { cout <<"Base \n";
6
by: Jonathan Potter | last post by:
Hi, I was wondering if someone could explain something to me. We recently upgraded to a new C++ compiler and found some old code wouldn't compile. Boiled down, the code that causes the error is:...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
7
by: Fraser Ross | last post by:
template<typename T, typename CounterPolicy = SimpleReferenceCount, typename ObjectPolicy = StandardObjectPolicy> class CountingPtr : private CounterPolicy, private ObjectPolicy { private: //...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.