vectorjon
vectorjon

Reputation: 51

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

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
}
};

Upvotes: 0

Views: 60

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52621

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).

Upvotes: 2

Related Questions