46
General discussions / Re: [Video Tutorial] Arkanoid in 160 lines - C++11 + SFML2
« on: November 25, 2013, 08:06:37 am »
Everything Nexus said is correct, just wanted to reiterate and show more examples.
Please correct me if my example above contains any mistake.
Basically, there are occasions when the compiler can automatically "guess" the type, and it's not necessary to specify it. Example:
Very interesting link: http://programmers.stackexchange.com/questions/133688/is-c11-uniform-initialization-a-replacement-for-the-old-style-syntax
"constexpr" its the same than "#define" but more oriented to define variables ?
constexpr int arraySize{15};
int array[arraySize]; // Valid C++11 code, arraySize is a constexpr
constexpr int getArraySize(int x) { return 15 + x; }
int array2[getArraySize(15)]; // Valid C++11, getArraySize(15) can be evaluated at compile-time
int main() {
int temp;
cin >> temp;
int array3[getArraySize(temp)]; // Unvalid code, getArraySize(temp) cannot be evaluated at compile-time
cout << getArraySize(temp) << endl; // Valid code, but getArraySize(temp) will be evaluated at run-time
}
int array[arraySize]; // Valid C++11 code, arraySize is a constexpr
constexpr int getArraySize(int x) { return 15 + x; }
int array2[getArraySize(15)]; // Valid C++11, getArraySize(15) can be evaluated at compile-time
int main() {
int temp;
cin >> temp;
int array3[getArraySize(temp)]; // Unvalid code, getArraySize(temp) cannot be evaluated at compile-time
cout << getArraySize(temp) << endl; // Valid code, but getArraySize(temp) will be evaluated at run-time
}
Please correct me if my example above contains any mistake.
aaaand.... I don't understand how this code:shape.setSize({paddleWidth, paddleHeight});
is equivalent to this one:shape.setSize(Vector2f(paddleWidth, paddleHeight));
How exactly works the initialization with { } ? Mmm
Basically, there are occasions when the compiler can automatically "guess" the type, and it's not necessary to specify it. Example:
Vector2f getMyVector1() { return Vector2f(5.f, 5.f); }
Vector2f getMyVector2() { return {5.f, 5.f}; }
// The two functions are practically equivalent
Vector2f getMyVector2() { return {5.f, 5.f}; }
// The two functions are practically equivalent
Very interesting link: http://programmers.stackexchange.com/questions/133688/is-c11-uniform-initialization-a-replacement-for-the-old-style-syntax