operator presence?

G

Gernot Frisch

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
 
J

John Harrison

Gernot Frisch said:
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
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Gernot said:
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.
 
V

Victor Bazarov

Gernot said:
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
 
G

Gernot Frisch

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 !"
 
G

Gernot Frisch

Victor Bazarov said:
Gernot said:
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...
 
G

Gernot Frisch

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??
}
 
G

Gernot Frisch

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
 
J

John Harrison

Gernot Frisch said:
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
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Gernot said:
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.
 
J

John Harrison

Gernot Frisch said:
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-lite/const-correctness.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
 

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

Similar Threads

why does it crash? 5
operator + without = 2
reference to parent inherritance? 9
operator overloading... 5
cast operator for built in to class? 5
string concatenation 4
unreachable code? 4
int(a) vs (int)a 19

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top