Skip to main content
GameDev.net gamedev.net
🔒 Locked

inline a=b+c

Started by gsus Feb 21, 2005 at 7:54 AM 3 replies 950+ views
Original Post
gsus
gsus
Hi, im implementing a template class for image processing. I've overloaded operator+ template Image& Image::operator+ (const Image& img2){ Image ret(xdim, ydim); int i = 0; for(i=0;i<(ydim*xdim);i++){ ret.data = data + img2.data; } return ret; } but when i want to avaluate a = b+c this overlaoded funtion first needs to create image to store the sum. How can i store the sums direktrly in 'a'. A workaround woult be use somting like a.sum(b,c). tnx gsus
MaulingMonkey
MaulingMonkey
DANGER, WILL ROBSIN!!!

Quote:
Original post by Anonymous Poster
By the way, your operator + is bugged - you are returning a reference to a local variable. The variable ret will be destroyed when it goes out of scope, meaning the reference you returned will not be valid.


*points up* what he said. You need to return by value, or smart pointer of some sort, or handle class, or the like.
gsus
gsus
oh...yes tnx.. the reference still there cos i've tried to tell the compiler to work on the 'a'.
I think ill use the a.sum(b,c) syntax. its not so nice an flexible, and i dont have the time to try expression templates. i thourght theres an easy way...

tnx

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.