C++ 是使用基指针新建派生类时调用的派生类的构造函数

C++ 是使用基指针新建派生类时调用的派生类的构造函数,c++,inheritance,constructor,destructor,C++,Inheritance,Constructor,Destructor,它是在调用Circle的构造函数吗?即使它是形状构造的不同名称? 删除s时,是否调用圆的析构函数?是,将调用圆的数据析构函数: 因为Shape的dtor是虚拟的,所以从它继承的任何类都将具有虚拟dtor 这意味着dtor不是直接调用的,而是通过v表调用的:当您“删除s”时,它调用s.dtor(),它调用v表中的正确项,即Circle的dtor 是的,圆圈的dtor将被称为: 因为Shape的dtor是虚拟的,所以从它继承的任何类都将具有虚拟dtor 这意味着dtor不是直接调用的,而是通过v

它是在调用Circle的构造函数吗?即使它是形状构造的不同名称?
删除s时,是否调用圆的析构函数?

是,将调用圆的数据析构函数:

  • 因为Shape的dtor是虚拟的,所以从它继承的任何类都将具有虚拟dtor
  • 这意味着dtor不是直接调用的,而是通过v表调用的:当您“删除s”时,它调用s.dtor(),它调用v表中的正确项,即Circle的dtor

是的,圆圈的dtor将被称为:

  • 因为Shape的dtor是虚拟的,所以从它继承的任何类都将具有虚拟dtor
  • 这意味着dtor不是直接调用的,而是通过v表调用的:当您“删除s”时,它调用s.dtor(),它调用v表中的正确项,即Circle的dtor

C++中的构造函数不能是虚的。

class Shape {
public:
    virtual ~Shape() = 0;
};

Shape::~Shape() { std::cout << "Shape dtor called" << std::endl;} // Since you declared the destructor pure virtual you must define it

class Circle : public Shape {
public:
    Circle() { std::cout << "Circle ctor called." << std::endl; }
    ~Circle() { std::cout << "Circle dtor called." << std::endl; }
    // Something ...
};

int main () 
{
  Shape* s = new Circle(); // This will call the constructor of Circle
  delete s; // This will call the destructor of Circle and then the dtor Shape.
  return 0;
}
<>你所提供的代码无效C++ 以下是更正后的代码,您可以看到它在此处运行: 程序的输出应该回答您的所有问题。我建议您使用代码来回答您可能遇到的任何其他问题

Class Shape {
    virtual Shape() = 0;
    virtual ~Shape() = 0;
}

Class Circle : Public Shape {
    Circle();
    ~Circle();
    // Something ...
}

int main () {
Shape* s = new Circle();
delete s;
}
类形状{
公众:
虚~Shape()=0;
};

S::~形状({):在C++中,STD::CUT

构造函数不能是虚的。

class Shape {
public:
    virtual ~Shape() = 0;
};

Shape::~Shape() { std::cout << "Shape dtor called" << std::endl;} // Since you declared the destructor pure virtual you must define it

class Circle : public Shape {
public:
    Circle() { std::cout << "Circle ctor called." << std::endl; }
    ~Circle() { std::cout << "Circle dtor called." << std::endl; }
    // Something ...
};

int main () 
{
  Shape* s = new Circle(); // This will call the constructor of Circle
  delete s; // This will call the destructor of Circle and then the dtor Shape.
  return 0;
}
<>你所提供的代码无效C++ 以下是更正后的代码,您可以看到它在此处运行: 程序的输出应该回答您的所有问题。我建议您使用代码回答您可能遇到的任何其他问题

Class Shape {
    virtual Shape() = 0;
    virtual ~Shape() = 0;
}

Class Circle : Public Shape {
    Circle();
    ~Circle();
    // Something ...
}

int main () {
Shape* s = new Circle();
delete s;
}
类形状{
公众:
虚~Shape()=0;
};

Shape::~Shape(){std::cout构造函数不能是虚拟的

class Shape {
public:
    virtual ~Shape() = 0;
};

Shape::~Shape() { std::cout << "Shape dtor called" << std::endl;} // Since you declared the destructor pure virtual you must define it

class Circle : public Shape {
public:
    Circle() { std::cout << "Circle ctor called." << std::endl; }
    ~Circle() { std::cout << "Circle dtor called." << std::endl; }
    // Something ...
};

int main () 
{
  Shape* s = new Circle(); // This will call the constructor of Circle
  delete s; // This will call the destructor of Circle and then the dtor Shape.
  return 0;
}
是,
new Circle()
调用Circle的构造函数。这是因为表达式
new
分配足够的内存,然后通过调用适当的构造函数(或非类类型的其他初始化)对其进行初始化。由于您已显式声明了最派生的类型,因此不需要虚拟

newcircle()的结果
是指向圆的指针。将此类型指定给类型为pointer to Shape的变量涉及隐式转换。转换的结果是指向圆的基本形状子对象的指针。同样,这不是虚拟的,因为形状不是虚拟的基本对象,它只是一个常规的基本对象,并且位于Ci中rcle是静态已知的


delete s
确实调用圆析构函数。这确实使用虚拟分派。编译器知道它正在删除一个形状,而形状的析构函数是虚拟的,因此它会查找相应的析构函数,即
~Circle()
,并调用它。如果形状的析构函数不是虚拟的(并且实际上在某个地方有定义)这样就不会执行此操作,只有形状对象将被销毁,而不是圆对象,并且解除分配可能不会正确执行。行为将是未定义的。

构造函数不能是虚拟的

class Shape {
public:
    virtual ~Shape() = 0;
};

Shape::~Shape() { std::cout << "Shape dtor called" << std::endl;} // Since you declared the destructor pure virtual you must define it

class Circle : public Shape {
public:
    Circle() { std::cout << "Circle ctor called." << std::endl; }
    ~Circle() { std::cout << "Circle dtor called." << std::endl; }
    // Something ...
};

int main () 
{
  Shape* s = new Circle(); // This will call the constructor of Circle
  delete s; // This will call the destructor of Circle and then the dtor Shape.
  return 0;
}
是,
new Circle()
调用Circle的构造函数。这是因为表达式
new
分配足够的内存,然后通过调用适当的构造函数(或非类类型的其他初始化)对其进行初始化。由于您已显式声明了最派生的类型,因此不需要虚拟

newcircle()的结果
是指向圆的指针。将此类型指定给类型为pointer to Shape的变量涉及隐式转换。转换的结果是指向圆的基本形状子对象的指针。同样,这不是虚拟的,因为形状不是虚拟的基本对象,它只是一个常规的基本对象,并且位于Ci中rcle是静态已知的


delete s
确实调用圆析构函数。这确实使用虚拟分派。编译器知道它正在删除一个形状,而形状的析构函数是虚拟的,因此它会查找相应的析构函数,即
~Circle()
,并调用它。如果形状的析构函数不是虚拟的(并且实际上在某个地方有定义)这样做是不可能的,只有形状对象会被破坏,而不是圆形对象,并且不能正确地执行Debug。行为是不确定的。< / P>这是C?如果,那么你可能想把它添加为标签。这是C++吗?如果是这样,标记构造函数是不合法的。这是C?如果是这样,你可能会。想把它添加为标签。这是C++吗?如果是这样,标记构造函数是不合法的。