Do I need to call constructor?

Started by
4 comments, last by Miserable 19 years, 2 months ago
ok, im doing somthing like this Unit* Curr_Unit; Curr_Unit = Existing_Unit; // where existing unit is a valid unit so do i need to call the constructor on Curr_Unit in order for this to work? thnks [grin]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
Generally you don't 'call' constructors - they are called automatically on object creation.

In your code, Curr_Unit is a pointer to a Unit. If Existing_Unit is also a Unit pointer, then the value of the pointer will be copied and they will now point to the same place in memory. The actual object isn't copied.

If you actually want to create a new unit, copied from Existing_Unit, there are a couple of different ways to do it.

Unit* currUnit = new Unit;
*currUnit = existingUnit;

This assumes that existingUnit is an object, not a pointer, and has the = operator defined (or that the default = is appropriate).

Or:

Unit* currUnit = new Unit(existingUnit);

This uses a copy constructor to create currUnit from an existing unit object.

Anyway, you might do a little reading on pointers and make sure you understand them - they can be tricky. Also try looking up 'constructor' and 'copy constructor'.
Quote:Do I need to call constructor?


Constructors cannot be called, placement new excepted.

Quote:Unit* Curr_Unit;
Curr_Unit = Existing_Unit; // where existing unit is a valid unit

so do i need to call the constructor on Curr_Unit in order for this to work?


Depends on whether you want the pointer to point to a new copy of Existing_Unit
Unit* Curr_Unit = new Unit(Existing_Unit);
The Unit class will need a constructor.

Or whether you want Curr_Unit to simply point to the existing unit, in which case you just need to get its address.

Unit* Curr_Unit = &Existing_Unit;
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Unit* Curr_Unit = &Existing_Unit;


Of course here, Existing_Unit must be an object, not a pointer...

other examples:
Unit Existing_Unit;              // default constructor will be called. automatic.Unit Copy_Unit( Existing_Unit ); // copy constructor will be called. also automatic.Unit *Ptr_Unit = new Unit;       // default constructorUnit *PtrCpy_Unit = new Unit( Existing_Unit ); // copy constructor
ok, i dont want a new unit or a copy i just want curr_unit to point to the same thing that existing_unit is pointing to so would i do this

Curr_Unit &(*Existing_Unit);//?
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Quote:Original post by raptorstrike
ok, i dont want a new unit or a copy i just want curr_unit to point to the same thing that existing_unit is pointing to so would i do this

Curr_Unit &(*Existing_Unit);//?

No need to dereference and take the address; if they're both pointers, just assign.
Curr_Unit = Existing_Unit;

This topic is closed to new replies.

Advertisement