访问使用接口实例化的类的私有成员
Access private members of a class instantiated using interface
我有一个派生自接口的类和一个派生类的友元类。我想访问实例化为接口的派生类的成员。它看起来像这样:
接口:
class AInterface
{
public:
virtual ~AInterface() = default;
virtual void set(int a) = 0;
};
派生类 A 与友元类 B:
class B;
class A : public AInterface
{
public:
~A() override {}
void set(int a) override
{
mem = a;
}
private:
friend class B;
int mem = 0;
};
B类:
class B
{
public:
B()
{
a = new A();
a->set(3);
}
int get_a()
{
// Access mem since it's a friend class
return a->mem;
}
private:
AInterface *a;
}
主要:
int main()
{
B *b = new B();
std::cout << b->get_a() << std::endl;
return 0;
}
该程序没有编译说AInterface没有名为"mem"的成员。 我是否需要在接口中使用 getter 函数并在 A 中实现它来实现这一点,或者还有其他方法可以做到这一点吗?
现在工作
#include <iostream>
using namespace std;
class AInterface
{
public:
virtual ~AInterface() = default;
int getMem() { return mem; }
virtual void set(int a) = 0;
protected:
int mem = 0;
};
class A : public AInterface
{
public:
~A() override {}
void set(int a) override
{
mem = a;
}
};
class B
{
public:
B()
{
a = new A{};
a->set(3);
}
int get_a()
{
// Access mem since it's a friend class
return a->getMem();
}
private:
AInterface *a;
};
int main()
{
B *b = new B();
std::cout << b->get_a() << std::endl;
return 0;
}
- 由子项覆盖且纯的方法应该是虚拟的。
- 如果每个类(子)
Interface
,变量int mem
应该在接口中受到保护。 现在工作正常,就像你想要的那样。 - 添加吸气剂
getMem()
带friend
的版本
#include <iostream>
using namespace std;
class AInterface
{
public:
virtual ~AInterface() = default;
virtual void set(int a) = 0;
protected:
friend class B;
int mem = 0;
};
class A : public AInterface
{
public:
~A() override {}
void set(int a) override
{
mem = a;
}
};
class B
{
public:
B()
{
a = new A{};
a->set(3);
}
int get_a()
{
// Access mem since it's a friend class
return a->mem;
}
private:
AInterface *a;
};
int main()
{
B *b = new B();
std::cout << b->get_a() << std::endl;
return 0;
}
在你的B类中。
class B
{
//...
int get_a()
{
return a->mem; // but a is a AInterface* !!!
}
private:
AInterface *a; // That's not an A*, but an AInterface*
};
您有 2 个选项。
使用
dynamic_cast<>
int B::get_a() { A* p = dynamic_cast<A*>(a); if (p) return p->mem; // a is not an A*, it's another AInterface*-type object !!! // What should you do? throw something_or_other(); // throw? return -1; // return an error code? } // or maybe add.. so you can check for errors before calling get_a() A* B::get_A_ptr() const { return dynamic_cast<A*>(a); }
dynamic_cast工作正常,但如果您需要频繁读取a->mem
,可能会降低您的应用程序速度。
将
a
存储在A*
中,这可能是您从一开始就打算做的事情...class B { // ... private: A* a; // now a->A::mem is visible. };
由于您在 B 的构造函数中显式调用new A
,我认为选项 2 更适合您的情况。
相关文章:
- 静态数据成员模板专用化的实例化点在哪里
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 受约束的成员函数和显式模板实例化
- 实例化多种类型的成员函数模板
- 为什么在使用指针时不采用类成员的默认值,而不是直接实例化对象时?
- 将类成员函数的模板定义放在 CPP 文件中C++隐式实例化而不是 .H 允许吗?
- 如何实例化类的公共成员并将其作为 std::p romise 返回?
- 在实例化封闭类模板之后,我们可以声明模板类成员的部分专用化吗
- 使用 SFINAE 有选择地实例化模板的成员函数
- 静态模板成员函数的实例化?
- 访问使用接口实例化的类的私有成员
- Google Mock:在目标类的构造函数中实例化的模拟私有变量成员
- 参考数据成员到模板的实例化
- 类的私有成员在我的类实例化期间更改,即使他们不应该
- 如果未实例化成员模板,是否要评估static_asserts?
- 实例化与unique_ptr的类集合成员
- 实例化成员模板函数时的Buggy(?)编译器行为
- 使用向量<向量>成员实例化对象时的隔离错误<int>
- 静态成员与静态常量成员实例化 - 有什么区别?
- 显式类成员实例化