get 和 set 函数是否受 C++ 程序员欢迎? [英] Are get and set functions popular with C++ programmers?

查看:23
本文介绍了get 和 set 函数是否受 C++ 程序员欢迎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初来自 C# 的世界,我正在学习 C++.我一直想知道 C++ 中的 get 和 set 函数.这些在 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 代码:

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++ 代码中,但这真的有必要吗?我认为它不像 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, @Daniel Earwicker.
    void SetBar(std::string bar);
private:
    std::string bar;
}

std::string Foo::GetBar() const
{
    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 和 set 函数的实现细节,因此您可以返回更有趣的内容,而不是返回私有字段.
  • 您可以稍后删除 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?

我为什么选择答案是少选票?我实际上非常接近选择veefu的答案;然而,我个人的意见(这显然是有争议的)是,答案超过了布丁.

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 是好的".因为它没有(或者也许,不应该)有一个伴随的 setter,说到这里,一个叫做 SetBalance(float f) 的函数可能是坏的(在我的意见)因为这会暗示函数的实现者必须在类之外操作帐户,这不是一件好事.

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# 中,您可以将公共字段更改为属性,而无需更改用户代码.在 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:
    const std::string& bar() const { return _bar; } 
    void bar(const std::string& bar) { _bar = bar; } 
private:
    std::string _bar;
};

不要忘记 getter 和 setter 有点邪恶.

这篇关于get 和 set 函数是否受 C++ 程序员欢迎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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