473,379 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

const method question

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;
}
};
Jul 19 '05 #1
5 4113

"scooter" <sc*****@btopenworld.com> wrote in message
news:t4********************************@4ax.com...
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
Jul 19 '05 #2


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.
Jul 19 '05 #3

"Stewart Gordon" <sm*******@yahoo.com> wrote in message
news:bn**********@sun-cc204.lut.ac.uk...


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

Jul 19 '05 #4

"scooter" <sc*****@btopenworld.com> wrote in message
news:t4********************************@4ax.com...
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.
Jul 19 '05 #5
Josephine Schafer wrote:
"Stewart Gordon" <sm*******@yahoo.com> wrote in message
news:bn**********@sun-cc204.lut.ac.uk...

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

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the...
1
by: Capstar | last post by:
Hi NG, I have a question on std::for_each. I try to use this in combination with std::bind2nd to call a method of all functions in a container (std::set or std::map) and pass that method the...
20
by: Chris | last post by:
Hi, can you define const-functions in C# as in C++ ? for example (C++-code) : int Cube :: GetSide() const { return m_side; }
3
by: David Scarlett | last post by:
Hi all, I've got a question regarding overriding const member functions with non-const functions. Let's say I've got a base class defined as follows: /*******************/ class Foo {...
2
by: Jim Langston | last post by:
I'm a little confused. I have a class function declared like: const CItem& operator << (const std::string &sIn); Which I use like this: CItem Item; Item <<...
3
by: dj | last post by:
Perhaps this question should be in the standard c newsgroup, but i hope somebody answers it here. Anyway, I came across this situation in an otherwise c++ code. I need a struct that works like...
15
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
8
by: neelsmail | last post by:
Hi, I have just started learning C# ; I was working in C++ till now (and would like to if allowed, but learning new things dont hurt). I am used to specifiying "const" to get the assurance that...
9
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen...
2
by: yuyang08 | last post by:
Dear all, I have a question on the const methods. If a method is overloaded with a const version, in the case that either one is okay (for example, the following code), which shall the compiler...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.