传递对象的地址会导致调用父类构造函数
Passing the address of an object causes parent class constructor to be called
main.cpp:
options my_options;
tree my_trees;
CODON_alphabet my_alphabet(1);
likelihood_engine my_likelihood(&my_options, &my_trees, &my_sites);
CODON_M0 m0(&my_trees, &my_alphabet, &my_likelihood);
类CODON_alphabet具有类字母表作为父级。当我创建CODON_M0对象时,由于某种原因,类alphabet和CODON_alphabet的构造函数分别被调用两次。这种情况发生在CODON_M0构造函数的第一行被命中之前。有人能给我指明正确的方向来弄清楚为什么会发生这种事吗?如果我遗漏了任何重要信息,请告诉我。谢谢
编辑:对不起,我以为这里的每个人都能读懂我的心思。以下是一些代码,我已经尽量减少了这些代码,但仍然调用了不需要的构造函数。
class CODON_M0: public CODON_model
{
public:
CODON_M0(tree* tree_ptr, CODON_alphabet* alpha_ptr, likelihood_engine* like_ptr)
}
class CODON_model: public model
{
public:
CODON_alphabet* my_alphabet;
CODON_model(tree* tree_ptr, CODON_alphabet* alpha_ptr, likelihood_engine* like_ptr)
}
class model
{
public:
model() {}
}
class CODON_alphabet: public alphabet
{
public:
CODON_alphabet()
{
cout << "nnn *** CODON_alphabet constructor called. *** nnn";
}
}
class alphabet
{
public:
alphabet()
{
cout << "nnn *** alphabet constuctor called. *** nnn";
}
}
我正在遍历gdb中的代码,当我点击创建CODON_M0对象的行并进入构造函数时,我看到了alphabet和CODON_alphabet构造函数已被调用两次的消息。为什么?
如果CODON_M0
有两个类型为CODON_alphabet
的数据成员,它们将在构造函数进入之前初始化,从而调用构造函数。
相关文章:
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 调用父类函数来比较父类和子类
- 如何从派生类实例调用父类重载函数
- 尝试在 C++ 中的子类中调用父类的受保护函数
- 当调用子类的析构函数时,是否也会调用父类的析构函数
- C++:如何从外部调用父类函数
- 从派生类调用父类的模板函数
- QThreadPool调用父类QRunnable的纯虚拟函数
- 传递对象的地址会导致调用父类构造函数
- 函数调用父类方法而不是子类方法
- 加载调用父类的 DLL 和子 dll
- 使用派生类调用父类的模板化函数重载
- C++继承:调用父类构造函数
- 为什么派生类对象在没有参数的情况下自动调用父类构造函数
- C++父类,调用子类中的方法,调用父类方法
- 如何调用父类类型列表的子类的方法?C++
- 委托和调用父类构造函数:如何做到这两点?
- 如何调用父类复制构造函数
- 从对象指针调用父类方法
- 调用父类的构造函数给出未定义