如何为Visual Studio 2008 C ++生成自动属性(get,set) [英] How to generate automatic properties (get, set) for Visual Studio 2008 C++
问题描述
阅读了有关生成吸气剂的问题和setters在Visual Studio和尝试(有点)所描述的技术,我失败凄惨地毕业超越写的Getters和Setters的longhand方式。
虽然我认识到封装(在这种情况下是一个类的私有成员)的概念上的优势,编写25个getter和setter是浪费空间和我的时间。 p>
为什么25?分开形成的膨胀因子(约2.5)我只是不知道在什么时候,我需要访问这一个变量在未来。我想我可以写一个函数,返回所有的和鱼出我需要的一个,但如果我添加更多的成员(经常做),那么函数必须在整个代码中更改。
我喜欢建议的表单这里 for VS 2008:
string sName {get;组; }
但它不会在C ++中编译。这只适用于 .NET和C#吗?
有没有一种整洁的方式来模拟这个在C + +?
在 Microsoft Compiler(非便携式)
中指定此技巧
struct person
{
std :: string m_name;
void setName(const std :: string& p_name)
{
m_name = p_name;
}
const std :: string& getName()const
{
return m_name;
}
//这是属性的名称和get,set(put)函数
__declspec(property(get = getName,put = setName))std :: string name;
};
int main()
{
person p;
p.name =Hello World!; // setName(...)
std :: cout<< p.name; // getName(...)
}
创建成员变量
加上这些成员的
,为每个 getters
和 setters
变量 getter / setter
对创建一个属性
您可以调用它任何你想要的,因为你必须指定getter和setter为这个属性。
只是为了有趣:)
#define属性\
public:const Type ##& get ## Variable()const {return Variable; }; \
void set ## variable(const Type& Variable ## _){Variable = Variable ## _;}
struct Test
{
property(int,x); property(int,y);
property(std :: string,text);
};
int main()
{
Test t;
t.setx(10);
t.sety(10);
t.settext(Hello World at:);
std :: cout<< t.gettext()<< < t.getx()<< ,< t.gety();
}
Having read this question on generating getters and setters in Visual Studio and tried (somewhat) the techniques described, I have failed miserably to graduate beyond the longhand way of writing Getters and Setters.
While I recognize the conceptual advantage of encapsulation (private members of a class in this case), writing 25 getters and setters is a waste of space and my time.
Why 25? Well apart form the exageration factor (approx 2.5) I just don't know at what point I will need access to that one variable in the future. I guess I could write a function that returns all of them and fish out the one I need, but if I add more members (often do) then the function must be changed throughout the code.
I like the form suggested here for VS 2008:
string sName { get; set; }
But it won't compile in C++. Is this just for .NET and C#?
Is there some tidy way to simulate this in C++?
Thanks @Dan for pointing out this trick in Microsoft Compiler (non-portable)
Here is the way:
struct person
{
std::string m_name;
void setName(const std::string& p_name)
{
m_name = p_name;
}
const std::string& getName() const
{
return m_name;
}
// Here is the name of the property and the get, set(put) functions
__declspec(property(get = getName, put = setName)) std::string name;
};
int main()
{
person p;
p.name = "Hello World!"; // setName(...)
std::cout << p.name; // getName(...)
}
After creating your member variables
plus the getters
and setters
of these member variables
, you create a property
for each getter/setter
pair. You can call it whatever you want, because you have to specify the getter and setter for this property.
Just for fun :)
#define property(Type, Variable) private: Type Variable; \
public: const Type##& get##Variable() const { return Variable; }; \
void set##Variable(const Type& Variable##_) { Variable = Variable##_;}
struct Test
{
property(int, x); property(int, y);
property(std::string, text);
};
int main()
{
Test t;
t.setx(10);
t.sety(10);
t.settext("Hello World at: ");
std::cout << t.gettext() << " " << t.getx() << ", " << t.gety();
}
这篇关于如何为Visual Studio 2008 C ++生成自动属性(get,set)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!