Is this statement true that declaring a constructor with arguments hides the default constructor and you cannot invoke the default constructor.
+8
A:
Not exactly. Instead it suppresses the generation of compiler-provided default constructor. Consider:
class Class1 {
};
Class1 will have a compiler-generated default constructor, so you can call new Class1(), while
class Class2 {
Class2( int );
}
will not have a compiler-generated default constructor. So you can't call new Class2() unless you explicitly declare a default constructor for Class2:
class Class2 {
public:
Class2();
Class2( int );
}
sharptooth
2010-05-31 05:58:31
Rather than specifying multiple constructors ,the best way is to use default argument like this :class MyClass { int i; public: MyClass(int i = 0) { this->i = i}};
Ashish
2010-05-31 06:04:05
@Mac: Sometimes what you suggest is better, sometimes not - depends on exact scenario.
sharptooth
2010-05-31 06:10:12