By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 446,189 Members | 904 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,189 IT Pros & Developers. It's quick & easy.

# operator presence?

 P: n/a See this example: class ds { public: ds() {dat[0]='\0';} ds(int i) {sprintf(dat, "%d", i);} ds(ds& s) {strcpy(dat, s.dat);} operator int(){ int i = atoi(dat); return i; } char dat[50]; }; ds operator+(ds& a, ds& b) {ds c(a); strcat(c.dat, b.dat); return c;} ds operator+(int a, ds& b) {ds c(a); strcat(ds(c).dat, b.dat); return c;} ds operator+(ds& a, int b) {ds c(a); strcat(c.dat, ds(b).dat); return c;} int main(int char**) { ds a(1),b(2),c(3); a = b+c+7; } first a+b = "23". That's fine. Now "23"+7, the "23" get's cased to an (int) before adding. How can I make the adding of +(ds, int) happen first? -- -Gernot int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);} ________________________________________ Looking for a good game? Do it yourself! GLBasic - you can do www.GLBasic.com Jul 22 '05 #1
Share this Question
11 Replies

 P: n/a "Gernot Frisch" wrote in message news:2n***********@uni-berlin.de... See this example: class ds { public: ds() {dat[0]='\0';} ds(int i) {sprintf(dat, "%d", i);} ds(ds& s) {strcpy(dat, s.dat);} operator int(){ int i = atoi(dat); return i; } char dat[50]; }; ds operator+(ds& a, ds& b) {ds c(a); strcat(c.dat, b.dat); return c;} ds operator+(int a, ds& b) {ds c(a); strcat(ds(c).dat, b.dat); return c;} ds operator+(ds& a, int b) {ds c(a); strcat(c.dat, ds(b).dat); return c;} int main(int char**) { ds a(1),b(2),c(3); a = b+c+7; } first a+b = "23". That's fine. Now "23"+7, the "23" get's cased to an (int) before adding. How can I make the adding of +(ds, int) happen first? Like this a = b+(c+7); but really you need to add some const's to your code. ds(const ds& s) {strcpy(dat, s.dat);} operator int() const { ds operator+(const ds& a, const ds& b) {ds c(a); strcat(c.dat, b.dat); return c;} etc. etc. If you do this throughout then casting problem will probably solve itself. (hard to say for certain since you are working with a compiler that only approximates C++). John Jul 22 '05 #2

 P: n/a Gernot Frisch wrote: class ds { public: ds() {dat[0]='\0';} ds(int i) {sprintf(dat, "%d", i);} ds(ds& s) {strcpy(dat, s.dat);} operator int(){ int i = atoi(dat); return i; } char dat[50]; }; ds operator+(ds& a, ds& b) {ds c(a); strcat(c.dat, b.dat); return c;} ds operator+(int a, ds& b) {ds c(a); strcat(ds(c).dat, b.dat); return c;} ds operator+(ds& a, int b) {ds c(a); strcat(c.dat, ds(b).dat); return c;} int main(int char**) { ds a(1),b(2),c(3); a = b+c+7; } first a+b = "23". That's fine. Now "23"+7, the "23" get's cased to an (int) before adding. How can I make the adding of +(ds, int) happen first? Makes the ds & paramaters of the operators const. Make const the operators itself. The result of a + b is a temporary, a temporary cannot be used where a non const reference is expected. -- Salu2 Jul 22 '05 #3

 P: n/a > but really you need to add some const's to your code. ds(const ds& s) {strcpy(dat, s.dat);} operator int() const { ds operator+(const ds& a, const ds& b) {ds c(a); strcat(c.dat, b.dat); return c;} Oh, OK. I'll do that. I use MSVC for debugging (7.1), but GCC for the final project. Thank you, Gernot (really in despair) If anyone is interested, I want to have a (fast) class set that allows casting from numbers (variables + constants) to strings and vice versa, also alowing this: cs("A") + some_string + 5 + cs("!") become: "A five 5 !" Jul 22 '05 #4

 P: n/a Gernot Frisch wrote: See this example: class ds { public: ds() {dat[0]='\0';} ds(int i) {sprintf(dat, "%d", i);} ds(ds& s) {strcpy(dat, s.dat);} ^ const operator int(){ int i = atoi(dat); return i; } char dat[50]; }; ds operator+(ds& a, ds& b) {ds c(a); strcat(c.dat, b.dat); return c;} ^ ^ const const ds operator+(int a, ds& b) {ds c(a); strcat(ds(c).dat, b.dat); return ^ const c;} ds operator+(ds& a, int b) {ds c(a); strcat(c.dat, ds(b).dat); return ^ const c;} int main(int char**) ^ , { ds a(1),b(2),c(3); a = b+c+7; } first a+b = "23". That's fine. Now "23"+7, the "23" get's cased to an (int) before adding. You mean, 'b+c' = "23", don't you? Well, in fact, it's not "23". It is a 'ds', whose 'dat' contains { '2','3', 0 ... }. How can I make the adding of +(ds, int) happen first? What do you mean by that? You defined the operator+(ds&,int) to perform the concatenation, just like all others. The result of 'b+c+7' should be a 'ds' object that contains "237" in its 'dat' array. Isn't that what happens? Isn't that what you _wanted_ to happen? If not, what is it you wanted to happen when an 'int' is added to a 'ds'? Victor Jul 22 '05 #5

 P: n/a "Victor Bazarov" schrieb im Newsbeitrag news:b1***************@newsread1.dllstx09.us.to.ve rio.net... Gernot Frisch wrote: See this example: class ds { public: ds() {dat[0]='\0';} ds(int i) {sprintf(dat, "%d", i);} ds(ds& s) {strcpy(dat, s.dat);} ^ const operator int(){ int i = atoi(dat); return i; } char dat[50]; }; ds operator+(ds& a, ds& b) {ds c(a); strcat(c.dat, b.dat); return c;} ^ ^ const const ds operator+(int a, ds& b) {ds c(a); strcat(ds(c).dat, b.dat); return ^ const c;} ds operator+(ds& a, int b) {ds c(a); strcat(c.dat, ds(b).dat); return ^ const c;} int main(int char**) ^ , { ds a(1),b(2),c(3); a = b+c+7; } first a+b = "23". That's fine. Now "23"+7, the "23" get's cased to an (int) before adding. You mean, 'b+c' = "23", don't you? Well, in fact, it's not "23". It is a 'ds', whose 'dat' contains { '2','3', 0 ... }. >How can I make the adding of +(ds, int) happen first? What do you mean by that? You defined the operator+(ds&,int) to perform the concatenation, just like all others. The result of 'b+c+7' should be a 'ds' object that contains "237" in its 'dat' array. Isn't that what happens? Isn't that what you _wanted_ to happen? If not, what is it you wanted to happen when an 'int' is added to a 'ds'? It's not "237" but "30". I think the "const" could help me out... Victor Jul 22 '05 #6

 P: n/a Now it's almost good: class ds { public: ds() {dat=new char[50]; dat[0]='\0';} ds(int i) {dat=new char[50]; sprintf(dat, "%d", i);} ds(const ds& s) {dat=new char[50]; strcpy(dat, s.dat);} ~ds() {if (dat) delete[] dat; dat=NULL;} ds& operator=(const ds& s) {if (dat) delete[] dat; dat=new char[50]; strcpy(dat, s.dat); return *this;} operator int()const {int i = atoi(dat);return i;} char* dat; }; ds operator+(const ds& a, const ds& b) {ds c(a); strcat(c.dat, b.dat); return c;} ds operator+(int a, const ds& b) {ds c(a); strcat(ds(c).dat, b.dat); return c;} ds operator+(const ds& a, int b) {ds c(a); strcat(c.dat, ds(b).dat); return c;} int main(int, char**) { ds a(1),b(2),c(3); a = b+c+7; // 237 now - yehaa! a = 2; // good b = 1+a+3; // b = "13" - why this?? } Jul 22 '05 #7

 P: n/a Silly me! Forget it. Thank you all sooo much! ds operator+(int a, const ds& b) {ds c(a); strcat(ds(c).dat, b.dat); Jul 22 '05 #8

 P: n/a "Gernot Frisch" wrote in message news:2n***********@uni-berlin.de... but really you need to add some const's to your code. ds(const ds& s) {strcpy(dat, s.dat);} operator int() const { ds operator+(const ds& a, const ds& b) {ds c(a); strcat(c.dat, b.dat); return c;} Oh, OK. I'll do that. I use MSVC for debugging (7.1), but GCC for the final project. My mistake I assumed VC++ 6 which lets you get away without const in all sorts of situations where it is required. VC++ 7.1 is much better provided you turn on the "disable language extensions" option. const is really important in C++. Its easy to ignore it as a newbie, but sooner or later lack of const-correctness will get you. Its good to get into the habit of thinking about const for every piece of C++ code you write. john Jul 22 '05 #9

 P: n/a > const is really important in C++. Its easy to ignore it as a newbie, but sooner or later lack of const-correctness will get you. Its good to get into the habit of thinking about const for every piece of C++ code you write. Do you know any good source for learnig where to put const and where not? I come form VC6, so I've not used const a lot, yet... Thank you - it's working so great now! -Gernot Jul 22 '05 #10

 P: n/a Gernot Frisch wrote: Do you know any good source for learnig where to put const and where not? I come form VC6, so I've not used const a lot, yet... A simple rule is: if you do not modify it, make it const. -- Salu2 Jul 22 '05 #11

 P: n/a "Gernot Frisch" wrote in message news:2n************@uni-berlin.de... const is really important in C++. Its easy to ignore it as a newbie, but sooner or later lack of const-correctness will get you. Its good to get into the habit of thinking about const for every piece of C++ code you write. Do you know any good source for learnig where to put const and where not? I come form VC6, so I've not used const a lot, yet... Thank you - it's working so great now! -Gernot The FAQ has a whole section http://www.parashift.com/c++-faq-lit...rrectness.html I think most of the cases can be covered in a few simple rules 1) When you write a method think whether the method modifies the object, if it never does declare it const. class Widget { public: int get_price() const { return price; } void set_price(int new_price) { price = new_price; } private: int price; }; get_price is const, but set_price isn't (and if you tried to make it const you would get a compiler error). 2) When you declare a reference, consider whether you will ever use that reference to modify the object it refers to, if not then declare it const. 'Modify the object it refers to' translates in language terms as call any of the methods that you thought about in step 1 but decided not to declare const. void copy_price(Widget& x, const Widget& y) { x.set_price(y.get_price()); } x calls a non-const method so isn't const, but y only calls const methods so is declared const. 3) As step 2 but for pointers not references. The reason for all this is the rule that you cannot bind a non-const reference to a temporary object (that is the rule that VC++ 6 does not enforce). And it is that rule that was causing your compiler to prefer casting b+c to an int before adding it to 7. It couldn't do what you wanted because the result of b+c is a temporary and you had declared operator+ with non-const references. john Jul 22 '05 #12

### This discussion thread is closed

Replies have been disabled for this discussion.