const method question

S

scooter

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;
}
};
 
J

Josephine Schafer

scooter said:
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
 
S

Stewart Gordon

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:

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.
 
J

Josephine Schafer

Stewart Gordon said:
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>

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
 
J

jeffc

scooter said:
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.
 
C

chris

Josephine said:
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>

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! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top