我正在尝试创建一个包含三个类作为成员的结构,并且为了我的工作原理,我希望我的类的实例能够访问一个结构中的姐妹实例。因此,在初始化struct
之后,我希望我的custom_class_one
能够调用已初始化的sister custom_class_two
方法。它甚至可以做到吗?这是我的头文件:
struct custom_wrapper
{
public:
custom_wrapper();
class custom_class_one
{
private:
int number1;
public:
int getNumber1();
};
class custom_class_two
{
private:
int number2;
public:
int getNumber2();
};
class custom_class_three
{
private:
int number3;
public:
int getNumber3();
};
};
答案 0 :(得分:0)
只要您的方法可以访问且类可以相互看到(如果需要,可以向前声明),就可以完成:
struct custom_wrapper
{
public:
custom_wrapper();
class custom_class_two; // Forward declaration for inner class
class custom_class_one
{
private:
int number1;
public:
int getNumber1();
int accessSisterMethods(custom_class_two& obj) {
return obj.getNumber2();
}
};
class custom_class_two
{
private:
int number2;
public:
int getNumber2() {
return 42;
}
};
class custom_class_three
{
private:
int number3;
public:
int getNumber3();
};
};
int main()
{
custom_wrapper::custom_class_one obj1;
custom_wrapper::custom_class_two obj2; // Need a sister object
std::cout << obj1.accessSisterMethods(obj2);
}
当然你需要另一个姐妹类的实例(除非你正在调用静态方法,但我正在考虑你发布的代码)。
答案 1 :(得分:0)
根据您的评论,我认为您在这里混淆了一些概念:组合,嵌套类,实例。或者我误解了你的问题并删除/编辑了我的答案。
当你说“一个结构中的姐妹的实例”时,我认为你真的在寻找作文。与 has-a 关系一样。您不需要嵌套类来组合。对于组合,您需要成员变量。
也许你真正想要的是这样的,如果我错了,请纠正我:
struct CustomClassA {
private:
int number;
public:
int getNumber() { return number; }
};
struct CustomClassB {
private:
int number;
public:
int getNumber() { return number; }
};
struct CustomClassC {
private:
int number;
public:
int getNumber() { return number; }
};
struct CustomWrapper {
private:
CustomClassA a;
CustomClassB b;
CustomClassC c;
};
因此,如果您创建CustomWrapper
的实例,则具有自定义类的实例a
,b
和c
。它“拥有”它们。
然后您希望这三个自定义类能够相互通信。有很多方法可以实现这一目标。自定义类可以指向其他姐妹类。这些可以在CustomWrapper
:
#include <iostream>
struct CustomClassB; // Forward declaration for member pointer in CustomClassA.
struct CustomClassA {
private:
int number;
CustomClassB* b; // Member pointer to CustomClassB.
public:
CustomClassA() : number(1), b(nullptr) {} // Initialized sister b to nullptr.
void setSisterB(CustomClassB *sister_b) { b = sister_b; }
int getNumber() { return number; }
};
struct CustomClassB {
private:
int number;
CustomClassA* a;
public:
CustomClassB() : number(2), a(nullptr) {}
void setSisterA(CustomClassA *sister_a) { a = sister_a; }
int getNumber() {
if (a) // Check a is not null
return a->getNumber() + number; // Use pointer to sister a
return number;
}
};
struct CustomClassC {
private:
int number;
CustomClassA* a;
CustomClassB* b;
public:
CustomClassC() : number(4), a(nullptr), b(nullptr) {}
void setSisterA(CustomClassA* sister_a) { a = sister_a; }
void setSisterB(CustomClassB* sister_b) { b = sister_b; }
int getNumber() { return number; }
};
struct CustomWrapper {
private:
CustomClassA a;
CustomClassB b;
CustomClassC c;
public:
CustomWrapper() {
a.setSisterB(&b); // Set the pointers to the sister custom
b.setSisterA(&a); // classes in each custom class in the
c.setSisterA(&a); // constructor of CustomWrapper.
c.setSisterB(&b);
}
int getAnswer() {
return b.getNumber() + c.getNumber();
}
};
int main() {
CustomWrapper wrapper;
std::cout << wrapper.getAnswer();
}
或者自定义类可以有一个指向自定义包装器的指针,并通过包装器与他们的姐妹进行通信。