首页 > 解决方案 > C ++如何指向类中的私有继承类对象

问题描述

我正在学习类继承,我想知道如何创建指向另一个类私有继承的类的指针?我在下面包含了一个简单的示例。非常感谢那些帮助回答这个问题的人。

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

标签: c++classpointersinheritanceprivate-inheritance

解决方案


简单到

std::string* p = this;

由于A派生自std::string,A*可以隐式转换为std::string*(当然,该基类实际上是可访问的)。


推荐阅读