| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"鼻涕王子" |
| Date: |
22 Feb 2005 07:22:55 PM |
| Object: |
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!
.
|
|
| User: "Donovan Rebbechi" |
|
| Title: Re: non-member function can not have 'const' method qualifier? |
22 Feb 2005 08:04:17 PM |
|
|
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/
.
|
|
|
| User: "" |
|
| Title: Re: non-member function can not have 'const' method qualifier? |
22 Feb 2005 11:10:24 PM |
|
|
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
.
|
|
|
| User: "Kurt Stutsman" |
|
| Title: Re: non-member function can not have 'const' method qualifier? |
22 Feb 2005 11:22:38 PM |
|
|
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());
}
.
|
|
|
| User: "" |
|
| Title: Re: non-member function can not have 'const' method qualifier? |
23 Feb 2005 10:01:55 AM |
|
|
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
.
|
|
|
|
|
| User: "Donovan Rebbechi" |
|
| Title: Re: non-member function can not have 'const' method qualifier? |
23 Feb 2005 12:04:47 AM |
|
|
On 2005-02-23, <> 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 ``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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
.
|
|
|
|
|
|
| User: "鼻涕王子" |
|
| Title: Re: non-member function can not have 'const' method qualifier? |
22 Feb 2005 07:52:03 PM |
|
|
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;
}
.
|
|
|
|
| User: "Jonathan Turkanis" |
|
| Title: Re: non-member function can not have 'const' method qualifier? |
22 Feb 2005 07:53:27 PM |
|
|
鼻涕王子 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 ignored
const. At any rate, in standard C++, const can only apply to non-static member
functions.
Jonathan
.
|
|
|
|

|
Related Articles |
|
|