Home arrow C++ Programming arrow C++ Pitfalls

Language Translator

Hacking Zone

Hacking Tools
Attacking

Configure Windows

Windows Configuration

Novels

Mix Novels

Human Personality

Body Language
C++ Pitfalls PDF Print E-mail
Written by Scott Wheeler   
Thursday, 10 January 2008
Article Index
C++ Pitfalls
Page 2
Page 3
Page 4
Page 5
Page 6
 

const Return Values

We saw in the above example the methods cWidget() where our type had a const prepended. In that position in the value that is returned may not be modified.

Consider (using the above class):

int main()
{
Foo f;

Widget *w1 = f.widget(); // fine

Widget *w2 = f.cWidget(); // ERROR - "cWidget()"
// returns a const value
// and "w2" is not const

const Widget *w3 = f.cWidget(); // fine

return 0;
}

So, if we are using a method with a const return value we must assign the value to a const local variable.

If such a const return value is a pointer or a reference to a class then we cannot call non-const methods on that pointer or reference since that would break our agreement not to change it.

 

Note: As a general rule methods should be const except when it's not possible to make them such. While getting used to the semantics you can use the compiler to inform you when a method may not be const -- it will give an error if you declare a method const that needs to be non-const.

 

const Function Arguments

The keyword const can also be used as a guarantee that a function will not modify a value that is passed in. This is really only useful for references and pointers (and not things passed by value), though there's nothing syntactically to prevent the use of const for arguments passed by value.

Take for example the following fuctions:

void foo( const std::string &s )
{
s.append("blah"); // ERROR -- we can't modify the string

std::cout << s.length() << std::endl; // fine
}

void bar( const Widget *w )
{
w->rotate(); // ERROR - rotate wouldn't be const

std::cout << w->name() << std::endl; // fine
}

In the first example we tried to call a non-const method -- append() -- on an argument passed as a const reference, thus breaking our agreement with the caller not to modify it and the compiler will give us an error.

The same is true with rotate(), but with a const pointer in the second example.



Last Updated ( Thursday, 10 January 2008 )
 
< Prev   Next >
Your Ad Here

Donate us!!

Enter Amount:

RSS socialnet

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits