Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > To bean or not to bean

Reply
Thread Tools

To bean or not to bean

 
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-29-2004
Phlip wrote:

> Steven T. Hatton wrote:
>> I can't imagine
>> anyone has a more extensive understanding of the relationship between C++
>> and the Cpp than he does. Why do you think he is wrong about it?

>
> Use the Cpp for:
>
> - token pasting

Why? What can that give me that I cannot achieve using the internal
features of the language?

> - stringerization


I'm not familiar with the term. Care to explain?

> - conditional compilation (oh, yeah. Everyone likes that one...)


I am aware that the Cpp is used for this. There have, as yet, been no
viable alternatives introduced, and accepted into the C++ standard.

> Putting those inside a C language would make it not a C language. And they
> permit techniques that more "modern" languages need but can't do.
>
> A repost:
>
> Visual Studio surfs to errors using <F8>: Go To Output Window Next
> Location. The Windows SDK function to write text into the output panel,
> for this feature to read it and surf to an error, is OutputDebugString().
> Putting them all together yields this killer trace macro:
>
> #define db(x_) do { std::stringstream z; \
> z << __FILE__ << "(" << __LINE__ << ") : " \
> #x_ " = " << x_ << endl; \
> cout << z.str() << std::flush; \
> OutputDebugStringA(z.str().c_str()); \
> } while (false)
>
> That takes any argument, including expressions, which support operator<<.
> We will return to these techniques while exploring more Fault Navigation
> issues in C++.
>
> db(q) pushes "C:\path\source.cpp(99) : q = 5\n" into the Output Debug
> panel. <F8> parses the file name and line number and navigates your editor
> directly to the line containing the db(q).
>
> Those are major wins. Tracing with db() is very low cost for very high
> feedback.


I guess I'm not getting what that does for me. Can you explain how I would
gain from the use of such a macro? I don't use microsoft products very
often, so I really have no idea of what you are talking about. Can you
explain how I could use this macro without using a specific IDE?

> C++ has flaws. But those of you inclined to dismiss it entirely are
> invited to write db(), with all these features, in your favorite language.


Who has dismissed C++?

--
"[M]y dislike for the preprocessor is well known. Cpp is essential in C
programming, and still important in conventional C++ implementations, but
it is a hack, and so are most of the techniques that rely on it. ...I think
the time has come to be serious about macro-free C++ programming." - B. S.

 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      08-29-2004
Steven T. Hatton wrote:

> E. Robert Tisdale wrote:
>
>>Steven T. Hatton wrote:
>>
>>
>>>Stroustrup's view on classes, for the most part,
>>>seems to be centered around the notion of invariants.
>>>After a bit of adjusting to the use of yamt
>>>(yet another math term) in computer science,
>>>I came to appreciate some of the significance in what he is asserting.

>>
>>Please site passage an verse.

>
> Regarding what? I'm not sure what you are questioning.


I'm questioning your interpretation of "Stroustrup's view on classes".
I'm assuming that you read something that Stroustup wrote and published
and that you aren't communicating with him privately
and that you are not reading his mind.
It would help if you could cite publication and, perhaps,
quote the passage that he wrote.

>>You probably misunderstood what Stroustrup was saying.
>>There is nothing terribly profound in language design.
>>It is a mistake to read too much into what Stroustup says.
>>He really tries to explain his ideas in the most straight-forward manner
>>using *plain* English.
>>
>>
>>>I believe a good example would be the Riemann-Christoffel curvature
>>>tensor.

>>
>>No, it isn't a good example.
>>Very few computer programmers have cause to truck with such things.

>
> For my purposes, it was a good example.
> It is irrelevant whether a person understand
> the full definition of the tensor.
> Only that it has invariants,
> and that it has multiple data elements, some of which are redundant.
>
>>A C++ class (or struct) is used
>>to introduce a User Defined Type (UDT) into a program.
>>A C++ class is most useful in implementing an Abstract Data Type (ADT).
>>The reason for data hiding (private data members) is practical.
>>It allows the class library developer to change the data representation
>>without any changes to application programs which use the class library
>>except, possibly, recompiling the application program and linking in
>>the revised class library.

>
> I'm not sure how that really address my question.
> I am asking whether the creation of a class that consists of data members
> which are all manipulatable through the use of access functions
> is contrary to philosophy that
> a class should have a clearly defined invariant that is preserved
> when operated on by invoking its member or friend functions functions.
>
> More specifically, I am asking if the creation of a class
> that consists of nothing but data fields and set and get methods
> is an indication that I am doing something wrong.


It is an *indication* that you might be doing something wrong.
If only get and set methods are defined,
the object is nothing more than a "junk box".
An array (of any type) is an example of such an object.
A vector, matrix or tensor object is superficially similar to an array
but with vector arithmetic operations and other interesting methods.

I'll offer a more simple example:

class SocialSecurityNumber {
private:
unsigned long int Number;
public:
explicit
SocialSecurityNumber(unsigned long int n): Number(n) {
// throw exception if n does not represent a valid SSN
}
explicit
SocialSecurityNumber(std::string n) {
// throw an exception if n does not represent a valid SSN
}
operator unsigned long int(void) const {
return Number;
}
operator std::string(void) const {
std:stringstream oss;
oss << Number/1000000 << '-'
<< (Number/10000)%100 << '-'
<< Number%10000;
return oss.str();
}
};

You can't do much with a social security number except set and get it.
In this case, the setter is a constructor and the getter is a type cast.
There is no "invarient" because a social security number can't change
except via the assignment operator

SocialSecurityNumber& operator=(const SocialSecurityNumber&);

All this shows is that even if all you have is getters and setters,
the needn't be named

SocialSecurityNumber::get(/* arguments */) const;
SocialSecurityNumber::set(/* arguments */);


 
Reply With Quote
 
 
 
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-29-2004
E. Robert Tisdale wrote:

> Steven T. Hatton wrote:


> I'm questioning your interpretation of "Stroustrup's view on classes".
> I'm assuming that you read something that Stroustup wrote and published
> and that you aren't communicating with him privately
> and that you are not reading his mind.
> It would help if you could cite publication and, perhaps,
> quote the passage that he wrote.


TC++PL(SE) 24.3.7.1 Invariants

"The values of the members and the objects referred to by members are
collectively called the /state/ of the object (or simply, its /value/). A
major concern of a class design is to get an object into a well-defined
state (initialization/construction), to maintain a well-defined state as
operations are performed, and finally to destroy the object gracefully.
The property that makes the state of an object well-defined is called
its /invariant/."
....
"Much of the skill in class design involves making a class simple enough to
make it possible to implement it so that it has a useful invariant that can
be expressed simply. It is easy enough to state that every class needs an
invariant. The hard part is to come up with a useful invariant that is
easy to comprehend and that doesn't impose unacceptable constraints on the
implementer or on the efficiency of the operations."

>
> I'll offer a more simple example:
>
> class SocialSecurityNumber {
> private:
> unsigned long int Number;
> public:
> explicit
> SocialSecurityNumber(unsigned long int n): Number(n) {
> // throw exception if n does not represent a valid SSN
> }
> explicit
> SocialSecurityNumber(std::string n) {
> // throw an exception if n does not represent a valid SSN
> }
> operator unsigned long int(void) const {
> return Number;
> }
> operator std::string(void) const {
> std:stringstream oss;
> oss << Number/1000000 << '-'
> << (Number/10000)%100 << '-'
> << Number%10000;
> return oss.str();
> }
> };
>
> You can't do much with a social security number except set and get it.
> In this case, the setter is a constructor and the getter is a type cast.
> There is no "invarient" because a social security number can't change
> except via the assignment operator
>
> SocialSecurityNumber& operator=(const SocialSecurityNumber&);
>
> All this shows is that even if all you have is getters and setters,
> the needn't be named
>
> SocialSecurityNumber::get(/* arguments */) const;
> SocialSecurityNumber::set(/* arguments */);


