私の教授は、クラス不変式に敬意を払って整形された状態を維持するアサーション(check_inv())を使用するのが適切かどうかを判断する練習をしました。C++でのassert()の適切な使用
私はクラスinvariantをチェックする必要があるときは分かりませんが、私は彼のコンストラクタからオブジェクトを返す前にクラスの不変をチェックするのが良い練習であることを知っていますが、私は完全には分かりません。
これは試験のテキストです:
iは不変クラスを確認してください
class C
{
public:
bool check_inv() const
{
// Implementation of class invariant code
}
C(int a, int b)
{
// Implementation of class constructor.
}
void foo(C& y)
{
// Implementation of method foo.
}
void bar(const C& y)
{
// Implementation of method bar.
}
void ying(const C& y)
{
// Implementation of method ying.
}
void yang(const C& y) const
{
// Implementation of method yang.
}
~C()
{
// Implementation of class destructor.
}
static void zen(int i, double d)
{
//Implementation of method zen.
}
// ... Some other code ...
}; // class C
?
EDIT:これは宿題に関するものではありません。例を使ってアサーションの機能を理解するだけです。
これは宿題であるため、このトピックをオフトピックとして閉じるよう投票しています。 – Benoit
@Benoitそれは、それ自体が近い理由ではありません。 http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions –
本当に安全にしたいのであれば、毎回あらゆる機能の開始時にチェックすることができますオブジェクトを変更した可能性のある関数を残します。 –