Re: How to use operator overloading?
"Immortal Nephi" <Immortal_Nephi@hotmail.com> ha scritto nel messaggio
news:3bb94404-78df-4ea4-8e48-8399574443e4@34g2000yqp.googlegroups.com...
On Jan 16, 7:30 pm, Immortal Nephi <Immortal_Ne...@hotmail.com> wrote:
On Jan 16, 7:05 pm, "osmium" <r124c4u...@comcast.net> wrote:
"Immortal Nephi" wrote:
<elided stuff>
test& operator+( const test &r )
{
return *this;
}
};
Shouldn't the code for a function that adds have a '+' someplace in the
code?
Is there an extra brace? Please post code that you compile, not simply code
produced by the same set of fingers. We call it "cut and paste".
test& operator+( const test &r )
{
return *this;
}
};
I am asking. How can left object and right object be prevented from
doing addition. Only "left object += right object" and "left object =
left object + right object" are allowed.
Look at operator+ function below. I don't know how to write my code
in the operator+ function body. I do know how to do addition.
test& operator+=( const test &r )
{
this->x += r.x;
i think you can replace above with "x=r.x"
return *this;
}
test& operator+( const test &r )
{
this->x += r.x; // How to prevent addition?
i think you can replace above with "x=r.x"
return *this;
}
Another example does the same to string like below.
string s1 = "A", s2 = "B";
s1 + s2; // prevent from doing addition
s1 += s2; // s1 = "AB"
s1 = s1 + s2; // s1 = "ABB"
I hope I can explain clear. Thanks...- Hide quoted text -
- Show quoted text -
I checked C++ Tutorial about operator+. The operator+ passes data by
value and operator+= passes data by reference.
#i never used that type& operator +(type&)
#don't know its pratical use (it seems not like my compiler too)
#the only 2 ones operator+ i use are
#type& operator+ () for "a=+b" should be used in "+b"
#and type& operator+(type& a, type& b); for "a=b+c" used in "b+c"
It does answer my
question about "s1 + s2;" instead of "s1 = s1 + s2;" I understand
now.
#don't know
It is all I have now. Thanks...
--------------------------
#this is my try
#include <iostream>
#include <cstdlib>
#include <cstdio>
// #include <vector>
#include <string>
#include <stdint.h>
#define u8 uint8_t
#define i8 int8_t
#define u32 uint32_t
#define i32 int32_t
// float 32 bits
#define f32 float
#define S sizeof
#define R return
#define P printf
#define F for
#define G goto
using namespace std;
class test;
class test
{public:
string s;
test() { s=""; }
test(i8* a) { s=a; }
~test() { }
// this is lile v=r
test& operator= (test& r){s =r.s; R *this;}
// don't know what it mean
// test& operator+ (test& r){R *this;}
// a=+b it is used in +b
test& operator+ (){R *this;}
// this is in a+=b
test& operator+=(test& r){s+=r.s; R *this;}
// this calculate a+b and return its result
// in the vector array
// i can not write it withot the use of "friend"
// don't know why
// NB.
// here
// one expression can have Max 16 level of ()
// for type class test
friend test& operator+(test& a, test& b);
// this for print in stdout
friend ostream& operator<<(ostream& ost, test& b)
{cout << b.s << flush; R ost;}
};
// array of the results
u32 index=0;
test v[16]; // 0..15
test& operator+(test& a, test& b)
{u32 u=index;
v[u].s=a.s+b.s;
++index; if(index==16) index=0;
R v[u];
}
int main(void)
{test t("ts_"), t2("t2s_"), t3("t3s_");
int x;
// t + t2; // ???
t = t + t2;
cout << t << "\n";
return 0;
}