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对象时,由于某种原因,类字母表和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 << "\n\n\n *** CODON_alphabet constructor called. *** \n\n\n";
}
}
class alphabet
{
public:
alphabet()
{
cout << "\n\n\n *** alphabet constuctor called. *** \n\n\n";
}
}
我正在单步执行gdb中的代码,当我点击创建CODON_M0对象并进入构造函数的行时,我看到字母和CODON_alphabet构造函数被调用两次的消息。为什么呢?
答案 0 :(得分:1)
如果CODON_M0
有两个类型CODON_alphabet
的数据成员,它们将在构造函数进入之前初始化,因此构造函数被调用。