Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

non-member function can not have 'const' method qualifier?
Hi,guys!
I find an example from the book "Advanced C++ Programming Styles and
idoms" by James O.Coplien, but it fails to compile.

Code:

class String{
public:
friend String operator+ (const char*,const String&) const;
friend String operator+ (const String&,const char*) const;
..
};

Environment:
Dev C++ 4.9.9.1, Visual C++ 2005 Express Beta

When I deleted the last two const method qualifier, I passed the compile.

Any help is appreciated, thanks!







Report this thread to moderator Post Follow-up to this message
Old Post
鼻涕王子
02-23-05 08:58 AM


Re: non-member function can not have 'const' method qualifier?
String operator+(const String& S,const char *s) const {
String retval;
retval.rep = new char [ ::strlen(s) + S.length() ];
::strcpy(retval.rep, S.rep);
::strcat(retval.rep,s);
return retval;
}

String operator+(const char *s, const String& S) const {
String retval;
retval.rep = new char [ ::strlen(s) + S.length() ];
::strcpy(retval.rep,s);
::strcat(retval.rep,S.rep);
return retval;
}









Report this thread to moderator Post Follow-up to this message
Old Post
鼻涕王子
02-23-05 08:58 AM


Re: non-member function can not have 'const' method qualifier?
鼻涕王子 wrote:
> Hi,guys!
>   I find an example from the book "Advanced C++ Programming Styles and
> idoms" by James O.Coplien, but it fails to compile.
>
> Code:
>
> class String{
> public:
>  friend String operator+ (const char*,const String&) const;
>  friend String operator+ (const String&,const char*) const;
>  ...
> };
>
> Environment:
>   Dev C++ 4.9.9.1, Visual C++ 2005 Express Beta
>
>   When I deleted the last two const method qualifier, I passed the
> compile.
>
>   Any help is appreciated, thanks!

James Coplien's book covers pre-standard C++. I'm not sure if const ever had
 a
special meaning for non-member functions, or if some compilers simply ignore
d
const. At any rate, in standard C++, const can only apply to non-static memb
er
functions.

Jonathan



Report this thread to moderator Post Follow-up to this message
Old Post
Jonathan Turkanis
02-23-05 08:58 AM


Re: non-member function can not have 'const' method qualifier?
On 2005-02-23, 鼻涕王子 <me@privacy.net> wrote:
> Hi,guys!
>   I find an example from the book "Advanced C++ Programming Styles and
> idoms" by James O.Coplien, but it fails to compile.

Looks like a typo/error. The const qualifier isn't necessary (there's no
"this" pointer in the picture because it's not a member function)

>  friend String operator+ (const char*,const String&) const;

Ouch! I don't recommend this. One can always write String("foo") + x instead
,
which is much clearer.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/

Report this thread to moderator Post Follow-up to this message
Old Post
Donovan Rebbechi
02-23-05 08:58 AM


Re: non-member function can not have 'const' method qualifier?
For + operators I prefer the friend syntax.

Imagine a math complex class.

Complex a;

If friend you can do  "a+1" and "1+a" otherwise "1+a" would be illegal

Or is there a way to achieve this without making it a friend

Raj


Report this thread to moderator Post Follow-up to this message
Old Post
rajkumar@hotmail.com
02-23-05 08:59 AM


Re: non-member function can not have 'const' method qualifier?
rajkumar@hotmail.com wrote:
> For + operators I prefer the friend syntax.
>
> Imagine a math complex class.
>
> Complex a;
>
> If friend you can do  "a+1" and "1+a" otherwise "1+a" would be illegal
>
> Or is there a way to achieve this without making it a friend
>
> Raj
>
It's possible to do without making a friend if the class provides enough
publically accessible accessors to do the operation and create a new
value. For instance:

class complex {
private:
double m_real;
double m_imag;

public:
complex(const double& real, const double& imag) :
m_real(real), m_imag(imag)
{}

const double& real()const
{ return m_real; }

const double& imag()const
{ return m_imag; }
};

complex operator + (const complex& x, const complex& y)
{
return complex(x.real() + y.real(), x.imag() + y.imag());
}

Report this thread to moderator Post Follow-up to this message
Old Post
Kurt Stutsman
02-23-05 08:59 AM


Re: non-member function can not have 'const' method qualifier?
On 2005-02-23, rajkumar@hotmail.com <rajkumar@hotmail.com> wrote:
> For + operators I prefer the friend syntax.
>
> Imagine a math complex class.
>
> Complex a;
>
> If friend you can do  "a+1" and "1+a" otherwise "1+a" would be illegal
>
> Or is there a way to achieve this without making it a friend

return Complex(left) += right

But the main thing I don't like about writing "Foo" + s to mean ``concatenat
e
"Foo" and s'' is that to me, it smells like a thinly veiled type conversion.
The complex example is different because int and complex already both suppor
t
addition, and int supports addition with other types, so it does not violate
the expectations of the programmer to allow "promotion semantics" for mixed
type addition.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/

Report this thread to moderator Post Follow-up to this message
Old Post
Donovan Rebbechi
02-23-05 08:59 AM


Re: non-member function can not have 'const' method qualifier?
Good enough...

I misinterpreted your comment. When you said write 'String("foo") + x'
i thought you were against making it a non-member function. I agree
with you on the friend part.

Raj


Report this thread to moderator Post Follow-up to this message
Old Post
rajkumar@hotmail.com
02-23-05 09:00 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C++ archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:21 AM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.