Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > trying to learn: const

Reply
Thread Tools

trying to learn: const

 
 
Guest
Posts: n/a
 
      09-24-2003
what is the meaning of 2 *const* in following line?
-----------
const MyClass MyFunction const (MyClass &a);
-----------


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      09-24-2003
<- Chameleon -> wrote:
> what is the meaning of 2 *const* in following line?
> -----------
> const MyClass MyFunction const (MyClass &a);
> -----------


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

 
Reply With Quote
 
 
 
 
Martin Magnusson
Guest
Posts: n/a
 
      09-24-2003
<- Chameleon -> wrote:

> what is the meaning of 2 *const* in following line?
> -----------
> const MyClass MyFunction const (MyClass &a);
> -----------


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-lit...rrectness.html

/ martin


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-24-2003

"Martin Magnusson" <(E-Mail Removed)> wrote in message news:bkt109$qe5$(E-Mail Removed)...
> <- Chameleon -> wrote:
>
> > what is the meaning of 2 *const* in following line?
> > -----------
> > const MyClass MyFunction const (MyClass &a);
> > -----------

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



 
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 vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
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
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments