[当我拿掉()参数括弧时第二次运行良好,但是当我使用()时,它跳过了孙代的构造函数,也跳过了所有其他构造函数和析构函数。
有人可以解释一下这里发生的事情吗,我认为仍然可以使用。
#include <iostream>
class Base {
public:
int age=5;
Base() {std::cout << "Base Created" << std::endl;}
virtual ~Base() { std::cout << "Base Deleted" << std::endl; }
};
class son : public virtual Base {
public:
// int age = 1;
son(std::string name) { std::cout <<"son created" << std::endl; }
son() { std::cout << "son created - default constructor" << std::endl; }
~son() { std::cout << "son deleted" << std::endl; }
};
class sonwife : public virtual Base {
public:
// int age = 2;
sonwife() { std::cout << "son wife created - default constructor" << std::endl; }
sonwife(std::string name) { std::cout << "son wife created" << std::endl; }
~sonwife() { std::cout << "sons wife deleted" << std::endl; }
};
class grandchild : public sonwife, public son
{
public:
grandchild() { std::cout << "Grand child created" << std::endl;
std::cout << age;
}
~grandchild() { std::cout << "grand child gone!" << std::endl; }
};
int main()
{
Base *b = new son("Ryan");
delete b;
grandchild ryan; // vs. grandchild ryan();
return 0;
}
grandchild ryan();
声明了称为ryan的function,该参数不带任何参数并返回孙代。它没有声明名为ryan的变量。]>很明显,当ryan是一个函数时,没有构造函数调用。