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!
 
±

±ÇÌéÍõ×Ó

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;
}
 
J

Jonathan Turkanis

±ÇÌéÍõ×Ó said:
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 ignored
const. At any rate, in standard C++, const can only apply to non-static member
functions.

Jonathan
 
D

Donovan Rebbechi

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,
 
R

rajkumar

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
 
K

Kurt Stutsman

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());
}
 
D

Donovan Rebbechi

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 ``concatenate
"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 support
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,
 
R

rajkumar

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
 

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

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top