Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: definition for invariant and class invariant

Reply
Thread Tools

Re: definition for invariant and class invariant

 
 
James Kanze
Guest
Posts: n/a
 
      07-30-2010
On Jul 30, 4:09 am, "(E-Mail Removed), India"
<(E-Mail Removed)> wrote:
> In the websitehttp://en.wikipedia.org/wiki/InvariantI found the
> following definition for 'invariant'
> Invariant(ComputerScience), an expression whose value doesn't change
> during program execution.


No you didn't. There's no definition on the page you cite
above, just a bit of text to disambiguate different entries.
Any definition would be in one of the entries (and the entry
which is referenced by the text you cite gives a complete
definition).

> For example, consider the following program:
> #include <cstdlib>
> #include <iostream>


> using namespace std;
>
> class Test
> {
> public:
> explicit Test(int arg = 0);
> int get(void) const;
> static int get_static();
> private:
> const int val;
> static const int s_int;
> };


> const int Test::s_int = 100;


> inline Test::Test(int arg) : val(arg)
> {
> }


> inline int Test::get(void) const
> {
> return val;
> }


> inline int Test::get_static(void)
> {
> return s_int;
> }


> int main()
> {
> cout << "static const int Test::s_int = "
> << Test::get_static() << endl;
> Test first(1);
> cout << first.get() << endl;


> Test second(1000);
> cout << second.get() << endl;


> return EXIT_SUCCESS;
> }


> In the above program, (from the above definition, I thought an
> 'invariant' is a non-static const data member and a 'class invariant'
> is a static const data member)


If I were you, I'd reread the page in question, then go to the
entries it references. In particular, for class invariant, go
to the page on class invariants referenced on that page.

If you read the text in the article "invariant", you'll see that
an invariant is always a predicate. Thus, in the above code,
"Test::s_int == 100" is an invariant.

> 'Test::val' is an invariant and
> 'Test::s_int' is a class invariant. Is my understanding correct?


Not at all.

> If I am wrong, kindly provide the definitions for the terms
> 'Invariant' and 'Class Invariant' with supporting program
> sample.


You might start by reading the articles in Wikipedia. The
Wikipedia may not be the best reference in the world, but in
this case, it does present a reasonably good introduction to
class invariants; the article on loop invariants is also worth
reading.

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: definition for invariant and class invariant Jonathan Lee C++ 3 07-31-2010 11:52 AM
Keeping datetime culture invariant helveticus ASP .Net 2 09-23-2008 03:18 PM
min/max: "stable" invariant? Giovanni Bajo Python 1 03-11-2008 11:34 PM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
Handling class invariant violations DaKoadMunky C++ 8 12-05-2004 08:53 PM



Advertisments