0
class A {};

A a;// 1
A a{};// 2
A a = {};// 3
A a = A();// 4

There seems to be all options. Are 1, 2 and 3 are same and just the matter of style or there is some difference? 4 supposed to first create a temporary object and then assign it to an a, but it will happen only if I will turn off comliler's optimization completely, right?

4
  • 1
    I don't understand, what does your question have to do with Object Oriented Programming? Commented Jun 12, 2020 at 23:41
  • 2
    Supplementary reading on the options available: en.cppreference.com/w/cpp/language/initialization . Some entertaining viewing on just how deep the rabbit hole can go: youtube.com/watch?v=7DTlWPgX6zs Commented Jun 12, 2020 at 23:44
  • Note that starting with C++17, the 4th option does not actually create a temporary. In fact, it doesn't even require a move constructor be available (but C++14 does). Commented Jun 12, 2020 at 23:58
  • Initialization in C++ is more complicated than it should and has lot of corner cases. All your options might have different behaviors (but for your A, they are similar). Commented Jun 13, 2020 at 0:08

1 Answer 1

2
  1. Is different because it does default initialisation. This does not matter in case of A however, because there are no members to initialise.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.