In C++, what is the difference between something like: int i = 3; and int i(3); and which is better?

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions.

A community for students.

In C++, what is the difference between something like: int i = 3; and int i(3); and which is better?

Computer Science
See more answers at brainly.com
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

For intrinsic types, they're identical. Even for your own defined classes, they should result in the same code if no or an empty constructor is explicitly defined. However, consider this: class A { public: A() { for(int i=0; i<100000; i++) m_data += sqrtf(m_data); } A(const A &other) { m_data = other.m_data; } A &operator = (float val) { m_data = val; } private: float m_data; }; It's nonsensical, but illustrates the point. In this case, if you do for example A myA; myA = someOtherA; you're executing the default constructor, which does a metric ton of work, only to then throw away that work by assigning via the = operator. If instead you do A myA(someOtherA); it uses the copy constructor, which simply assigns the m_data value of the object using m_data of someOtherA. Even if you don't explicitly define a copy constructor, C++ will create one for you (called the implicitly defined copy constructor), that simply copies the value of each member variable over using their own copy constructor. There's a pitfall here, because if the member variables of a class are complex types themselves, they may have time intensive copy constructors that are invoked by the implicitly generated copy constructor of your class without you noticing.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

Not the answer you are looking for?

Search for more explanations.

Ask your own question