传递对象的地址会导致调用父类构造函数

Passing the address of an object causes parent class constructor to be called

本文关键字:调用 父类 构造函数 对象 地址      更新时间:2023-10-16

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的数据成员,它们将在构造函数进入之前初始化,从而调用构造函数。