Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const method question

Reply
Thread Tools

const method question

 
 
scooter
Guest
Posts: n/a
 
      10-28-2003
Is there any advantage in using the second form?

thanks



//-------------------------------1st
class MyClass
{
int a;

DoSomething()
{
//do some calculations

Calculate();
}

Calculate()
{
//do some calculations

a = some_value;
}
};


//-------------------------------2nd
class MyClass
{
int a;

DoSomething()
{
//do some calculations

a = Calculate();
}

int Calculate()const
{
//do some calculations

return some_value;
}
};
 
Reply With Quote
 
 
 
 
Josephine Schafer
Guest
Posts: n/a
 
      10-28-2003

"scooter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Is there any advantage in using the second form?
>
> thanks
>
>
>
> //-------------------------------1st
> class MyClass
> {
> int a;
>
> DoSomething()
> {
> //do some calculations
>
> Calculate();
> }
>
> Calculate()
> {
> //do some calculations
>
> a = some_value;
> }
> };
>
>
> //-------------------------------2nd
> class MyClass
> {
> int a;
>
> DoSomething()
> {
> //do some calculations
>
> a = Calculate();
> }
>
> int Calculate()const
> {
> //do some calculations
>
> return some_value;
> }
> };


const means that the member function does not alter the state of the object.
So if your function doesn't change the object's state then declare it const.
Declaring const has an indirect advantage that if you inadvertently try to
mutate object state through a
const function then compiler stops you. Also for debugging purposes you know for
sure that these functions aren't the culprit functions which could have
modified the object state.

HTH,
J.Schafer


 
Reply With Quote
 
 
 
 
Stewart Gordon
Guest
Posts: n/a
 
      10-28-2003


While it was 28/10/03 8:50 am throughout the UK, Josephine Schafer
sprinkled little black dots on a white screen, and they fell thus:

<snip>
> const means that the member function does not alter the state of the object.
> So if your function doesn't change the object's state then declare it const.
> Declaring const has an indirect advantage that if you inadvertently try to
> mutate object state through a const function then compiler stops you.

<snip>

There's also the direct advantage that you can call it on a const object.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.
 
Reply With Quote
 
Josephine Schafer
Guest
Posts: n/a
 
      10-28-2003

"Stewart Gordon" <(E-Mail Removed)> wrote in message
news:bnlpgq$51h$(E-Mail Removed)...
>
>
> While it was 28/10/03 8:50 am throughout the UK, Josephine Schafer
> sprinkled little black dots on a white screen, and they fell thus:


Sprinkling some more black dots........

> <snip>
> > const means that the member function does not alter the state of the object.
> > So if your function doesn't change the object's state then declare it const.
> > Declaring const has an indirect advantage that if you inadvertently try to
> > mutate object state through a const function then compiler stops you.

> <snip>
>
> There's also the direct advantage that you can call it on a const object.
>


Ofcourse.
[To the OP] const qualifier can be used for function overloading.

Overloads -
void foo () const{}
void foo (){}

HTH,
J.Schafer



 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      10-28-2003

"scooter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Is there any advantage in using the second form?
>
> thanks
>
>
>
> //-------------------------------1st
> class MyClass
> {
> int a;
>
> DoSomething()
> {
> //do some calculations
>
> Calculate();
> }
>
> Calculate()
> {
> //do some calculations
>
> a = some_value;
> }
> };
>
>
> //-------------------------------2nd
> class MyClass
> {
> int a;
>
> DoSomething()
> {
> //do some calculations
>
> a = Calculate();
> }
>
> int Calculate()const
> {
> //do some calculations
>
> return some_value;
> }
> };


I'd prefer the second. That Calculate function can be called from other
const functions, while the first one can't. The name of the first function
DoSomething implies that it's the one that will actually be making some
change. The second function just figures it out. It seems to me that there
is lower "coupling" and higher "cohesion" in the second example, if you'd
like to look up those terms.


 
Reply With Quote
 
chris
Guest
Posts: n/a
 
      10-28-2003
Josephine Schafer wrote:

> "Stewart Gordon" <(E-Mail Removed)> wrote in message
> news:bnlpgq$51h$(E-Mail Removed)...
>
>>
>>While it was 28/10/03 8:50 am throughout the UK, Josephine Schafer
>>sprinkled little black dots on a white screen, and they fell thus:

>
>
> Sprinkling some more black dots........
>
>
>><snip>
>>
>>>const means that the member function does not alter the state of the object.
>>>So if your function doesn't change the object's state then declare it const.
>>>Declaring const has an indirect advantage that if you inadvertently try to
>>>mutate object state through a const function then compiler stops you.

>>
>><snip>
>>
>>There's also the direct advantage that you can call it on a const object.
>>

>
>
> Ofcourse.
> [To the OP] const qualifier can be used for function overloading.
>
> Overloads -
> void foo () const{}
> void foo (){}
>
> HTH,
> J.Schafer
>
>

although you really won't make friends if 'foo() const' and 'foo()' do
different things!

 
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
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments