禁止在类中使用方法

Forbid use of method within class

本文关键字:使用方法 禁止      更新时间:2023-10-16

我有一个类,它有访问器方法,这些方法旨在用于在类外部查看其某些私有数据。

但是,类的内部方法不应使用这些公共访问器

,因为类的内部状态由其其他方法更改,并且在计算完成之前,这些更改不会写入公共访问器查看的位置。

是否有编译时方法可以防止类调用其自己的一个或多个成员?或者,使用 assert 进行运行时检查是可以的,因为它可以很容易地在发布编译中消除。

虽然我在实践中从未见过这种模式,但您可以将实现和接口分开(尽管通常是相反的方式)

namespace detail {
    class Impl {
    protected:
        Impl() : x_(0) {}
        int x_;
        void internal() { x_ = 1; }
        void something_else() { x_ = 2; }
    };
}
class Interface : public detail::Impl {
public:
    int x() const { return x_; }
    void x(int x) { x_ = x; }
};

或者更进一步,无需派生:

class Interface;
namespace detail {
    class Impl {
    private:
        friend class ::Interface;
        Impl() : x_(0) {}
        int x_;
        void internal() { x_ = 1; }
        void something_else() { x_ = 2; }
    };
}
class Interface {
public:
    int x() const { return impl_.x_; }
    void x(int x) { impl_.x_ = x; }
private:
    Impl impl_;
};

还要查找 PIMPL 习惯用法,它通常用于减少标头/库/客户端耦合并增加编译时间。

这是我的建议,对于使用断言的调试版本:

  • 创建一个简单的函数或一个名为 IncrementInternalCallCount 的宏。这将增加类中的调用计数。为此,只需在类中有一个计数器变量。把这些东西仅用于调试/诊断目的。
  • 类的每个方法中调用此宏/函数。 除了那些 getter 函数。
  • 在 getter 函数中,检查内部调用计数是否大于零,如果大于零。提出断言。

为此,您可以使用一些 RAII 类,该类将递增计数器,并在函数退出时递减计数器。因此,您需要在每个函数的开头放置一个 RAII 对象。析构函数将自动调用。这也将允许从其他类函数调用所有非 getter 函数。

非常少的代码(严格来说C++不正确):

class CYourClass
{
    int Counter = 0; // Not C++ syntax
   IncrementCallCounter() { Counter++;}
   DecrementCallCounter() { Counter++;}
   CheckCounterStatus() { if(Counter>0) ASSERT;}
private:
   void InternalFun() 
  {
     IncrementCallCounter();
    // Do work, call anything
    DecrementCallCounter();
  }
public:
int Getter1()
{
   CheckCounterStatus();
}
};