首页 > 解决方案 > 为什么当我包含“参数”时默认构造函数会被调用两次?

问题描述

我有点困惑。当我为默认构造函数baseClass(JACOB) 添加参数时,我的程序输出添加了两个对象:JACOB 和 ANONYMOUS。

如果我省略了“JACOB”,我只会得到预期的“ANONYMOUS”。我的问题是,为什么当我通过“JACOB”时,我的程序中仍然得到“ANONYMOUS”对象?

(main.cpp)

//*************************************************************************
#include <iostream>     //inclusion of support for doing input & output
#include <string>       //inclusion of support of the string class
#include <vector>       //inclusion of support of the vector class
#include <fstream>       //inclusion of support of file I/O
using namespace std;    //declare access to standard stuff like cin, cout


#include "baseClass.h" 
#include "childClassA.h" 
#include "childClassB.h" 



void test()
{     

    //*************************************************************************
    //Observe the implicit calls to constructors of the base and the child classes
    //*************************************************************************
      baseClass jacob("JACOB"); // <-- Question about this one

}   

int  main()
{
    test();
    return 0;
}

基类.cpp

//*************************************************************************
//  Base class implementation
//*************************************************************************
#include "baseClass.h"

int baseClass::count = 0;

baseClass::baseClass(string name)
{
      baseName = name;
      ++count;
      cout  << endl
                  << baseName
                  << ", a new baseClass object is CONSTRUCTED."
                  << endl << "Now "
                  << count << " baseClass objects in total."
                  << endl << endl;
}

baseClass::~baseClass()
{
      --count;
      cout  << endl
                  << baseName
                  << ", a baseClass object is DESTRUCTED."
                  << endl << "Now "
                  << count << " baseClass class objects in total."
                  << endl << endl;

}

void baseClass::output()
{
      cout << endl << "Hello! I am " << baseName << ", a baseClass object " << endl;
}

基类.h

//*************************************************************************
//  Base class declaration
//*************************************************************************
#ifndef BASE_CLASS_H

#define BASE_CLASS_H

using namespace std;
#include <string>       //inclusion of support of the string class
#include <iostream>
#include <fstream>

class baseClass
{     public:
            baseClass(string name = "ANONYMOUS");

            virtual ~baseClass();   //What is the effect of virtual??
            //~baseClass();               //What happens if no virtual??

            virtual void output();  //What is the effect of virtual??
            //void output();        //What happens if no virtual??

            static int count;

      private:
            string baseName;
};

#endif // Matching the starting #ifndef BASE_CLASS_H

输出:

JACOB, a new baseClass object is CONSTRUCTED.
Now 1 baseClass objects in total.


ANONYMOUS, a new baseClass object is CONSTRUCTED.
Now 2 baseClass objects in total.

标签: c++inheritanceconstructordestructordefault-constructor

解决方案


推荐阅读