是获取和设置功能很受C ++程序员? [英] Are get and set functions popular with C++ programmers?

查看:103
本文介绍了是获取和设置功能很受C ++程序员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从C#的世界是原来和我学习​​C ++。我一直想知道关于领取并设置C ++函数。在这些C#的使用都相当受欢迎,如Visual Studio工具,使它们非常容易和快速实施推广使用。然而,这似乎并没有在C ++的世界的情况。

I'm from the world of C# originally, and I'm learning C++. I've been wondering about get and set functions in C++. In C# usage of these are quite popular, and tools like Visual Studio promote usage by making them very easy and quick to implement. However, this doesn't seem to be the case in the C++ world.

下面是C#2.0 code:

Here's the C# 2.0 code:

public class Foo
{
    private string bar;

    public string Bar
    {
        get { return bar; }
        set { bar = value; }
    }
}

或者,在C#3.0:

Or, in C# 3.0:

public class Foo { get; set; }

也许有人会说,还有什么在这一点?为什么不创建一个公共字段,然后使其属性以后,如果你需要;说实话,我确实不知道。我只是做出来的良好做法,因为我已经看到了这样做了很多次了。

May people will say, well whats the point in that? Why not just create a public field and then make it a property later if you need to; honestly, I'm actually not sure. I just do it out of good practice because I've seen it done so many times.

现在,因为我习惯了做这件事,我觉得我应该结转的习惯,我的C ++ code,但是这真的有必要吗?我不认为它做尽可能多的C#。

Now because I'm so used to doing it, I feel like I should carry over the habit to my C++ code, but is this really necessary? I don't see it done as often as with C#.

总之,这里的从我收集的C ++:

Anyway, here's the C++ from what I gather:

class Foo
{
public:
    std::string GetBar() const; // Thanks for the tip Earwicker.
    void SetBar(std::string bar);
private:
    std::string bar;
}

const std::string Foo::GetBar()
{
    return bar;
}

void Foo::SetBar(std::string bar)
{
    // Also, I always wonder if using 'this->' is good practice.
    this->bar = bar;
}

现在,对我来说,好像全世界很多跑腿的活儿;考虑使用Visual Studio的工具,C#实现会从字面上需要几秒钟来实现,以及C ++,我花了很多时间打字 - 我觉得它不是值得的努力,特别是当选择是5号线长:

Now, to me that seems like a whole lot of leg work; considering using Visual Studio's tools the C# implementation would take literally seconds to implement, and the C++ took me a lot longer to type - I feel its not worth the effort, especially when the alternative is 5 lines long:

class Foo
{
public:
    std::string Bar;
}

从我收集,这些都是优点:

From what I gather, these are the advantages:


  • 您可以更改GET实施细则,并设定功能,所以不是返回私有字段可以返回一些更有趣。

  • 您可以删除get / set方法以后,使其读/只写(但对于一个面向公众的接口,这似乎不是很好)。

和缺点:


  • 需要年龄类型,这是的真正的值得的?一般来说。在某些情况下,优点使它值得的,但我的意思是,在良好做法来说的,是吧?

  • Takes ages to type, is this really worth the effort? Generally speaking. In some cases, the advantages make it worth the effort, but I mean, speaking in terms of "good practice", is it?

为什么我选择<一个href=\"http://stackoverflow.com/questions/737409/are-get-and-set-functions-popular-with-c-programmers/737437#737437\">the用更少的票数回答?我其实非常接近,选择<一个href=\"http://stackoverflow.com/questions/737409/are-get-and-set-functions-popular-with-c-programmers/737552#737552\">veefu's回答;然而,我个人的看法(这显然是有争议的),是答案在怂恿布丁。

Why did I choose the answer with less votes? I was actually very close to choosing veefu's answer; however my personal opinion (which is apparently controversial), is that the answer over egged the pudding.

我选择的答案,在另一方面,似乎争论双方;我认为getter和setter的如果过度使用是的恶(我的意思是,当它没有必要而且会打破商业模式),但为什么我们不应该有一个名为所以getBalance()?

The answer I chose, on the other hand, seems to argue both sides; I think getters and setters are evil if used excessively (by that I mean, when it's not necessary and would break the business model), but why shouldn't we have a function called GetBalance()?

这肯定将远远小于 PrintBalance更灵活();如果我想展示给以另一种方式的用户不是作为类要我?现在,在某种意义上所以getBalance()可能没有足够的相关争辩说,getter和setter都很好,因为它没有(或者,的不应该' ŧ的)有一个附带的制定者和口语其中,被调用函数的setBalance(浮点六)可能是坏的(在我看来),因为这将意味着到该帐户必须被操纵出来的类侧功能,这是不是一件好事的实现者。

Surely this would be far more versatile than PrintBalance(); what if I wanted to show it to the user in another way than as the class wanted me to? Now, in some sense GetBalance() may not be relevant enough to argue that "getters and setters are good" because it doesn't (or maybe, shouldn't) have an accompanying setter, and speaking of which, a function called SetBalance(float f) could be bad (in my opinion) because it would imply to the implementer of the function that the account must be manipulated out side of the class, which is not a good thing.

推荐答案

我认为,提供访问位于C比C#更重要++。

I'd argue that providing accessors are more important in C++ than in C#.

C ++对性能没有内置支持。在C#中你可以改变一个公共领域的财产大多没有改变用户code。在C ++中,这是更难

C++ has no builtin support for properties. In C# you can change a public field to a property mostly without changing the user code. In C++ this is harder.

对于不太字,就可以实现琐碎的setter / getter方法​​为内联方式:

For less typing you can implement trivial setters/getters as inline methods:

class Foo
{
public:
    std::string bar() const { return _bar; } 
    void bar(const std::string& bar) { _bar = bar; } 
private:
    std::string _bar;
};

和不要忘记, getter和setter有些邪恶。

这篇关于是获取和设置功能很受C ++程序员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