我的问题是我想要创建升级类的多个实例,用于不同的升级。也许是因为我习惯使用Java,所以我不能只输入
Source.cpp:我想要这个类的多个实例。
Source first("first"), second("second");
,因为如果我这样做并调用 first.getName()
,我会得到"second"
。我创建了一个示例文件,只写了我遇到困难的部分,这样你就不必试图理解我的混乱代码了。Source.cpp:我想要这个类的多个实例。
#include "Source.h"
std::string name;
Source::Source()
{
}
Source::Source(std::string nameToSet)
{
name = nameToSet;
}
std::string Source::getName()
{
return name;
Source.h
#pragma once
#include <string>
class Source {
public:
Source();
Source(std::string namel);
std::string getName();
};
Test.cpp
#include "Source.h"
#include "iostream"
Source first("first"), second("second");
int main()
{
std::cout << first.getName() << std::endl;
}
输出: second
Test.h
#pragma once
#include <string>
first("first")
或second("second")
做什么? - Code-Apprentice