c++ - 为什么当我包含“参数”时默认构造函数会被调用两次?
问题描述
我有点困惑。当我为默认构造函数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.
解决方案
推荐阅读
- ios - 将图像 URL 上传到 Firestore
- odoo - 您是否找到了一种将 odoo 核心模块以不同形式保存在 v14 中的方法?那么,可以在 gcloud run 中部署 odoo 吗?
- angular - 如何在 Angular 中使用 Jasmine Karma 对 MatSnackbar 进行单元测试
- python - 快速排序python实现
- jquery - 使用选择在数据表中创建个性化过滤器
- python - 使用重复的第二个标题多次转换 DB 中的 *.csv 文件
- python - Python 语音识别在 Manjaro Linux 中不起作用
- c# - .ParseExact 无法根据格式数组格式化字符串
- google-bigquery - 使用 google 工作流执行 BigQuery 以获取表的最后修改。在工作流程中得到错误的结果,但在 BIGQUERY UI 中同样可以正常工作
- javascript - 如何获得自动归档的输入值?