Code Comments
Programming Forum and web based access to our favorite programming groups.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!
Post Follow-up to this messageString 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;
}
Post Follow-up to this message鼻涕王子 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
Post Follow-up to this messageOn 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/
Post Follow-up to this messageFor + 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
Post Follow-up to this messagerajkumar@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());
}
Post Follow-up to this messageOn 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/
Post Follow-up to this messageGood 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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.