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) 'Test::val' is an invariant and
'Test::s_int' is a class invariant. Is my understanding correct ? If I
am wrong, kindly provide the definitions for the terms 'Invariant' and
'Class Invariant' with supporting program sample.
Thanks
V.Subramanian
While const data members may fit the literal definition of
"invariant",
usually it is used to describe a sort of rule that a section of code
or class must obey.
For example, I expect the std::vector class has a class invariant
"the size of this vector is less than or equal to the reserved
size". Operations must then obey this invariant. If the vector were to
add new elements, it must ensure that there is enough reserved data
to hold those new elements. And if the vector's reserve were to shrink
(as some kind of garbage collection, for instance), then it can only
shrink a certain amount.
There are more formal ways of saying this, but that's the general
idea.
Take a look here, too: http://en.wikipedia.org/wiki/Object_invariant
--Jonathan
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).
> return EXIT_SUCCESS;
> }
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
> <subramanian10...@yahoo.com> wrote:
> > 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)
> While const data members may fit the literal definition of
> "invariant",
Not really, since an invariant must be a predicate. (A const
data member of type bool could be considered an invariant, but
it's not a very interesting one, since it is trivially true,
regardless of what you do.)
--
James Kanze
"const" is essentially the predicate P(x) = "x does not change".
A const data member is a value such that P(x) is true. So, okay,
the value _itself_ isn't a predicate, but it immediately implies
one.
--Jonathan
> > > On Jul 29, 11:09 pm, "subramanian10...@yahoo.com, India"
> > > <subramanian10...@yahoo.com> wrote:
> > > > 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)
> > > While const data members may fit the literal definition of
> > > "invariant",
> > Not really, since an invariant must be a predicate. (A const
> > data member of type bool could be considered an invariant, but
> > it's not a very interesting one, since it is trivially true,
> > regardless of what you do.)
> "const" is essentially the predicate P(x) = "x does not change".
One can look at it like that, although the predicate would be
more along the lines "x == whatever".
A predicate involving time doesn't make sense as an invariant,
since it is the concept of invariant which introduces the
element of time: at all instances (within a certain time frame),
the predicate is true.
> A const data member is a value such that P(x) is true. So, okay,
> the value _itself_ isn't a predicate, but it immediately implies
> one.
A const data member certainly implies a predicate: "x ==
initialization value", and thus an invariant. Such invariants
are usually not considered, however, since they are evident and
impossible (more or less) to violate. More interesting are
invariants like the one cited by someone else, "size() <=
capacity()" for std::vector, or loop invariants like "i >= 0 &&
i < v.size()" (where v is a vector).
--
James Kanze