我有一个程序,它只有class A
类的一个实例和class B
类的多个实例,其中每个class B
实例都有一个指向class A
实例的指针。我想我可以在main()
中初始化它们,然后将class A
的单个实例的地址传递给class B
的所有实例。我想知道这是否正确,我一直在寻找继承,但根据我的理解(这通常是错误的),如果你继承了另一个类,那么这个类每次都会被初始化,所以创建了一个多对多的关系,而我想要一个一对多的关系。我附上了一些代码。任何建议都将不胜感激。
// C/C++ standard library
#include <vector>
#include <iostream>
#include <cstdlib>
using namespace std;
class A {
public:
double get_value(void) {
return value;
}
private:
double value;
};
// Forward declare A if split over files
class B {
public:
void assign_pointer(A class_a_to_assign) {
class_a = &class_a_to_assign; // assign the pointer the address to point to
}
void update_my_value(void) {
value_b += class_a->get_value();
}
double get_value(void) {
return value_b;
}
private:
double value_b = 0.1;
A* class_a; // pointer to class A
};
int main() {
cout << "hello world" << endl;
// create 2 instances of B there could be thousands of these tho.
B b1;
B b2;
// create 1 instance of A
A a1;
// Now I want both instances of the B class to point to the one instance of A
b1.assign_pointer(a1);
b2.assign_pointer(a1);
// THen do stuff with B so that if any changes occur in A, then they can be automatically updated in class B through the pointer
b1.update_my_value();
b2.update_my_value();
cout << b1.get_value() << " and " << b2.get_value() << endl;
return 0;
}
发布于 2018-05-28 13:26:13
首先,你的代码中有一个严重的问题:
void assign_pointer(A class_a_to_assign) {
class_a = &class_a_to_assign; // assign the pointer the address to point to
}
在这里,class_a_to_assign
是一个按值计算的函数参数,就生存期而言,它与任何函数局部变量大致相同。换句话说,一旦控制离开了方法的作用域,class_a
就变成了一个悬空指针(一个指向不再存在的本地对象的指针)。快速修复非常简单明了:
void assign_pointer(A &class_a_to_assign) {
class_a = &class_a_to_assign; // assign the pointer the address to point to
}
不同之处在于只有一个字符-函数参数声明中的&号将其从一个临时值转换为对一个更长寿的对象的引用。
接下来,如果你只有一个class A
对象,你有没有考虑过让它成为一个单例?这样,B
的实例甚至不需要保留该指针,A
自己管理该实例。有很多关于设计单例类的说法,一个粗糙而幼稚的实现是这样的:
class A {
A(); // note it's private
public:
int getValue() const;
static A &instance() {
static A retVal;
return A;
};
};
class B {
public:
void update_my_value(void) {
value_b += A::instance().get_value();
}
};
int main() {
A::instance(); // to warmup the static instance before any users are created
B b1; // no assign_pointer is needed as A is a Singleton
B b2; // and every B always knows where to find it
}
发布于 2018-05-28 13:23:59
您所描述的内容听起来很像singleton。处理这种情况的一种常用方法是让单例类包含一个static
方法,该方法返回类的单个实例。所以它看起来像这样:
class A {
public:
A() { /* constructor */ }
~A() { /* destructor */ }
static A* getInstance();
double get_value(void) {
return value;
}
private:
double value;
};
static
方法将位于实现(.cpp)文件中,如下所示:
static A gSharedInstance;
static A* A::getInstance()
{
return &gSharedInstance;
}
这将在静态初始化时构造一个A
,当调用上面的方法时,它将返回一个指向该静态共享实例的指针。现在,无论您想在何处使用A
,您都可以简单地执行以下操作:
A* sharedA = A::getInstance();
double value = sharedA->getValue(); // Or whatever you need from it.
请注意,单例本质上是一个全局变量,并且全局变量有一些问题。(例如,在本例中,它的value
成员集在哪里?)但是如果这个类代表一些真正独特的资源(例如,设备上唯一的麦克风),那么用一个单例来代表它是值得的。在这种情况下,最好将其设置为只读,这样就不会在许多不同的地方设置它的状态。此外,请注意,您可能需要添加代码来处理同时访问它的多个线程。
https://stackoverflow.com/questions/50559328
复制相似问题