gpt4 book ai didi

c++ - 构造函数(线程)后类成员更改地址

转载 作者:行者123 更新时间:2023-11-30 03:16:21 26 4
gpt4 key购买 nike

我试图将类 A 的类成员作为常量引用存储在类 B(由 A 控制)中,这样我只能通过类 A 修改值,但 B 仍然可以读取它。问题是 A 类成员的地址在构造函数完成后发生变化,因此 B 类中的引用成员将不再指向 A 类成员。

每个对象 A 在一个单独的线程中运行。当我不使用多线程时,我不会得到不同的地址,所以我知道这是导致问题的原因,但我无法弄清楚出了什么问题。

#include <thread>
#include <vector>
#include <iostream>

using namespace std;

class B
{
private:
const float& m_foo;
public:
friend class A;
B(const float& foo):
m_foo(foo)
{
cout << "m_b.m_foo address in B constructor:\t" << &m_foo << endl;
}
};

class A {
private:
float m_foo;
B m_b;

public:
A(float foo):
m_foo(foo),
m_b(m_foo)
{
cout << "m_a.m_foo address in A constructor:\t" << &m_foo << endl;
cout << "m_b.m_foo address in A constructor:\t" << &(m_b.m_foo) << endl;
}
void run()
{
cout << "m_a.m_foo address after A and B constructor:\t" << &m_foo << endl;
cout << "m_b.m_foo address after A and B constructor:\t" << &(m_b.m_foo) << endl;
}
};

int main(int argc, char *argv[]){

int n = 1;
vector<A> vector_A;
vector<thread> threads;
vector_A.reserve(n);

for (int i=0; i<n; i++){
// Store A objects inside vector_A
vector_A.push_back(A(10));
// Store the threads inside the threads vector
threads.push_back(thread(&A::run,
std::ref(vector_A[i])));}
for (int i=0; i<n; i++){
// Wait for the rest of the thread to finish
threads[i].join();
}

return 0;
}

输出:

m_b.m_foo address in B constructor:     0x7ffee160d708
m_a.m_foo address in A constructor: 0x7ffee160d708
m_b.m_foo address in A constructor: 0x7ffee160d708
m_a.m_foo address after A and B constructor: 0x7faab8c02af0
m_b.m_foo address after A and B constructor: 0x7ffee160d708

最佳答案

你可以使用这样的东西:(它对我有用)

  • 添加初始化A对象并调用run的函数:
void newA_run(vector<A>& v, float val){
// Store A objects inside vector_A
A a(val);
v.push_back(a);

a.run();
}
  • 像这样编辑主体:
int main(int argc, char *argv[]){

int n = 1;
vector<A> vector_A;
vector<thread> threads;
vector_A.reserve(n);

for (int i=0; i<n; i++){
// Store the threads inside the threads vector
//threads.push_back(thread(&A::run, std::ref(vector_A[i])));}
threads.push_back(thread(&newA_run,
std::ref(vector_A),
10));
}
for (int i=0; i<n; i++){
// Wait for the rest of the thread to finish
threads[i].join();
}

return 0;
}

更新:

有一个更简单的方法:

  • 仅编辑 main 中的 vector_A,如下所示:
int main(int argc, char *argv[]){

int n = 1;
A* vector_A[n];// = (A*) malloc(n * sizeof(A));
vector<thread> threads;

for (int i=0; i<n; i++){
// Store A objects inside vector_A
vector_A[i] = new A(10);
// Store the threads inside the threads vector
threads.push_back(thread(&A::run,
std::ref( *vector_A[i] )));}
for (int i=0; i<n; i++){
// Wait for the rest of the thread to finish
threads[i].join();

// I put public 'm_foo' in both classes and 'm_b', to check after join
// and it works fine.
cout << "m_a.m_foo address after join:\t" << &(vector_A[i]->m_foo) << endl;
cout << "m_b.m_foo address after join:\t" << &(vector_A[i]->m_b.m_foo) << endl;
}

return 0;
}

希望对你有帮助。

关于c++ - 构造函数(线程)后类成员更改地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56416683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com