如果我想编写自己的test.cpp来检查另一个.cpp文件是否正在输出我希望它输出的方式,那么无论如何都没有明确打印它吗?
换句话说,有什么比如
assert(output_of_file_being_tested, "this is the correct output");
Run Code Online (Sandbox Code Playgroud)
其中output_of_file_being_tested应该是"cout"ed.
假设我有两个派生自同一基类的类.我想根据命令行输入实例化一个新的类对象.我能做到这一点:
#include <iostream>
#include "DerivedClass1.h"
#include "DerivedClass2.h"
using namespace std;
int main(int argc, char *argv[])
{
if (argv[1] == "DERIVED CLASS 1") {
DerivedClass1 *myClass = new DerivedClass1(argv[2]);
myClass->doSomething();
} else if (argv[1] == "DERIVED CLASS 2") {
DerivedClass2 *myClass = new DerivedClass2(argv[2]);
myClass->doSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更优雅的方式来做到这一点.我正在考虑创建一个抽象类工厂,或硬编码将字符串名称映射到类实例.几个限制
1)我的基类是抽象的 - 它包含纯虚函数
2)我只能调用参数化构造函数