Why can I declare constructor out of the class
c++
Solution
You cannot declare constructor out of the class. You are talking about constructor definition.
class MyClass
{
public:
MyClass(); // declaration
};
MyClass::MyClass() // definition
{
}
You should read this.
The main reason why you should define your constructor outside the class is for readability. It is clearer to declare your class in the Header file and define it in the source file. You can apply this rule any members of your class.
A little quote from the standard :
12.1 Constructors
struct S {
S(); // declares the constructor
};
S::S() { } // defines the constructor
Problem
Consider this link. See this code: ``` class MyClass { public: MyClass(); ~MyClass(); private: int _a; }; MyClass::MyClass() { } MyClass::~MyClass() { } ``` We can declare constructor out of the class. Why can I declare constructor out of the class and why we should do this?