This code handles text + const char* :
but how can I handle a const char* + text expression ?Code:text& text::operator+ (const char* c)
{
//code
}
Printable View
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 typeQuote:
Originally 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).
Correct me if I'm wrong...Quote:
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.Quote:
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 ?Quote:
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 :Quote:
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....Quote:
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 :Quote:
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.Quote:
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.Quote:
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 leaksQuote:
Originally 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 wayQuote:
Originally 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 :Quote:
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 ?Quote:
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?
Why can't you just return a reference if that's effecient?Quote:
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).
Because if you return a HOLE object you actually copy the hole thing while if you only return a reference you just copy the adress.
Correct - it isnt as efficient, but when you return a reference, the objects you are refering to will have its destructor called before you have the oportunity to use it (it goes out of scope), therefore your code is refering to an object that no longer exists - a sure way to mess things up!!Quote:
Originally posted by Zahl
Why can't you just return a reference if that's effecient?
Because if you return a HOLE object you actually copy the hole thing while if you only return a reference you just copy the adress.
ok so we decided that if you were to return a reference u would have to use new within the function. So why is that not ok. Consider this.....
now what happens when we add these text objects together.Code:text& operator + (const text& Text1,const text& Text2)
{
text* NewText=new text;
// concat Text1 and Text2
return *NewText;
}
How many temporary objects are created in that last expression and who called delete on the newed memory they contain??Code:text T1("foo");
text T2("bar");
text T3("wont");
text T4("do");
text T5;
T5=T1+T2+T3+T4;
That is why operator + MUST return an object. A reference is never suitable.