Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
please reply me as soon as possible

What I have tried:

i have tried to define an constructor outside the class and it does not work sir tell me can it be possible to difine outside the class
Posted
Updated 10-Mar-16 21:38pm
Comments
dan!sh 10-Mar-16 23:48pm    
No.
PIEBALDconsult 10-Mar-16 23:55pm    
No, but maybe you want a Factory?
Member 12384360 10-Mar-16 23:56pm    
now what is factory here?
Sergey Alexandrovich Kryukov 11-Mar-16 0:14am    
See, for example, https://en.wikipedia.org/wiki/Factory_method_pattern...

Also, why writing something in the section "What I have tried" without showing what you have tried? What exactly have you written?

—SA
Sergey Alexandrovich Kryukov 11-Mar-16 0:13am    
Before asking about possible or impossible, think at this: what would it possibly mean? The constructor creates an instance of a class. And why?
—SA

No and you shouldnt do it, because it destroyes the class pattern of encapsulation. If you need to modify a class object it should always made with member functions.

I think you must rethink your software design. Learn from this tutorials.
 
Share this answer
 
The contructor must be (by definition) a member of the class. However you can put its definition outside of the class (declaration must be inside the class one):
C++
// ctor.h header file
class A
{
public:
  A(); // just ctor declaration
};

and
C++
// ctor.cpp source file
#include <iostream>
#include "ctor.h"

// this is the ctor definition
A::A(){std::cout << "A()" << std::endl;}

int main()
{
  A a;
}
 
Share this answer
 
v2
Comments
Member 12384360 11-Mar-16 6:41am    
this code is showing an error
that there is no such directory as ctor
Member 12384360 11-Mar-16 6:44am    
ok i now i understand that the difination nay be outside but declarationmust be inside

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900