Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this a const class member function

Reply
Thread Tools

Is this a const class member function

 
 
John
Guest
Posts: n/a
 
      04-14-2010
Hi,

Suppose I have the following class

class A
{
int a;

void func1(int& x) const { x = 1; }

void func2()
{
func1(a);
}

};

Should "func1" be a const member function? It doesn't change any
member data directly, but if called with member data as the argument
then the class data has changed before/after the call.

Thanks,
John
 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      04-14-2010
Leigh Johnston wrote:
>
>
> "John" <> wrote in message
> news:hq4n91$au7$...
>> Hi,
>>
>> Suppose I have the following class
>>
>> class A
>> {
>> int a;
>>
>> void func1(int& x) const { x = 1; }
>>
>> void func2()
>> {
>> func1(a);
>> }
>>
>> };
>>
>> Should "func1" be a const member function? It doesn't change any
>> member data directly, but if called with member data as the argument
>> then the class data has changed before/after the call.
>>
>> Thanks,
>> John

>
> I would say that it should be const yes but you could also argue that it
> should be static instead if it doesn't involve any state of A. func2()
> would not be callable via a const reference to A.
>
> /Leigh


Hi Leigh,

Are there any disadvanatages to making making a function like this
static. In other words, do you recommend making all member functions
static that do not change the state of a class?

Why would we want func2 to be callable by a const reference to A? Since
func2 is not 'const', shouldn't we want a const& reference to A to not
be able to call it?

Thanks,
John
 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      04-14-2010
>> Are there any disadvanatages to making making a function like this
>> static. In other words, do you recommend making all member functions
>> static that do not change the state of a class?

>
> No disadvantages as such just make sure the role of your class is
> clearly defined and it makes sense for the static member functions to be
> a part of the class rather than being free functions.
>
>>
>> Why would we want func2 to be callable by a const reference to A?
>> Since func2 is not 'const', shouldn't we want a const& reference to A
>> to not be able to call it?
>>

>
> Correct, as you couldn't call func2() via a const reference you don't
> have to worry about the fact that func1() is const.
>
> /Leigh


Thanks,
John
 
Reply With Quote
 
John H.
Guest
Posts: n/a
 
      04-14-2010
On Apr 14, 1:42*pm, John <gh14...@yahoo.com> wrote:
> do you recommend making all member functions
> static that do not change the state of a class?


First you use the word "class" when I think you may have ment
"object". Think of class as "the cookie cutter" and object as "the
cookie".
Second, just because a member function does not change the state of an
*object* does not mean it can be static.
Here is a general rule of thumb you could try:
- Member functions that do not use (read or write) the state of the
object can be static. (There is no "the object")
- Member functions that want to know, but not change, the state of the
object can be const.
- Member functions that need to change the state of the object should
be non-static non-const.

Example pseudocode:

class Color
{
private:
int red;
int green;
int blue;
public:
Color(int red, int green, int blue) :
red(red),
green(green),
blue(blue)
{
}

// neither const nor static because it modifies member variables
void set_color(int red, int green, int blue)
{
this->red = red;
this->green = green;
this->blue = blue;
}

// const because it only reads from member variables
void get_color(int & red, int & green, int & blue) const
{
red = this->red;
green = this->green;
blue = this->blue;
}

// static because it doesn't use any non-static functions or
variables
// i.e. it is related to the Color concept, but not to any
existing Color object
static Color get_black()
{
return Color(0,0,0);
}
};

Now, there are things called "static member variables". These are not
part of any particular object, although you might think of them as
part of "the state of the *class*". These can be read/written by
static member functions, const member functions, and non-const/non-
static member functions. Also (depending on the access) these can be
read/written by non-member functions and member functions from other
classes.

Also one more clarification for static member functions: when I saw
that they "can't use non-static member functions or variables", by
this I mean that they cannot use such things directly (because there
is no "this" object). They can however use such things indirectly,
i.e. through an object of the class. For instance our Color class
might have another static member function called get_white:

static Color get_white()
{
// For demonstration purposes here, assume Color
// has only a default constructor:
Color c;
c.set_color(255, 255, 255); // ok to call set_color because it is
indirect
return c;
}

The same thing with const member functions. They can modify the state
of other objects, just not there own. e.g.:

bool Color::isBlackOrWhite() const
{
// For this assume I have an equality operator defined
Color compare_color = make_black();
if(*this == compare_color)
{
return true;
}
compare_color.set_color(255, 255, 255); // ok to call non-const
function because it is not us
return *this == compare_color;
}
 
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 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
Overriding const member function with non-const David Scarlett C++ 3 02-07-2006 01:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Function parameter in: "void __cdecl Function(class Class1 *,float const (* const)[4]);" Polanski24 C++ 1 04-11-2005 12:48 PM



Advertisments