0

I know this is easy one but I'm confused whether int a (5); is faster than int a=5; in C++, as I read somewhere, if a constructor with only one parameter is defined in the class, the initialisation can be done with an equal sign.(A statement can be written with an equal sign) So I thought may be first one is bit of a extra Work.

Da3kL10rd
  • 340
  • 2
  • 6
  • 4
    It's equivalent. Feel free to test, but it's equivalent. Your own quote doesn't claim a difference; it's still initialization either way, and that's the same work for the same thing. – ShadowRanger Dec 22 '20 at 12:12
  • There are many more ways to do initialization in C++ than just those two. If you have an hour, you may be interested to watch the presentation [The Nightmare of Initialization in C++](https://www.youtube.com/watch?v=7DTlWPgX6zs) by Nicolai Josuttis. – Eljay Dec 22 '20 at 12:22
  • Okay I'll do that. – Da3kL10rd Dec 22 '20 at 12:42

2 Answers2

6

The two forms tell the compiler the same thing in different ways (the statements have the same ultimate meaning). Since the compiler is told the same thing, it should generate the same code.

(It is theoretically possible somebody could design a compiler to do different things with these statements, but that would be a bad design.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

Both should run within the same period. Even the quote you put doesn't suggest anything that shows a difference in performance.

Moataz Alsayed
  • 381
  • 2
  • 12