简体   繁体   English

C ++中的构造方法,继承性,堆栈,堆,此指针和segfaults

[英]constructors,inheritance,stack,heap,this-pointer and segfaults in c++

I'm struggled over some code where I don't know how to name it and how to solve it. 我在一些我不知道如何命名和如何解决它的代码上苦苦挣扎。 I tried to reduce the code to the following example (so the example itself won't make sense, but it shows the problematic): 我试图将代码简化为以下示例(因此该示例本身没有意义,但它显示了问题):

 struct MyInterface {
    virtual ~MyInterface() {

    };
    virtual void Output() = 0;
};

class A {
public:
    MyInterface *myInterface;
    A(MyInterface *myInterface) {
        std::cout << "this in A constructor: " << this << std::endl;
        this->myInterface = myInterface;
    }
    void CallA() {
        this->myInterface->Output();
    }
};

class B : public MyInterface, A {
public:
    int v;
    B(int v) : A(this) {
        std::cout << "this in B constructor: " << this << std::endl;
        this->v = v;
    }
    virtual void Output() override {
        std::cout << "Whatever" << std::endl;
    }
    void CallB() {
        std::cout << "this in CallB: " << this << std::endl;
        this->CallA();
    }
};

class Foo {
public:
    B b;
    Foo() : b(42) {
        b = B(41);  //This will make an "invalid" B:
                    //generates B on the Stack but assign the bytes to Foo.b (which is on the the heap)
                    //so b.myInterface will point to the stack
                    //after leaving this context b.other will be invalid
    }
    void Exec() {
        b.CallB();
    }
};
int main(int argc, char **args) {
    Foo *foo = new Foo();
    foo->Exec();    //Gives a segfault, because foo->b.myInterface is not valid
    return 0;
}

First I thought it has something to do with the inheritance and its virtual methods. 首先,我认为它与继承及其虚拟方法有关。 But I think the main problematic is the this pointer within the constructors. 但是我认为主要的问题是构造函数中的this指针。

So my questions: When b is constructed, the this pointer in the constructors points to the stack. 所以我的问题是:构造b时,构造函数中的this指针指向堆栈。 Why doesn't show the this pointer to the target memory (in the heap)? 为什么不显示指向目标内存(在堆中)的this指针? No copy constructor is called - Why? 没有复制构造函数被调用-为什么? How can I Name this problem? 我该如何命名这个问题?

The copy constructor isn't called because you aren't creating a new object you are assigning to an existing object. 之所以不会调用复制构造函数,是因为您没有创建要分配给现有对象的新对象。 This calls the assignment operator. 这将调用赋值运算符。

This is copy construction: 这是复制构造:

B b1(42); // construction
B b2(b1); // copy construction
B b3 = b1; // looks like assignment but is actually copy construction

This is assignment: 这是作业:

B b1(42); // construction
b1 = B(43); // b1 already exists we can't copy construct, construct a new object and assign to b1

You need to override the assignment operator : 您需要覆盖赋值运算符

class B
{
   B& operator=(const B& other)
   {
      // fix references to this here
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM