C ++ assigning variables is the usual way ..?

It may be a stupid question, but still I'm a little curious ... I recently worked on one of my previous colleagues projects, and I noticed that he really liked to use something like this:

int foo(7); 

instead:

 int foo = 7; 

Is this a normal / good way to do in C ++? Are there any advantages to this? (Or is it just some kind of silly programming style that he was in ...?)

It really reminds me of a good sense of how cool member variables can be assigned in a class constructor ... something like this:

 class MyClass { public: MyClass(int foo) : mFoo(foo) { } private: int mFoo; }; 

instead of this:

 class MyClass { public: MyClass(int foo) { mFoo = foo; } private: int mFoo; }; 
+7
source share
8 answers

For basic types there is no difference. Use what matches the existing code and looks more natural to you.

Otherwise

 A a(x); 

performs direct initialization and

 A a = x; 

initializes copying .

The second part is a list of member initializers; there is a bunch of Q & How about this in StackOverflow.

+8
source

Both are valid. For built-in types, they do the same; there is a subtle difference for class types.

 MyClass m(7); // uses MyClass(int) MyClass n = 3; // uses MyClass(int) to create a temporary object, // then uses MyClass(const MyClass&) to copy the // temporary object into n 

The obvious implication is that if MyClass does not have a copy constructor or has one, but it is not available, the attempt to build fails. If the construct is successful, the compiler can skip the copy constructor and use MyClass(int) directly.

+3
source

All of the above answers are correct. Just add to this that C ++ 11 supports a different way, common, as they say, to initialize variables.

 int a = {2} ; 

or

 int a {2} ; 
+3
source

This is the style of initialization of C ++ variables - C ++ added it for basic types, so the same form can be used for basic and user types. this can be very important for template code that is designed to instantiate any type.

If you want to use it for normal initialization of basic types, this is a style preference.

Note that C ++ 11 also adds a uniform initialization syntax that allows you to use the same initialization style for all types - even aggregates like POD structures and arrays (although custom types may require a new constructor type that accepts an initialization list to use single syntax with them). A.

+2
source

A few other good answers point to the difference between building in place ( ClassType v(<constructor args>) ) and creating a temporary object and using the copy constructor to copy it ( ClassType v = <constructor arg> ). I think two more points need to be done. Firstly, the second form obviously has only one argument, so if your constructor accepts several arguments, you should prefer the first form (yes, there are ways around this, but I think the direct construction is more concise and readable, but, as indicated that personal preference).

Secondly, the form you use matters if your copy constructor does something significantly different from your standard constructor. In most cases, this will not be the case, and some will argue that this is a bad idea, but the language really allows you to do this (all the surprises that you end up with, because of this, is your own mistake).

+2
source

Your question is not stupid, because everything is not as simple as it might seem. Suppose you have:

 class A { public: A() {} }; 

and

 class B { public: class B(A const &) {} }; 

Record

 B b = B(A()); 

Requires copy constructor B to be available. Record

 B b = A(); 

It is also required that the constructor B of the transform B (A const &) not be declared explicit. On the other hand, if you write

 A a; B b(a); 

everything is fine but if you write

 B b(A()); 

This is interpreted by the compiler as declaring function b, which takes an unnamed argument, which is a parameterless function that returns A, which leads to mysterious errors. This is called C ++ the most unpleasant parsing .

+2
source

I prefer to use a style in brackets ... although I always use space to highlight calls to functions or methods that I don't use a space on:

 int foo (7); // initialization myVector.push_back(7); // method call 

One of my reasons to prefer to use this all over the board for initialization is that it helps to remind people that this is not an assignment. Therefore, overloads for the assignment operator will not apply:

 #include <iostream> class Bar { private: int value; public: Bar (int value) : value (value) { std::cout << "code path A" << "\n"; } Bar& operator=(int right) { value = right; std::cout << "code path B" << "\n"; return *this; } }; int main() { Bar b = 7; b = 7; return 0; } 

Output:

 code path A code path B 

It seems that the presence of an equal sign overshadows the difference. Even if this is β€œgeneral knowledge,” I like to do initialization, look much different than assignment, because we can do it.

+2
source

This is just the syntax to initialize something: -

 SomeClass data(12, 134); 

It looks reasonable, but

 int data(123); 

It looks weird, but they have the same syntax.

+1
source

All Articles