Can I cast a derived class to a private base class, using C-style cast?

AdamIerymenko

Can I do this?

class A { ... };

class B : private A
{
    const A &foo() const
    {
        return *((const A *)this);
    }
};

Can I take a subclass that inherits privately from a base class and cast it to a public version of its base class? Can I do this without it having virtual methods?

My guess is yes, but I wanted to make sure it's safe / portable.

Bathsheba

Yes you can: §5.4/7 of the standard:

... the following static_cast and reinterpret_cast operations (optionally followed by a const_cast operation) may be performed using the cast notation of explicit type conversion, even if the base class type is not accessible:

a pointer to an object of derived class type or an lvalue of derived class type may be explicitly converted to a pointer or reference to an unambiguous base class type, respectively;

But try not to as it defeats the purpose of private inheritance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cast derived class to base

From Dev

Can derived object cast to base reference in c++ template class?

From Dev

Cast base object to derived class

From Dev

Cast object to base or derived class?

From Dev

Cast derived class type dynamically to base class type C#

From Dev

Cast derived class type dynamically to base class type C#

From Dev

Can static_cast be done from base class to derived class if derived class contains additional methods and members?

From Dev

"unregistered void cast" using boost serialiazation derived to base class

From Dev

C++: Selecting a Derived Class through a Base Using Overloading instead of Dynamic Cast

From Dev

In which case it is not safe to cast an object of base class to EMPTY derived class

From Dev

How to make a derived concrete class implicitly cast a generic base class?

From Dev

Cast array of pointers to derived class to array of pointers to base class

From Dev

reinterpret_cast vector of derived class to vector of base class

From Dev

Is it possible to access a derived class's method from a base class by using $cast in systemverilog?

From Dev

Static Cast from ( Base Reference to Base Object ) to ( Derived Class Reference)

From Dev

Can't cast inherited class field which is a derived class

From Dev

SFINAE to enable cast operator only from derived to base class

From Dev

Member function pointer cast, from Derived to Base class

From Dev

c++ design: cast from base to derived class with no extra data members

From Dev

c++ design: cast from base to derived class with no extra data members

From Dev

Using a base class without having an explict cast

From Dev

Can I make class cast in Java using methods?

From Dev

Is it better to cast a base class to derived class or create a virtual function on the base class?

From Dev

Avoid dynamic_cast with derived classes (Cast Derived class)

From Dev

How can I create an instance of a derived class from an instance of a base class and include private fields?

From Dev

How can a derived class invoke private method of base class?

From Dev

C++ public method inherited from base class can not access private member variable in derived class

From Dev

In C++ how could I cast a sub class instance from one base class pointer to another without dynamic_cast?

From Dev

C++ bad_cast exception casting *this to derived template class

Related Related

  1. 1

    Cast derived class to base

  2. 2

    Can derived object cast to base reference in c++ template class?

  3. 3

    Cast base object to derived class

  4. 4

    Cast object to base or derived class?

  5. 5

    Cast derived class type dynamically to base class type C#

  6. 6

    Cast derived class type dynamically to base class type C#

  7. 7

    Can static_cast be done from base class to derived class if derived class contains additional methods and members?

  8. 8

    "unregistered void cast" using boost serialiazation derived to base class

  9. 9

    C++: Selecting a Derived Class through a Base Using Overloading instead of Dynamic Cast

  10. 10

    In which case it is not safe to cast an object of base class to EMPTY derived class

  11. 11

    How to make a derived concrete class implicitly cast a generic base class?

  12. 12

    Cast array of pointers to derived class to array of pointers to base class

  13. 13

    reinterpret_cast vector of derived class to vector of base class

  14. 14

    Is it possible to access a derived class's method from a base class by using $cast in systemverilog?

  15. 15

    Static Cast from ( Base Reference to Base Object ) to ( Derived Class Reference)

  16. 16

    Can't cast inherited class field which is a derived class

  17. 17

    SFINAE to enable cast operator only from derived to base class

  18. 18

    Member function pointer cast, from Derived to Base class

  19. 19

    c++ design: cast from base to derived class with no extra data members

  20. 20

    c++ design: cast from base to derived class with no extra data members

  21. 21

    Using a base class without having an explict cast

  22. 22

    Can I make class cast in Java using methods?

  23. 23

    Is it better to cast a base class to derived class or create a virtual function on the base class?

  24. 24

    Avoid dynamic_cast with derived classes (Cast Derived class)

  25. 25

    How can I create an instance of a derived class from an instance of a base class and include private fields?

  26. 26

    How can a derived class invoke private method of base class?

  27. 27

    C++ public method inherited from base class can not access private member variable in derived class

  28. 28

    In C++ how could I cast a sub class instance from one base class pointer to another without dynamic_cast?

  29. 29

    C++ bad_cast exception casting *this to derived template class

HotTag

Archive