CString A;
A = "Hello";
A = A + "Hello";
and it works! why is that? does the compiler create a seperate CString and construct it using "Hello", then assign tempstring to A? Just wondering, cuz this is kindof neat!
farmersckn
Overloaded operator question.
I have my own string class, and it has constructors to create from a char, char *, and another string. It also has assignment op overloaded, for a string. However, I can do this, too, for some reason:
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
if you post the rest of the code for the class then i might be able to tell you why
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions
If you put it in quotes like that the compiler takes it as a constant char string. If you dont have the operator overloaded to support this, the compiler "guesses" what you might want it to do. Sometimes it''s right, sometimes it''s not. a+b might mean strawberries plus bannana''s to one person, and lion''s plus elephants to the next.
p.s. excuse the weird analogy :-)
http://www.animerave.com
p.s. excuse the weird analogy :-)
http://www.animerave.com
October 22, 2000 11:06 PM
quote: Original post by farmersckn
I have my own string class, and it has constructors to create from a char, char *, and another string. It also has assignment op overloaded, for a string. However, I can do this, too, for some reason:
CString A;A = "Hello";A = A + "Hello";
and it works! why is that? does the compiler create a seperate CString and construct it using "Hello", then assign tempstring to A? Just wondering, cuz this is kindof neat!
farmersckn
It works because the compiler is allowed to use your defined constructors to convert data types. You indicate you have a constructor for char*, so the compiler can convert the literal "Hello" into a temporary CString object containing that string.
If you don''t want to allow the compiler that freedom, use the keyword explicit with the constructor. That will allow you to create instances of CString only when you specify.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement