禁止在类中使用方法
Forbid use of method within class
我有一个类,它有访问器方法,这些方法旨在用于在类外部查看其某些私有数据。
但是,类的内部方法不应使用这些公共访问器,因为类的内部状态由其其他方法更改,并且在计算完成之前,这些更改不会写入公共访问器查看的位置。
是否有编译时方法可以防止类调用其自己的一个或多个成员?或者,使用 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();
}
};
相关文章:
- C++:使用方法调用析构函数的顺序是什么?
- 对类中的对象使用方法
- 正确的使用方法
- 如何使用方法ID从Powerbuilder调用PBNI方法
- 如何使用方法覆盖在输出屏幕上显示堆栈整数值
- 如果未使用方法本身,则忽略方法中的未定义符号
- 使用方法影响另一个对象的字段
- 使用方法失败更改constexpr对象成员
- 如何使用方法在SDL2中的键按键上移动矩形
- 如何使用 C++ 方法创建 JSON 文件
- 如何使用方法连接按钮,请单击
- C 枚举类可以使用方法
- 如何使用方法返回指向对象结构的指针?
- 无法使用方法更改成员值
- 我如何使用方法来处理大量指针
- 如果未在类 c++ 中定义方法,如何在类对象上使用方法
- 是否可以使用方法返回使用 #define 创建的变量
- 通过对象的指针数组分配内存,并使用方法设置它们的名称
- 在指向C中对象的指针上使用方法
- 禁止在类中使用方法