This code handles text + const char* :
but how can I handle a const char* + text expression ?Code:text& text::operator+ (const char* c) { //code }
This code handles text + const char* :
but how can I handle a const char* + text expression ?Code:text& text::operator+ (const char* c) { //code }
You need to overload the operator for + for that situation...you cant really do it for char* as you cant overload operators on a built in data typeOriginally posted by laasunde
This code handles text + const char* :
but how can I handle a const char* + text expression ?Code:text& text::operator+ (const char* c) { //code }
Code:text operator+ (const char* c,const text& t) { //code }
operator + must return an object an not a reference.
For a detailed explanation consult the bible part 1 (effective C++ by Scott Meyers) item 23 (Dont try to return a reference when you must return an object).
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Correct me if I'm wrong...operator + must return an object an not a reference.
I think so because if you creat the object in the function it should be destroyed aftert the function is executed.
Yes....if you return a reference, the object you are providing the reference to will be out of scope, and therefore effectively out of existance by the time you use it.Originally posted by ammar
Correct me if I'm wrong...
I think so because if you creat the object in the function it should be destroyed aftert the function is executed.
Are you saying this wont work ?Originally posted by Fordy
Yes....if you return a reference, the object you are providing the reference to will be out of scope, and therefore effectively out of existance by the time you use it.
Code:text& text::operator+ (const char* c) { text *obj = new text() //code return obj; }
That will work because the memory is dynamically allocated, but this is what you musn't do :Originally posted by laasunde
Are you saying this wont work ?
Code:text& text::operator+ (const char* c) { text *obj = new text() //code return obj; }
Code:text& text::operator+ (const char* c) { text obj; //code return obj; }
Firstly, no it wont as you are trying to return a pointer, when the func user will expect a reference....Originally posted by laasunde
Are you saying this wont work ?
Code:text& text::operator+ (const char* c) { text *obj = new text() //code return obj; }
Secondly, its an stupid way of doing things as the user will have to call delete to free that memory for a simply operator+ addition, and even more worryingly they wont have to the option to even do this as they wont be able to even access the pointer to actually call delete on it!
I didn't even see that, it should've been :Originally posted by Fordy
Firstly, no it wont as you are trying to return a pointer, when the func user will expect a reference....
Code:text& text::operator+ (const char* c) { text *obj = new text() //code return *obj; }
Sorry I forgot about the dereference operator.Originally posted by The Dog
I didn't even see that, it should've been :
Code:text& text::operator+ (const char* c) { text *obj = new text() //code return *obj; }
Your first point was a typo, the dereference operator should have been there.Originally posted by Fordy
Firstly, no it wont as you are trying to return a pointer, when the func user will expect a reference....
Secondly, its an stupid way of doing things as the user will have to call delete to free that memory for a simply operator+ addition, and even more worryingly they wont have to the option to even do this as they wont be able to even access the pointer to actually call delete on it!
Your second point, I see your point but how would solve the issue ?
Appreciate any response.
Cheers
It doesnt matter...you are still creating an opject on the heap that you have no real way of freeing with a call to delete..therefore you can expect some nasty memory leaksOriginally posted by laasunde
Your first point was a typo, the dereference operator should have been there.
Always return an object by value in this situation....its not the most efficient way, but its the safe wayOriginally posted by laasunde
Your second point, I see your point but how would solve the issue ?
So my overloading method should look something like this then :Originally posted by Fordy
It doesnt matter...you are still creating an opject on the heap that you have no real way of freeing with a call to delete..therefore you can expect some nasty memory leaks
Always return an object by value in this situation....its not the most efficient way, but its the safe way
Code:text text::operator+ (const char* c) { text obj; // return obj; }
Any good utillitys that can help me find possible memory leaks ?Originally posted by Fordy
It doesnt matter...you are still creating an opject on the heap that you have no real way of freeing with a call to delete..therefore you can expect some nasty memory leaks
[QUOTE Stoned_Coder ]
operator + must return an object an not a reference.
For a detailed explanation consult the bible part 1 (effective C++ by Scott Meyers) item 23 (Dont try to return a reference when you must return an object). [/QUOTE]
Why can't you just return a reference if that's effecient?
Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
Hell I might even learn something