#include <iostream>
class B
{
public:
B() {
std::cout << "B" << std::endl;
}
~B() {
std::cout << "~B" << std::endl;
}
};
class D : public B
{
public:
D() {
std::cout << "D" << std::endl;
}
~D() {
std::cout << "~D" << std::endl;
}
};
void Play(B b)
{
}
int main()
{
D d;
Play(d);
return 0;
}
运行结果如下:
image.png
修改后:
#include <iostream>
class B
{
public:
B() {
std::cout << "B" << std::endl;
}
B(const B& other) {
std::cout << "c B" << std::endl;
}
~B() {
std::cout << "~B" << std::endl;
}
};
class D : public B
{
public:
D() {
std::cout << "D" << std::endl;
}
D(const D& other) {
std::cout << "c D" << std::endl;
}
~D() {
std::cout << "~D" << std::endl;
}
};
void Play(B b)
{
}
int main()
{
D d;
Play(d);
return 0;
}
运行结果如下:
image.png
已经报给visual studio:
https://developercommunity.visualstudio.com/content/problem/1010288/a-class-without-a-explicit-copy-constructor-will-c.html
网友评论