[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

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 ;

}
0
dipak_kr_ghosh
Asked:
dipak_kr_ghosh
  • 4
  • 3
  • 2
1 Solution
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now