0

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
}
};
vectorjon
  • 31
  • 6
  • 1
    Must read: [Don't inherit from std types](https://quuxplusone.github.io/blog/2018/12/11/dont-inherit-from-std-types/). You're getting ready to shoot yourself in the foot. – spectras Dec 19 '20 at 14:06
  • There is no such thing as a "pointer to a class" – Eric Dec 19 '20 at 14:10

1 Answers1

2

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

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85