can we define overloaded constructor outside the class ?
if yes then what would be return type in definition like
return_type class_name::func_name(arguments){}
You define the implementation of a constructor in a different location from the declaration, but you cannot define a new constructor for it.
1 2 3 4 5 6
class A
{
int a;
public:
A() { a = 1; }
};
Could be re-written as:
1 2 3 4 5 6 7 8 9 10
class A
{
int a;
public:
A();
};
A::A()
{
a = 1;
}
I'm not sure if this is what you are asking so here's another idea. Let's say you want to take an existing class and extend it by making your own constructor. You could inherit from it. Example: if you want to construct a std::string using your own custom type as a parameter:
1 2 3 4 5
class String : std::string
{
public:
String(MyType& mt);
};
OR you could make a function that will make a string out of your type: std::string CreateString(MyType& mt);