Search code examples
c++performancemethodsconstructorvariable-initialization

C++ -> Which is faster?? int a(5); or int a = 5;


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.


Solution

  • 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.)