There is an invariant in the SocialSecurityNumber. It is established by
checking that it is valid when constructed, and maintained by checking that
it is valid when assigned to.
--
"[M]y dislike for the preprocessor is well known. Cpp is essential in C
programming, and still important in conventional C++ implementations, but
it is a hack, and so are most of the techniques that rely on it. ...I think
the time has come to be serious about macro-free C++ programming." - B. S.

 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      08-29-2004
On Sat, 28 Aug 2004 21:18:57 -0400, "Steven T. Hatton"
<(E-Mail Removed)> wrote:

[snip]
>As for trailing underscores, what is your alternative?


I like to prefix the names of member data with "m_". It's only one
more character to type, and so much easier to read.

>> I find it much easier
>> to avoid superfluous comma errors like this because I don't have to
>> read to the end of the line to find it.

>
>Agreed. What about situations such as:
>
>bigLongLeftHandSide
> = bigLongIdentifier
> ->bigLongMemberName
> ->yourGonnaWrapSucker
> ->bigLongFunctionName();
>?


I like the 80 column rule. It helps to use indentation to add
structure, as in the example above:

bigLongLeftHandSide =
bigLongIdentifier
->bigLongMemberName
->yourGonnaWrapSucker
->bigLongFunctionName();

I like to move the lines after the first one far enough over to the
right to make it obvious that they are on the RHS. And I try to avoid
overly long names, too.

My compiler usually catches missing semicolons, but often has problems
reporting a missing comma ... i.e., there will be an error message,
but it will be somewhat cryptic.

--
Bob Hairgrove
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-29-2004
Bob Hairgrove wrote:

> On Sat, 28 Aug 2004 21:18:57 -0400, "Steven T. Hatton"
> <(E-Mail Removed)> wrote:
>
> [snip]
>>As for trailing underscores, what is your alternative?

>
> I like to prefix the names of member data with "m_". It's only one
> more character to type, and so much easier to read.


I refuse! I will not be assimilated! I will never do that! Actually,
after bashing my knuckles a few times on the alternative, that approach
does have its appeal. I just find it redundant. That's what /this/ is
for. And I do use /this/ religiously. The one place where it doesn't
serve me well is in the member initialization list.

I seem to recall reading that /this/ should be available there, but it has
never worked there for me when I tried to use it. I should try to look it
up in the Standard to see what is actually specified, but not right now.

> bigLongLeftHandSide =
> bigLongIdentifier
> ->bigLongMemberName
> ->yourGonnaWrapSucker
> ->bigLongFunctionName();
>
> I like to move the lines after the first one far enough over to the
> right to make it obvious that they are on the RHS. And I try to avoid
> overly long names, too.


I am slowly adjusting to using shorter names in C++. The Java dogma is to
spell out everything, and follow strict rules of capitalization. The
advantage is that it is very predictable - so long as no one decides color
should have a 'u' in it. The disadvantage is that it tends to obscure the
logic a bit, and leads to the problem we are discussing.

The place where I encounter the need to wrap my member access strings is
when working with XML DOM.

As for indenting the second and subsequent '->' or '.', my editors don't do
that by default, and I find the meaning pretty clear without the additional
indentation.

> My compiler usually catches missing semicolons, but often has problems
> reporting a missing comma ... i.e., there will be an error message,
> but it will be somewhat cryptic.


I prefer the editor to catch such mistakes. It took me some time to get
used to Emacs, but once I caught on, I found the syntax sensitive
indentation invaluable. Regardless of whether I'm writing DocBook EBNF XML
or C++, if I mess up the syntax, the indentation stops working after the
point where I made the mistake. KDevelop also supports a very similar kind
of behavior.

With JBuilder, the editor actually highlights syntax errors, and even
catches what I consider to be semantic errors such as using an identifier
which is not in scope. KDevelop does some of this, but I don't believe it
is even possible with C++ to provide the level of edit-time error detection
I've seen done with Java. See my signature for the reason.
--
"[M]y dislike for the preprocessor is well known. Cpp is essential in C
programming, and still important in conventional C++ implementations, but
it is a hack, and so are most of the techniques that rely on it. ...I think
the time has come to be serious about macro-free C++ programming." - B. S.

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      08-29-2004
Steven T. Hatton wrote:

> > Use the Cpp for:
> >
> > - token pasting


> Why? What can that give me that I cannot achieve using the internal
> features of the language?


The TEST_() macro uses it.

> > - stringerization

>
> I'm not familiar with the term. Care to explain?


My #define db() sample.

> > - conditional compilation (oh, yeah. Everyone likes that one...)

>
> I am aware that the Cpp is used for this. There have, as yet, been no
> viable alternatives introduced, and accepted into the C++ standard.


That's my point. Those who bust on the CPP overlook CC, and CC works fine to
make large systems managable, such as Linux.

Another great thing about the CPP is it's language-agnostic (so long as you
don't find a language that does not balance "" and () operators, or use
commas for something silly). So the same macros can influence, for example,
your .cpp, .rc, and .idl files, etc.

> > Putting those inside a C language would make it not a C language. And

they
> > permit techniques that more "modern" languages need but can't do.
> >
> > A repost:
> >
> > Visual Studio surfs to errors using <F8>: Go To Output Window Next
> > Location. The Windows SDK function to write text into the output panel,
> > for this feature to read it and surf to an error, is

OutputDebugString().
> > Putting them all together yields this killer trace macro:
> >
> > #define db(x_) do { std::stringstream z; \
> > z << __FILE__ << "(" << __LINE__ << ") : " \
> > #x_ " = " << x_ << endl; \
> > cout << z.str() << std::flush; \
> > OutputDebugStringA(z.str().c_str()); \
> > } while (false)
> >
> > That takes any argument, including expressions, which support

operator<<.
> > We will return to these techniques while exploring more Fault Navigation
> > issues in C++.
> >
> > db(q) pushes "C:\path\source.cpp(99) : q = 5\n" into the Output Debug
> > panel. <F8> parses the file name and line number and navigates your

editor
> > directly to the line containing the db(q).
> >
> > Those are major wins. Tracing with db() is very low cost for very high
> > feedback.

>
> I guess I'm not getting what that does for me. Can you explain how I

would
> gain from the use of such a macro? I don't use microsoft products very
> often, so I really have no idea of what you are talking about. Can you
> explain how I could use this macro without using a specific IDE?


It is a trace macro, like TRACE(). You put an expression in, and the macro
inserts its value into both the console and the Output panel (which all MS
Windows IDEs support).

Read my verbiage again.

> > C++ has flaws. But those of you inclined to dismiss it entirely are
> > invited to write db(), with all these features, in your favorite

language.
>
> Who has dismissed C++?


I copied that repost from an essay, where, as a rhetorical technique, I
hypothesized that someone dismissed C++.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


 
Reply With Quote
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-29-2004
Phlip wrote:

> Steven T. Hatton wrote:
>
>> > Use the Cpp for:
>> >
>> > - token pasting

>
>> Why? What can that give me that I cannot achieve using the internal
>> features of the language?

>
> The TEST_() macro uses it.


What is the TEST_() macro? I searched the ISO/IEC 14882:2003 for the string
"TEST" using acrobat, and got no hits. I assume it is not part of standard
C++?

>> > - stringerization

>>
>> I'm not familiar with the term. Care to explain?

>
> My #define db() sample.


That doesn't explain what the word means.

>> > - conditional compilation (oh, yeah. Everyone likes that one...)

>>
>> I am aware that the Cpp is used for this. There have, as yet, been no
>> viable alternatives introduced, and accepted into the C++ standard.

