I know the concept that we can call the Constructor both Explicitly
and Implicitly
, and i have tested both the scenarios(generally till now my all purp
There are three ways a constructor can be called:
=
or by causing an implicit conversion from the argument type to your class.Which of these you can use in a particular context depends on the constructors you're calling.
class Foo
{
Foo(); // 1
Foo(int a); // 2
explicit foo(const std::string& f); // 3
Foo(int c, int d); // 4
};
Foo f;
. Never attempt to call a constructor without arguments explicitly, as Foo f();
will declare a function!Foo f = 42;
or Foo f(42)
.explicit
keyword forbids implicit conversion by writing Foo f = std::string("abc");
or function_taking_foo(function_returning_string());
.