简体   繁体   English

在同一个类中强制使用Getter / Setter(C ++)

[英]Enforce use of Getter / Setter within same class (C++)

Is there a way in C++ to enforce the use of getters or setters WITHIN the class? 在C ++中,有没有一种方法可以在类中强制使用getter或setter?

class C{
   private:
   int x;        // should only be Changed by setX();

   private:
   setX(int i){
       (...)     // enforce some complicated invariantes
       x = i;
   };

   m(){
      x = 5;     // should not not allowed
   }
}

The only thing that comes to my mind is to put all the super-private members as private variables into an abstract base class with protected getters/setters. 我唯一想到的就是将所有超级私有成员作为私有变量放入具有受保护的getter / setter的抽象基类中。

But that doesn't sound like good practice. 但这听起来不像是一种好习惯。

Are there any more common ways or conventions to ensure, that everyone uses the setters? 还有其他更通用的方式或约定来确保每个人都使用二传手吗?

(My purpose: If the class gets bigger i find it hard to remember what variables carry invariants with them. And currently before setting any variable I search, whether I created a getter or setter (to find out whether i have to consider invariants). But i want to get rid of this unprofessional searching-each-time.) (我的目的:如果类变大了,我很难记住哪些变量带有不变式。目前,在设置任何变量之前,我要搜索的是创建getter还是setter(以确定是否必须考虑不变式)。但是我想摆脱每次这种不专业的搜索。)

You can take advantage of composition like this: 您可以像这样利用组合:

class C_int
{
public:
    void setX(int i)
    {
        /* enforce invariants */
        x = i;
    }

private:
    int x;
};

class C
{
pulbic:
    void m()
    {
        x.setX(5);
        // x.x = 5; // Won't compile.
    }

private:
    C_int x;
};

But I find it odd that this is an actual problem for you, IMHO. 但是,我感到奇怪的是,这对您来说是一个实际的问题,恕我直言。 You're the writer of the C class and its members. 您是C类及其成员的作者。 You have control over how the x variable is read/written, and x is not part of the public interface of the class. 您可以控制x变量的读/写方式,并且x不属于该类的公共接口。 While it is true that other people (eg maintainers) can write code that breaks invariants in the future, unit tests should be able to cover those cases. 虽然其他人(例如维护人员)将来确实可以编写破坏不变式的代码,但是单元测试应该能够涵盖这些情况。

a) favor accessors throughout your codebase for consistency. a)支持整个代码库中的访问器以保持一致性。 then you will more easily spot direct accesses to members. 那么您将更容易发现对成员的直接访问。 if you need special accessors, then create special methods: 如果需要特殊的访问器,则创建特殊的方法:

void setFoo(const t_foo& foo) {
    assert(foo.isValid());
    this->d_foo = foo;
}

void invalidateFoo() {
    this->d_foo = t_foo::InvalidFoo();
}

b) to get to an answer: i'll often create an inner class (temporarily in some cases): b)寻求答案:我经常创建一个内部类(在某些情况下是临时的):

class C {
    class t_inner {
    public:
    /* ... */
        void setX(int arg) {
            /* enforce some complicated invariants... */
            this->x = arg;
        }
        const int& getX() const {
            /* ... */
            return this->x;
        }
    private:
        int x;
    };
public:
/* ... */
private:
/* ... */
    void m() {
        this->inner().setX(5);
    }
private:
    t_inner d_inner;
};

The clean answer is No . 答案是肯定的 There is no language feature for that; 没有语言功能; you can do some variation to achieve it, but that will clutter your code. 您可以做一些变化来实现它,但这会使您的代码混乱。

In Visual C++, __declspec( property ) can be used to have this feature: 在Visual C ++中,可以使用__declspec( property )具有此功能:

__declspec(property(put=setFunction, get=getFunction)) data-type property-name; 

See this article 看这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM