Click here to Skip to main content
14,668,434 members
Rate this:
Please Sign up or sign in to vote.
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
lw@zi 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?
   
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
   
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
Member 12384360 11-Mar-16 0:20am
   
ohk i got it
«_Superman_» 11-Mar-16 3:12am
   
Please post what you have tried so far.

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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):
// ctor.h header file
class A
{
public:
  A(); // just ctor declaration
};

and
// 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;
}
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100