tags:

views:

235

answers:

5

Alternative 1, reusing a temporary variable:

Sticker sticker;
sticker.x = x + foreground.x;
sticker.y = foreground.y;
sticker.width = foreground.width;
sticker.height = foreground.height;
board.push_back(sticker);

sticker.x = x + outline.x;
sticker.y = outline.y;
sticker.width = outline.width;
sticker.height = outline.height;
board.push_back(sticker);

Alternative 2, scoping the temporary variable:

{
 Sticker sticker;
 sticker.x = x + foreground.x;
 sticker.y = foreground.y;
 sticker.width = foreground.width;
 sticker.height = foreground.height;
 board.push_back(sticker);
}

{
 Sticker sticker;
 sticker.x = x + outline.x;
 sticker.y = outline.y;
 sticker.width = outline.width;
 sticker.height = outline.height;
 board.push_back(sticker);
}

Alternative 3, writing straight to the vector memory:

{
 board.push_back(Sticker());
 Sticker &sticker = board.back();
 sticker.x = x + foreground.x;
 sticker.y = foreground.y;
 sticker.width = foreground.width;
 sticker.height = foreground.height;
}

{
 board.push_back(Sticker());
 Sticker &sticker = board.back();
 sticker.x = x + outline.x;
 sticker.y = outline.y;
 sticker.width = outline.width;
 sticker.height = outline.height;
}

Which approach do you prefer?

Edit: For the sake of this discussion, assume that the assignments have to be made one by one outside of a constructor

+14  A: 

My option - give Sticker a constructor that takes the parameters. then:

board.push_back( Sticker( outline.x, foo.bar, etc. ) );  

Edit: Code to illustrate constructor parameter names:

#include <iostream>
using namespace std;

struct S {
    int a, b;
    S( int a, int b ) : a(a), b(b) {
    }
};

int main() {    
    S s( 1, 2);
    cout << s.a << " " << s.b << endl;
}
anon
My suggestion as well, but I'd prefer `board.push_back(Sticker(outline, x)); board.push_back(Sticker(foreground, x))` rather than passing each piece one by one.
Joseph Garvin
Ya, add an init list inside and voila'
Maciek
What would you call your constructor parameters, since they ideally should have the same names as the class members?
henle
@henle Why would they ideally have the same names? But you can in fact give them the same names. I've posted some code to illustrate this.
anon
I guess x and y are ideal names for coordinates. But if you can give you the same names, then all is swell!
henle
@henle For that you have to initialize the members in the *initialization list* of the constructor (as in the example). Using that syntax is unambiguous and the compiler can figure out what you want to do. Doing it in the body of the CTor would only assign the parameters to themselves because then they hide the member variables.
mxp
@mxp: unless you explicitly use the `this` keyword to reference them `this->x = x` but anyway using the initialization list is better.
Matthieu M.
A: 

Alternative 1. Why create a scope just for a variable? There is usually an enclosing scope nearby (at the minimum, you should keep your functions/procedures small so that will scope it).

Why? You can create a shorter variable name e.g. st in this case. Since the assignment will be nearby there should be no loss in clarity. Actually it will look simpler and cleaner.

Also, if the vector needs to be dereferenced/accessed from several other levels of indirection, then it will also simplify the code.

Larry Watanabe
Really? I don't mind the verbosity that much. Regarding scoping: I would think it was easier for a compiler to optimize the temporary away if it is writing to a brand new variable.
henle
@henle Don't care about the compiler. These are very advanced pieces of software (just think about how long C++ exists) and can figure out quite a lot of stuff. Scoping is for you so you don't make mistakes.
mxp
@mxp I **do** care about the compiler
henle
@henle Don't :-) Sounds like you're trying to optimize where not necessary. Try measuring the differences between "normal" and "supposedly optimized". It often leads to the insight, that the compiler can do very well on its own.
mxp
+2  A: 

board.resize(sticker_count);

Then iterate through all the vector and set parameters.

Nailer
A: 

How about winforms style:

// Class members

Sticker sticker1;
Sticker sticker2;
Board board;

// Initialization 

void InitBoard()
{
    sticker1.x = x + foreground.x;
    sticker1.y = foreground.y;
    sticker1.width = foreground.width;
    sticker1.height = foreground.height;

    sticker2.x = x + outline.x;
    sticker2.y = outline.y;
    sticker2.width = outline.width;
    sticker2.height = outline.height;

    // Add to board
    board.push_back(sticker1);
    board.push_back(sticker2);
}
AareP
+1  A: 

You can find more details about temporary objects here.

Julio Guerra