>
> That's my point. Those who bust on the CPP overlook CC, and CC works fine
> to make large systems managable, such as Linux.


Linux is written in C. It is a very controlled development process that
requires a great deal of specialized expertise to work on. Working on any
given component does not require knowledge of a wide variety of rapidly
changing interfaces.

I really don't know what you mean by the "CC". To me, CC is a somewhat
antiquated alias for the C Compiler.

Now if you want to talk about program suites such as the KDE, /that/ is
written in C++ using Qt. I've been working with the KDE since spring of
1997. It is an extremely impressive project, with many gifted
contributors. The component of the KDE which I have spent a good deal of
the past several months working with is KDevelop. That is the IDE
distrubuted with the KDE. I am acutely aware of its capabilities and
limitations.

I've also worked with J2EE fairly extensively. For purposes of developing
enterprise applications such as the web based interface for the US Army's
personnel database, BEA's WebLogic is easier to use, and facilitates faster
development than anything I know of based on C++.

> Another great thing about the CPP is it's language-agnostic (so long as
> you don't find a language that does not balance "" and () operators, or
> use commas for something silly). So the same macros can influence, for
> example, your .cpp, .rc, and .idl files, etc.


That doesn't address the problems created by using the CPP.

http://www.freshsources.com/bjarne/ALLISON.HTM

//--------------excerpt-----------------------------
CUJ: What do you do for your day job now?

BS: I'm trying to build up a research group to focus on large-scale
programming -- that is, to do research on the use of programming in large
programs rather than just language design, just the study of small
(student) programs, or the exclusive focus on design and/or process. I
think that programming technique, programming language, and the individual
programmer have a central role in the development of large systems. Too
often, either the scale of industrial projects or the role of programming
is ignored. This research will involve work on libraries and tools.
....

CUJ: What is the next step in the evolution of C++?

