gpt4 book ai didi

c++ - 构造函数/析构函数计数不匹配

转载 作者:太空宇宙 更新时间:2023-11-04 14:21:09 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
C++ basic constructors/vectors problem (1 constructor, 2 destructors)

我有代码:

#include <iostream>
class A
{
public:
A() { std::cout<<"A::A"<<std::endl; }
~A() { std::cout<<"A::~A"<<std::endl; }
};

class B : public A
{
public:
B(){ std::cout<<"B::B"<<std::endl; }
~B(){ std::cout<<"B::B"<<std::endl; }
};

void Func( A a ){}

int main()
{
B b;
Func(b);
}

在 VS2010EE 中输出将是:

A::A
B::B
A::~A //why twice? Once on gcc!
A::~A
B::~B
A::~A

但是,当我们有复制构造函数时,输出是:

A::A
B::B
A::A(copy)
A::~A
B::~B
A::~A

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