   inheritance and constructors
  Hi, I have a qustion about constructors that get called in an inheritted object. I have been reading "Thinking in C++" and it does cover this topic but I can''t seem to get it to work. What I am doing is this:I have an object player that inherits most of it''s stucture from my particle object. Now my particle object supports several different constructors from a default (void) to on that accepts like 8 different parameters.I can get the inheritance to work if I just use the default constructors but I would like to call one of the more advanced constructors from my subclass constructors. here is an example of the current code I haveplayer.h   #pragma once#include "particle.h"#include "stdafx.h"class player: public particle{public:int currentWeapon, maxWeapons, armor, lives;player(double x, double y, int maxWpn);player(double x,double y, int life, int velocity, int theta, int maxWpn, int armor, int lives);~player(void);void age(void);void age(int decrement);void respawn(int x, int y);private:string *weapon;};player.cpp   #include "player.h"player::player(int maxWpn): particle(double x, double y){this->armor = 0;this->maxWeapons = maxWpn;this->currentWeapon = 0;this->weapon = new string[this->maxWeapons];this->lives = 3;}player::player(double x,double y, int life, int velocity, int theta, int maxWpn, int armor, int lives){}void player::age(){}void player::age(int decrement){}void player::respawn(int x, int y){}player::~player(void){delete this->weapon;}any help would be greatly appreciated. Thanks in advance.-Andy   "And then 2 men appeared... Men in dark suits.. with dark soulless eyes. Men like this could have come from only one place.. The bank."
  If you use an initialization list, you can specify how the base class constructor is called.  So instead of assigning all of your default player values in the constructor function body, you can do something like this as the constructor declaration in the header file: player(double x,double y, int life, int velocity,  int theta, int maxWpn, int armr, int lives) : armor(armr), maxWeapons(maxWpn), currentWeapon(0), weapon( new string[maxWpn] ), lives(life), particle( stuff you want to pass to particle() ) {} Or did I completely misunderstand you?   [edited by - null_void on February 13, 2004 1:00:47 PM] [edited by - null_void on February 13, 2004 1:01:27 PM] [edited by - null_void on February 13, 2004 1:06:09 PM] [edited by - null_void on February 13, 2004 1:06:35 PM];  Constructors for derived classes are called from the highest level of inheritance (e.g., the base) downward. Destructors are called in the reverse order. For your classes, for example, when you create a new player, first the particle constructor is called for it, and then the player constructor. This way the derived class can be assured that all members of the base class have been constructed to some sane values.By default, if you don't explicitly invoking a base class constructor in one of your derived constructors, you will get the default constructor for the base class. If you want to invoke a specific constructor you must use member initialization syntax -- which you have done *almost* correctly on the lineplayer :: player(int maxWpn): particle(double x, double y) { /* ...*/ }in player.cpp. The correct syntax would be to omit the "double"s from the call to particle() and pass the values you want, exactly as if you were invoking the desired particle constructor.Here are some examples in case I've been unclear (happens a lot): // First invokes particle::particle() before executing:player::player(void) { }// First invokes particle(double x,double y):player::player(double xPos,double yPos) : particle(xPos,yPos) { }// You can pass anything, its just a function call:player::player(int foo,char *bar) : particle(foo,bar,3.0f) { }Hope that helps.EDIT: Smilies can go die, and null beat me to it anyway.   [edited by - jpetrie on February 13, 2004 1:10:53 PM];  Your explanation was so much better than mine.  ;  Hey guys thanks for your replies! jpetrie that is exactly what I was looking for thanks a ton man! Thinking in C++ is an excellent book.. but the dude couldn''t make it as simple as you did.Thanks again,Andy   "And then 2 men appeared... Men in dark suits.. with dark soulless eyes. Men like this could have come from only one place.. The bank."