BS: Tools/environments and library design. I'd like to see incremental
compilers and linkers for C++. Something like two seconds is a suitable
time for re-compiling and re-linking a medium-sized C++ program after a
change that is localized to a few functions. I'd like to see browsers and
analysis tools that know not only syntax but also the type of every entity
of a program. I'd like to see optimizers that actually take notice of C++
constructs and do a decent job of optimizing them, rather than simply
throwing most of the useful information away and giving the rest to an
optimizer that basically understands only C. I'd like to see debuggers
integrated with the incremental compiler so that the result approximates a
C++ interpreter. (I'd also like to see a good portable C++ interpreter.)
None of this is science fiction; in fact, I have seen experimental versions
of most of what I suggest -- and more. We still suffer from
first-generation C++ environments and tools.
....

The preprocessor is one of the main factors that has led to the lack of more
sophisticated C program development environments: the fact that the source
text seen by the programmer isn't the text seen by the compiler is a
crippling handicap. I think the time has come to be serious about
macro-free C++ programming.
//--------------end-excerpt-----------------------------

> It is a trace macro, like TRACE(). You put an expression in, and the macro
> inserts its value into both the console and the Output panel (which all MS
> Windows IDEs support).
>
> Read my verbiage again.


Either I'm missing something, or that is far from impressive. The average
Java IDE can trace code and show me the value of any variable in detail. I
can also browse through the activation stack which is presented as a
clickable tree with all the classes and class members available for
inspection.

--
"[M]y dislike for the preprocessor is well known. Cpp is essential in C
programming, and still important in conventional C++ implementations, but
it is a hack, and so are most of the techniques that rely on it. ...I think
the time has come to be serious about macro-free C++ programming." - B. S.

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      08-29-2004
Steven T. Hatton wrote:

> Phlip wrote:


> >> > Use the Cpp for:
> >> >
> >> > - token pasting


> > The TEST_() macro uses it.

>
> What is the TEST_() macro? I searched the ISO/IEC 14882:2003 for the

string
> "TEST" using acrobat, and got no hits. I assume it is not part of standard
> C++?


Steve, people are allowed to write other macros than those that appear in
The Standard. Google this newsgroup for my street name, TEST_, and
Steinbach.

> >> > - stringerization
> >>
> >> I'm not familiar with the term. Care to explain?

> >
> > My #define db() sample.

>
> That doesn't explain what the word means.


Look inside my macro for the # operator.

> >> > - conditional compilation (oh, yeah. Everyone likes that one...)
> >>
> >> I am aware that the Cpp is used for this. There have, as yet, been no
> >> viable alternatives introduced, and accepted into the C++ standard.

> >
> > That's my point. Those who bust on the CPP overlook CC, and CC works

fine
> > to make large systems managable, such as Linux.

>
> Linux is written in C.


Snip from here down. I can't find evidence you are reading my posts.

Please take a deep breath, and entertain the idea that passionately decrying
a handful of keywords is a kind of zealotry.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


 
Reply With Quote
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-29-2004
Phlip wrote:

> Steven T. Hatton wrote:
>
>> Phlip wrote:

>
>> >> > Use the Cpp for:
>> >> >
>> >> > - token pasting

>
>> > The TEST_() macro uses it.

>>
>> What is the TEST_() macro? I searched the ISO/IEC 14882:2003 for the

> string
>> "TEST" using acrobat, and got no hits. I assume it is not part of
>> standard C++?

>
> Steve, people are allowed to write other macros than those that appear in
> The Standard. Google this newsgroup for my street name, TEST_, and
> Steinbach.


This is silly. You used a macro that you wrote as an example that I am
expected to be familiar with, without even mentioning that you wrote it.

>> >> > - stringerization
>> >>
>> >> I'm not familiar with the term. Care to explain?
>> >
>> > My #define db() sample.

>>
>> That doesn't explain what the word means.

>
> Look inside my macro for the # operator.


Perhaps you could simply provide a definition for the term. My attempting
to extract a definition from an example has the significant potential for
my arriving at a different definition than the one you intend.

> Snip from here down. I can't find evidence you are reading my posts.
>
> Please take a deep breath, and entertain the idea that passionately
> decrying a handful of keywords is a kind of zealotry.


Sorry if you were confused by my use of words I learned in college when I
studied computer science. I can't say I will refrain from doing so in the
future, but I will do my best to restrict my vocabulary when addressing you
directly.

Now, back to the topic at hand. I changed the subject field in the heading
to reflect the fork this thread has taken. For the moment, forget any of
my own comments regarding the Cpp, and explain to the news group where
Bjarne Stroustrup is in error regarding the opinions expressed in the
following:


http://www.freshsources.com/bjarne/ALLISON.HTM

//--------------excerpt-----------------------------
*CUJ:*What*do*you*do*for*your*day*job*now?

BS: I'm trying to build up a research group to focus on large-scale
programming -- that is, to do research on the use of programming in large
programs rather than just language design, just the study of small
(student) programs, or the exclusive focus on design and/or process. I
think that programming technique, programming language, and the individual
programmer have a central role in the development of large systems. Too
often, either the scale of industrial projects or the role of programming
is ignored. This research will involve work on libraries and tools.
....

*CUJ:*What*is*the*next*step*in*the*evolution*of*C++?

BS: Tools/environments and library design. I'd like to see incremental
compilers and linkers for C++. Something like two seconds is a suitable
time for re-compiling and re-linking a medium-sized C++ program after a
change that is localized to a few functions. I'd like to see browsers and
analysis tools that know not only syntax but also the type of every entity
of a program. I'd like to see optimizers that actually take notice of C++
constructs and do a decent job of optimizing them, rather than simply
throwing most of the useful information away and giving the rest to an
optimizer that basically understands only C. I'd like to see debuggers
integrated with the incremental compiler so that the result approximates a
C++ interpreter. (I'd also like to see a good portable C++ interpreter.)
None of this is science fiction; in fact, I have seen experimental versions
of most of what I suggest -- and more. We still suffer from
first-generation C++ environments and tools.
....

The preprocessor is one of the main factors that has led to the lack of more
sophisticated C program development environments: the fact that the source
text seen by the programmer isn't the text seen by the compiler is a
crippling handicap. I think the time has come to be serious about
macro-free C++ programming.
//--------------end-excerpt-----------------------------

--
"[M]y dislike for the preprocessor is well known. Cpp is essential in C
programming, and still important in conventional C++ implementations, but
it is a hack, and so are most of the techniques that rely on it. ...I think
the time has come to be serious about macro-free C++ programming." - B. S.

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      08-29-2004
Steven T. Hatton wrote:

> E. Robert Tisdale wrote:
>
>>Steven T. Hatton wrote:

>
>>I'm questioning your interpretation of "Stroustrup's view on classes".
>>I'm assuming that you read something that Stroustup wrote and published
>>and that you aren't communicating with him privately
>>and that you are not reading his mind.
>>It would help if you could cite publication and, perhaps,
>>quote the passage that he wrote.

>
> TC++PL(SE) 24.3.7.1 Invariants
>
> "The values of the members and the objects referred to by members are
> collectively called the /state/ of the object (or simply, its /value/). A
> major concern of a class design is to get an object into a well-defined
> state (initialization/construction), to maintain a well-defined state as
> operations are performed, and finally to destroy the object gracefully.
> The property that makes the state of an object well-defined is called
> its /invariant/."
> ...
> "Much of the skill in class design involves making a class simple enough to
> make it possible to implement it so that it has a useful invariant that can
> be expressed simply. It is easy enough to state that every class needs an
> invariant. The hard part is to come up with a useful invariant that is
> easy to comprehend and that doesn't impose unacceptable constraints on the
> implementer or on the efficiency of the operations."
>
>>I'll offer a more simple example:
>>
>>class SocialSecurityNumber {
>>private:
>>unsigned long int Number;
>>public:
>>explicit
>>SocialSecurityNumber(unsigned long int n): Number(n) {
>>// throw exception if n does not represent a valid SSN
>>}
>>explicit
>>SocialSecurityNumber(std::string n) {
>>// throw an exception if n does not represent a valid SSN
>>}
>>operator unsigned long int(void) const {
>>return Number;
>>}
>>operator std::string(void) const {
>>std:stringstream oss;
>>oss << Number/1000000 << '-'
>><< (Number/10000)%100 << '-'
>><< Number%10000;
>>return oss.str();
>>}
>>};
>>
>>You can't do much with a social security number except set and get it.
>>In this case, the setter is a constructor and the getter is a type cast.
>>There is no "invarient" because a social security number can't change
>>except via the assignment operator
>>
>>SocialSecurityNumber& operator=(const SocialSecurityNumber&);
>>
>>All this shows is that even if all you have is getters and setters,
>>the needn't be named
>>
>>SocialSecurityNumber::get(/* arguments */) const;
>>SocialSecurityNumber::set(/* arguments */);

>
> There is an invariant in the SocialSecurityNumber.
> It is established by checking that it is valid when constructed
> and maintained by checking that it is valid when assigned to.


Yes. The invariant is trivial.
And I think that your understanding of invariance is consistent
with the way that Stroustrup uses the term.
Notice that, in the example above, there is no way to construct
an invalid SocialSecurityNumber and no assignment is defined
except the default assignment from another valid SocialSecurityNumber.
 
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
Struts - bean:write or html:text - not getting bean value - Please help jill Java 5 12-15-2005 03:03 PM
Differnce between Java Bean and Enterprise Java Bean Markku Salminen Java 3 01-21-2004 09:25 AM
Mr. Bean Complete Bean NOT complete!! Waterperson77 DVD Video 5 12-10-2003 05:22 PM
can a session bean return a local entity bean object? David Thielen Java 2 09-12-2003 07:45 AM
To bean or not to bean... Benjamin Stewart Java 0 06-30-2003 12:34 AM



Advertisments