A bit confused

Recommended Posts

Hi, quick questions

From my thinking writing this:

vector3 (float vx, float vy, float vz) : x (vx), y (vy), z (vz) { }

is the same as writing this:

vector3 (float vx, float vy, float vz)
{
x = vx; y = vy; z = vz;
}

What is the difference??

And can someone explain the ? to me please. Ive seen it written and from the looks of it it checks if an if statement is true or something but can someone give me an example please.

Thanks.

Share on other sites
The first expression uses a special shorthand notation that is available only to class constructors. The results are otherwise identacle.

Share on other sites
For that example, using an initializer list or assigning values to your members in the body of the constructor are essentially the same thing. However, if instead of float you have a type with non-trivial constructors, your members would be default-constructed before the body of the constructor is run, and then you would call the assignment operator.

In other words, it's similar to the difference between
  std::string greeting("Hello");

and
  std::string greeting;  greeting = "Hello";

If the type of the member doesn't have an assignment operator, or if it's const or if it's a reference, or if you are trying to specify how a base class should be constructed, you can only use initializer lists.

Share on other sites
Quote:
 What is the difference??

For something like a float,
vector3 (float vx, float vy, float vz) : x (vx), y (vy), z (vz) { }
vs
vector3 (float vx, float vy, float vz) {x = vx; y = vy; z = vz;}
should yield the same thing.

The big savings from the initializer list (first one) comes when 'x' isn't just a float. The reason is that your second example first default initializes all the member variables, THEN sets them to values inside the constructor.
C++ faq lite on initalizer lists.

Quote:
 And can someone explain the ? to me please. Ive seen it written and from the looks of it it checks if an if statement is true or something but can someone give me an example please.

"(test)?(if_true):(if_false)" is the ternary conditional operator. It can be useful, but is generally something that just makes the code harder to read. But, it lets you make some statements a lot more concise. Prefer to use if() statements.
const int y = rand() % 10;// concise, and x is constconst int x = (y==5)?1:0;// not concise, but we kept x constint check_y( const int value ){  if( value == 5 )    return 1;  return 0;}const int x = check_y(y);// not concise, and we had to remove the const from xint x;if( y == 5 )  x = 1;else  x = 0;// As for readability, it is easy to write lines that are just confusing with it.// So prefer if()'sconst float x = (a==2||b==3||foo()&&baz()||!isRunning&&isntHappy)?(letsdoStuff(10,100,1000,100000)):letsNotdostuff(100000,1000,100,10);//vsfloat x;if( x == 2 ||    b == 3 ||    foo() && baz() ||    !isRunning&&isntHappy ){  x = letsdoStuff(10,100,1000,100000);}else{  x = letsNotdostuff(100000,1000,100,10);}

Share on other sites
Cool cool, thanks for the replies.

I'll start using the initialiser lists then as they do seem worth it.

KulSeran, so the ? does not really have any benefits, it is just used to minimise code??? I think Ill just stick with if() statements for now.

Thanks

Share on other sites
FYI, with the initialisation list it actually initialises the variables in the order they are declared in the class, not the other they appear in the constructor.

Share on other sites
Quote:
 Original post by MonkanCool cool, thanks for the replies.I'll start using the initialiser lists then as they do seem worth it.KulSeran, so the ? does not really have any benefits, it is just used to minimise code??? I think Ill just stick with if() statements for now.Thanks

Oh, I forgot about your second question. The ternary operator can always be replaced with if statements. Use whatever makes the code more clear.

For instance, I would write:
int sign(double x) {  return x > 0.0 ? +1       : x < 0.0 ? -1       : 0}

Another example:
  std::cout << num_files << (num_files == 1 ? " file has" : " files have") << " been copied.\n";

Create an account

Register a new account

• Forum Statistics

• Total Topics
628275
• Total Posts
2981740

• 10
• 11
• 17
• 10
• 9