类成员在构造函数(线程)之后更改地址

时间:2019-06-02 16:03:11

标签: c++ class reference pthreads memory-address

我试图将类A的类成员存储为类B中的const引用(由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

1 个答案:

答案 0 :(得分:2)

您可以使用以下方式:(对我有用)

  • 添加用于初始化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;
}

更新:

有一种更简单的方法:

  • 按如下所示仅编辑vector_A中的main
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;
}

希望它对您有帮助。