Class A
{
Protected:
A();
public:
void function1();
};
class B
{
public:
B();
void function2();
};
void main()
{
B b;
b.function1();
b.function2();//How can I call this function using second class object.
}
有人可以建议我这样做吗?
答案 0 :(得分:0)
我看到的唯一方法是公开A
类构造函数,然后可以通过合成使用A
对象:
class A {
public:
A() = default;
void function1() {
cout << "function 1\n";
}
};
class B {
public:
B() = default;
void function2() {
cout << "function 2\n";
}
A a;
};
int main() {
B b;
b.function2();
b.a.function1();
return 0;
}