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,
Sample 2,
thanks in advance,
George
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;
}
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;
}
thanks in advance,
George