我有這個(編譯)代碼:
#include <iostream>
#include <vector>
class Base {
std::vector<Base*> handles_;
public:
Base(Base* handle) : handles_( {handle} ) { };
};
class A : public Base {
using Base::Base;
};
class B : public Base {
using Base::Base;
};
int main()
{
A* addr_of_A = (A*)alloca(sizeof(A));
B* addr_of_B = (B*)alloca(sizeof(B));
new (addr_of_A) A(addr_of_B);
new (addr_of_B) B(addr_of_A);
}
編譯器是否處理了 A 和 B 中的向量?Afaik 要銷毀像 A 和 B 這樣分配的物件,我必須顯式呼叫解構式。我在這里不這樣做,我想知道當作用域結束時是否仍然為 A 和 B 的成員向量呼叫解構式。這當然是必要的,因為他們管理堆資源。
uj5u.com熱心網友回復:
將它們包含在一個包含這樣的結構中:
#include <iostream>
#include <vector>
class Base {
std::vector<Base*> handles_;
public:
Base(Base* handle) : handles_( {handle} ) { };
};
class A : public Base {
using Base::Base;
};
class B : public Base {
using Base::Base;
};
int main()
{
struct C {
A a;
B b;
C() : a(&b), b(&a) {}
} c;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425961.html
下一篇:對串列建構式的C 呼叫不明確
