那么公共变量有什么好处呢? [英] What good are public variables then?

查看:82
本文介绍了那么公共变量有什么好处呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个全新的,有吨的?在我心中,很多使用C ++的经验!有一些我觉得很混乱,它是使用公共变量,我已经看到了大量的代码,如下:

  class Foo {

private:
int m_somePrivateVar;

public:
void setThatPrivateVar(int const& new_val){
m_somePrivateVar = new_val;
}

int getThatPrivateVar(void)const {
return m_somePrivateVar;
}

};

为什么有人隐藏这个变量,实现访问器和mutator,当他们没有做什么比分配新值只是(没有范围检查等)或返回值,而不只是它是?
我已经听说过一些原因,并且其中一些在一些情况下是令人信服的,但是想象一下,用这样一种方式实现一个巨大的类,有很多变量,不需要任何检查和东西!
让我这样问你,你什么时候使用公共变量?你使用它吗?



提前感谢。

解决方案

现在,通过隐藏变量和添加方法,类设计器允许在将来不插入任何代码



另外请注意,提供大量的访问器/ mutator方法通常是一个标志,你的类设计需要再次寻找可能的改进。类方法应该实现实际的逻辑,而不只是提供对每个成员的访问。



我只使用 struct 形成。例如,我可能有一个表示字符串 - >值映射的数据库表,其中value是一个复合数据结构。我只是写一个结构并使用例如 std :: map< std :: string,MyStruct> 来表示数据库表。我不需要真正做数据的工作,只需要查看它,并在需要时使用它。



正如几个注释中所指出的,即使 struct 也可以经常受益于方法的司法使用,例如一些常见的构造函数,以保持成员被简单地初始化,清楚的函数来重用结构等。 / p>

I'm a total newbie with tons of ?'s in my mind and a lot to experience with C++ yet! There's been something which I find really confusing and it's the use of public variables, I've seen tons of code like this:

class Foo {

private:
    int m_somePrivateVar;

public:
    void setThatPrivateVar (int const & new_val) {
        m_somePrivateVar = new_val;
    }

    int getThatPrivateVar (void) const {
        return m_somePrivateVar;
    }

};

Why would anyone hide that variable and implement accessors and mutators when there's nothing done in them more than assigning the new value just as it got received (no range checking etc.) or returning the value without just as it is? Well I've heard some reasons and some of them are convincing in some cases, but imagine implementing a huge class in such a manner for with a lot of variables which do not need any checking and stuff! Let me ask you this way, When do you use public variables? Do you use that at all?

Thanks in advance.

解决方案

By hiding the variable and adding methods now, the class designer allows for inserting arbitrary code into those methods in the future without breaking tons of code that use the attributes directly.

Also note that providing a lot of accessor/mutator methods is generally a sign that your class design needs another look for possible improvement. Class methods should implement actual logic, not just provide access to each member.

I use public variables only in struct form. For example, I might have a database table that represents a string->value mapping, where value is a composite data structure. I'd just write a structure and use for example std::map<std::string, MyStruct> to represent the database table. I don't need to actually do work on the data, merely be able to look it up and make use of it when required.

As noted in a couple comments, even structs can often benefit from judicial use of methods, for example a couple of common constructors to keep the members sanely initialized, a clear function to reuse the structure, etc.

这篇关于那么公共变量有什么好处呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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