Calling a constructor from another constructor

Can't I legally call a constructor from another one. For exaple in the code given below, I find while debugging that the constructor with the argument is being called form the other one(one without any arguments) and also the value is initialized to i. but after the object is constructed i stores a junk value. It seems that somehow the value of i gets changed atfer it exits from the constructor.

 
#include<iostream.h>
class C1 {
public:
      int i;
      C1(){C1(5);}
      C1(int j){i=j;cout << "constructor called." << endl;};

};

void main(void)
{
C1 c1;
cout << c1.i ;

}
dipak_kr_ghoshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lif081199Commented:
     C1(){ C1(5); }
this line create a new C1 object, which is not copied into the this pointeur...

try this :

class C1
{
public:
      int i;
      C1(){ *this = C1(5); }
      C1(int j){i = j;};
}
;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lif081199Commented:
Or this works too :

class C1
{
public:
      int i;
      C1(){ i = C1(5).i; }
      C1(int j){i = j;};
}
;
0
dipak_kr_ghoshAuthor Commented:
Hi lif
  Your answer is almost acceptable. Actually I wanted to know whether we can call a constructor explicitly from anywhere, maybe another constructor (like in java).
  You are sure to get points on this :-)
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

lif081199Commented:
Why don't you work with pointers ?

So you can call the new operator in a very proper way.

The other good method is to use an Init or Create function :

class C1
{
public:
int i;
C1(){ Create(); }
C1(int j) { Create(j); }
void Create(int ii=5) { i = ii; }
};

And then you can call the C1::Create function from where you want.
This method is really proper too.

Lionel.
0
dipak_kr_ghoshAuthor Commented:
Hi lif
  My proble is not how to get rid of this situation or problem. I want to know only a theoritical point <<whether we can call a constructor explicitly from anywhere at all or not (this is supported in java) >> Also I am not averse to pointers.
0
lif081199Commented:
You CANNOT do this in theorical or practical OOP.

That's why you have lot's of Create() functions in MFC.

Lionel.
0
dipak_kr_ghoshAuthor Commented:
lif
Thanx
0
nietodCommented:
I've never seen any theoretical OOP principle against the calling of constructors.   However, C++ definitely does forbid this.  The compiler itself will call the constructor for an object once the object has been created.

But C++ does allow you to get around this.  You can use placement new to "effetively" call the constructor on an object.   Placement new ceates an object in a specified location (i.e it doesn't actually create an object, as the location already exists) and then calls the constructor on this object.   So this can be used to invoke the constructor of an existing object.   However using it for this purpose is certainly....questionable.
0
nietodCommented:
Note don't confuse the task of the constructor, which is to initialize an existing object, with creating an object.  They are seperate processes in C++.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.