trying to learn: const

G

Gianni Mariani

what is the meaning of 2 *const* in following line?

I suspect you mean somthing like this because what you have above is not
standard C++.

struct MyClass
{
const MyClass MyFunction (MyClass &a) const;
};


The first const is saying that the value returned is a const - i.e. it
may not be changed. The second const indicates the constness of "this".
 
M

Martin Magnusson

what is the meaning of 2 *const* in following line?

Is that legal code? If it were

const MyClass* MyFunction( MyClass &a ) const;

the last const would mean that MyFunction can't change any of the fields
(member variables) of MyClass, and the first const would mean that
MyFunction returns a constant pointer to a MyClass object -- that is,
the returned pointer cannot be used to change what it points to.

For more info on const, look at
http://www.possibility.com/Cpp/const.html and
http://www.parashift.com/c++-faq-lite/const-correctness.html

/ martin
 
R

Ron Natalie

Martin Magnusson said:
Is that legal code? If it were
No. The last const after the function name is not allowed.
The first const is OK, but sort of spurious since the return
value usually gets copied anyhow, it will lose the constness.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top