2

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?

Community
  • 1
  • 1

6 Answers6

17

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
Pierre Fourgeaud
  • 13,652
  • 1
  • 34
  • 58
8

You cannot. This is the constructor declaration, which has to be in the class definition:

// MyClass definition, possibly in .h file
class MyClass
{
 public:
  MyClass(); // default constructor declaration
  ...
};

and this is the constructor's definition

 // MyClass default constructor definition, possibly in .cpp file
MyClass::MyClass() { }

What you are referring to is the constructor's definition. You can define it outside of the class definition to allow you to de-couple one from the other. Usually this means you put the class definition in a header file, to be included by client code, and the constructor definition (as well as other class member function definitions) in an implementation file that gets compiled. This way, users of your code have no compile time dependency on said definitions.

juanchopanza
  • 210,243
  • 27
  • 363
  • 452
6

The main reason for defining the constructor (or any member function) outside of the class declaration is so that you can have a header file and an implementation file. This makes your code clearer to read and allows you to distrbute the interface to your class (the header file) without providing the implementation details.

dunc123
  • 2,327
  • 1
  • 8
  • 10
1
You can not declare a constructor or anything part of the class, outside it, but you can define

It's a common c++ programming practise of using 2 files for 1 class
For example,

ClassG.h

class ClassG
{
public:
ClassG();
~ClassG();
void anyfunc(int anyarg);
private:
int anydata;
};

ClassG.cpp

#include"ClassG.h"
ClassG::ClassG()
{
//something
}
ClassG::~ClassG()
{
//something
}
void ClassG::anyfunc(int anyarg)
{
//something
}

General Syntax

While declaring, inside .h file type returntype methodname(args_if_any);
While defining in a .cpp file type returntype ClassName::methodname(args_if_any){}

Govind Balaji
  • 621
  • 6
  • 15
0

First of all, you have your terminology confused, you need to read What is the difference between a definition and a declaration?. You can not declare a constructor outside of a class but you can define it outside of a class.

So on to the questions as you meant them:

Why can I define a constructor out of the class

Basically the standard has this to say about member functions in section 9.3.2:

A member function may be defined (8.4) in its class definition, in which case it is an inline member function (7.1.2), or it may be defined outside of its class definition if it has already been declared but not defined in its class definition.

So the standard says this is ok, next question:

why we should do this?

It makes your code cleaner and easier to digest, you can separate your declarations into header files and your implementation into source files. This previous thread Why have header files and .cpp files in C++? goes into this topic in more detail.

Community
  • 1
  • 1
Shafik Yaghmour
  • 143,425
  • 33
  • 399
  • 682
0

Yes, you can define the constructor inside the class no matter that whether you are using .h file or .cpp file. Actually the main difference between both the file is, header file never compile it only use for checking syntax during compilation of .cpp file.

Method that you define inside the class by default inline function. If you define one constructor inside the class it's fine but, if you required to define more than one constructor then 2 problem occur

Problem 1:
As we know that, when you call inline function it will paste code in called function that increase the code size and required more memory.

Problem 2: Reduce the readability of code.

Cody Gray
  • 222,280
  • 47
  • 466
  • 543
Antrikssh
  • 1
  • 2