C++ How can you Point to a Private Inherited Class Object Within a Class

68 Views Asked by At

I am learning about class inheritance and I wanted to know how one can create a pointer to a class that was inherited privately by another class? I've included a simple example below. Many thanks to those who help in answering this question.

class A: private std::string
{
public: 
A(){}
~A(){}

void func1() 
{
// I want a pointer that points to the std::string object. Then I will use that pointer in this function
}
};
1

There are 1 best solutions below

1
On

As simple as

std::string* p = this;

Since A derives from std::string, A* is implicitly convertible to std::string* (where that base class is in fact accessible, of course).