当我遇到问题时,我正在用工会做一些实验。
union U
{
// struct flag for reverse-initialization of each byte
struct rinit_t { };
constexpr static const rinit_t rinit{};
uint32_t dword;
uint8_t byte[4];
constexpr U() noexcept : dword{} { }
constexpr U(uint32_t x) noexcept : dword{x} { }
constexpr U(uint32_t x, const rinit_t&) noexcept : dword{}
{
U temp{x};
byte[0] = temp.byte[3];
byte[1] = temp.byte[2];
byte[2] = temp.byte[1];
byte[3] = temp.byte[0];
}
};
Run Code Online (Sandbox Code Playgroud)
这是我的示例实例:
constexpr U x{0x12345678, U::rinit};
Run Code Online (Sandbox Code Playgroud)
我在 g++ 的 5.1 和 8.1 版中遇到了这个错误-std=c++14,-std=c++17 …
例子:
class Base {
public:
virtual void f() = 0;
virtual ~Base() { std::cout << "Base::~Base()\n"; }
};
class Derived : public Base {
public:
void f() { }
~Derived() { std::cout << "Derived::~Derived()\n"; }
};
int main() {
Base* p = new Derived();
delete p;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Derived::~Derived()
Base::~Base()
Run Code Online (Sandbox Code Playgroud)
我认为只会调用派生类析构函数,因为要释放的指向对象是派生类的实例。
我有两个问题:
c++ polymorphism inheritance delete-operator virtual-destructor