【发布时间】:2016-09-10 16:29:33
【问题描述】:
这是我的问题,我想调用我的一个对象的 getter/setter,但不是直接调用,我想通过使用 std::string 来实现。
我找到了this,但它不适用于我的情况,我认为这是因为我的函数不是在我的 main 方法中定义的,而是在我的 square 类中定义的。此外,我的函数的定义方式也不尽相同 void(std::string) std::string() void(int)...
这是 a 想要做的一个例子。
我的对象方块
#include <map>
#include <functional>
#include <string>
class Square{
private:
std::string name;
int width;
float happinessPoint; //extremly important for your square.
public:
void setName(std::string);
void setWidth(int);
void setHappinessPoint(float);
std::string getName()
int getWidth()
float getHappinnessPoint()
}
和我的主要
#include "Square.h/cpp"
int main(){
Square square = Square("Roger",2,3.5);
// here in my magicalFunction I ask to the users the new values for my square (all in std::string for now)
vector <std::string> newValueForSquare = magicalFunction();
for (unsigned int i=0; i < newValueForSquare.size(), i++){
//here I have a function which tell me if my std::string
// is in fact a float or an int
// and I would like to call each of my setters one by one to
// sets my Square to some value I asked to the user before all that.
// something like that:
// someFunction("setName","Henry")
} }
我希望我已经清楚,很难解释你不知道该怎么做的事情。如果你想让我更具体的告诉我,我会尽我所能。
编辑:我想要做的是例如用 str::string 调用我的 square.setName() 而不在我的 main 中写入这个 square.setName。
【问题讨论】:
-
你的字符串文字
"Roger'使用单引号和双引号,我很确定这不会编译。 -
@Jezor 哦,我的错,我只是按错了键。已编辑
-
您阅读了链接,发现它并不那么简单。如链接所述,除非您愿意为您的 setter 和 getter 编写伪反射设计,否则目前在 C++ 语言中是不可能的。