Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which one should i use and why: {} vs = in c++

Tags:

c++

I was watching MVA's(Microsoft Visual Academy's) tutorials and I came across these two operators i.e. {} and = to pass in the value to variables. I have done C programming so I am pretty much aware of the assignment operator =. But {} is not in any language I did so far.

Kate is teaching the C++ course so she told that {} is used for copying.

But I was using the operator {} in the class below and it shows some error when I try to do this:

this->_width{new_width};

whereas below one works:

this->_width = new_width; 

Why so? I am also using {} to pass values in constructor but then they work perfectly. Only Problem is with member fucntions.

class Rectangle
{

public:
     void resize(int new_height, int new_width)  { this->_width{new_width ; this->_height{new_height};        //member function

     Rectangle(): _width{} , height{} {}   //constructor

private:
     int _width; 
     int _height; 

}; 
like image 367
hehehehe Avatar asked Jan 03 '15 12:01

hehehehe


2 Answers

{} can be used to initialise variables in C++11 in the same way that they are used to initialise arrays and structures in C.

This has been introduced primarily to provide consistency in language syntax (initialising with {} will work in all contexts, whereas initialising using the assignment operator or () will work in specific contexts.

There is a further advantage to list initialisation in that it prevents narrowing - i.e. it prevents you from providing an integer when a double is required, or a short when an integer is required. In this way it can help to reduce bugs.

Note that {} cannot be used to pass values to a function - only to construct new objects.

This page is also worth reading

like image 75
Richard Hodges Avatar answered Sep 16 '22 14:09

Richard Hodges


Using {} is called uniform initialization in this context. It was introduced mainly for two reasons.

First, as the name indicates, initialization is uniform, that is it looks and works the same for single objects, arrays, containers that accept initializer lists, etc.

Second, and equally important, it is impossible to get a most vexing parse using curly braces, which is quite possible unintentionally otherwise:

A a(); // What does this do? What was probably intended?
B b{}; // And what does this do?

Also, as a bonus (kudos to @Richard Hodges), you avoid narrowing conversions using uniform initialization.

To literally answer the question "which one should I use?", you should preferrably use {} as it has only advantages, and no disadvantages (plus, Bjarne Stroustrup recommends using it).

like image 24
Damon Avatar answered Sep 18 '22 14:09

Damon