C++: Overloading operators and references.

Started by
11 comments, last by Zakwayda 14 years, 4 months ago
The conventional signature for an overloaded assignment operator usually looks like this
My_class& operator=(const My_class& other) 

instead of returning nothing (void), you can return a reference to the current object, to allow chaning of the = operator, e.g. a=b=c=d;
   member_a=other.member_a;   member_b=other.member_b;   return *this; 



As for the + operator, you could consider changing that to pass-by-const-reference too (It won't be modifying the existing object so it can be a const member function)
 My_class operator+(const My_class& other) const


const correctness is a good thing to get right from the outset, although if you're already in the middle of a project (especially one which hasn't been designed with it in mind), trying to add const all over the place to existing classes can sometimes be a headache.
Advertisement
Hi:

nobodynews, you absolutely have the point there... As I will comment later on this post, I ended up with the "snowball" effect and soon I will be done with my particular implementation of the wheel so I can start new with something more standard... Actually, now that I think about it I started using this - buggy - implementation of a string because I had been working with it lately and felt familiar with it. By the way, memory leaks appeared again in the addition operator, shall take a look at it shortly.

Bench, I ended up going to bed at 2:00am yesterday just because "const correctness" (not a good thing to say about someone responsible at work but well, the ocassion deserved it). I happened to be more and more interested and started to evaluate my header files and considering "constness" for each single thing that shouldn't change... As you may imagine, one constant reference led to other, and other and other and ended up snowballing all around my code.

I wonder, is that a good thing at all?. I was forced to use the const_cast in a few times at the end of the chain because the SDL library doesn't have a prototype for SDL_BlitSurface(const SDL_Surface * [...]) but for (SDL_Surface * [...]) and I am not sure at all about all those "const" and "&" around my code.

Perhaps I should talk a bit about how I think it works... Let's see I am working at the graphic functions right now. I consider a few "entities":

-> Resources, surfaces loaded with an image and also a string with the file name.
-> Drawing blocks -> Structures with a pointer to a resource (what to draw) and to other SDL structures that point "Where" to draw (X, Y...). Also some other values for "draw depth" and offsetting of the resource.
-> The screen -> The entity that takes all the drawing blocks and puts them on the screen.

It's actually a bit larger than that, but that should do it.

So, my resources have to be passed to the drawing blocks. Since I don't want the drawing block to modify the original resource in any way the resource just returns a constant pointer to its surface (SDL Surfaces work with pointers...).
Same with the filename, of course.

Then, my drawing blocks had to be modified so they take a constant resource on their constructor... Some stuff I had to access into the resource when building the block had also to be made "const" - no changes made to the data - so I could call it. I also had to modify some code inside the blocks themselves since I wanted them to be passed to the screen as "const" too.

And of course, the screen has to get the blocks as constants since I don't want the screen to modify anything, just to draw. Here it came the const_cast bit: at the end of the chain, the original pointer to an SDL_Surface in the resource has become constant and SDL won't allow them as parameters so, in the end of the chain I have to cast them and pray the SDL does nothing to them (why this does seem so wrong?).

So, well, there are more and more ramifications of this around my code (I just copied and pasted the project so I woudn't break everything) and I am wondering if I am doing this right. So far I am trying to "normalize" the parameters to my methods so they take constant references (or references where there's no need for a constant) and also tracing the chain of my code to determine what things shouldn't be changed and make those constant references. Am I right or wrong about this?. Honestly, I am so absolutely new to all this constness and I would like to know more about it.

Thanks a lot.
Quote:I wonder, is that a good thing at all?. I was forced to use the const_cast in a few times at the end of the chain because the SDL library doesn't have a prototype for SDL_BlitSurface(const SDL_Surface * [...]) but for (SDL_Surface * [...]) and I am not sure at all about all those "const" and "&" around my code.
Yes, const-correctness is worth the trouble (IMO). And yes, if you have a large project that isn't consistently const-correct and you decide you want to make it const-correct, you can end up with a domino effect that can take a while to sort out. And yes, sometimes you may have to use const_cast to interoperate with third-party libraries that are not const-correct themselves (IMX, at least).

This topic is closed to new replies.

Advertisement