Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Derived class variable initialisation. (http://www.go4expert.com/forums/derived-class-variable-initialisation-t20079/)

Dench 13Nov2009 09:31

Derived class variable initialisation.
I have code with the following layout:


class Table
        // variables for all subclasses
        Table(int sizeOfTable);
Table::Table(int capacity)
  sizeOfTable = capacity
  // initialise other variables

class roundTable: public Table
      roundTable(int capacity) : Table (capacity){}
      //declare subclass variables

Now, the problem I have is that I have a number of variables that need initialising(in a function) for each subclass, and I want this function to only be called once, as the subclass is constructed.
I have found out (the hard way) that I cannot simply put a virtual function in the constructor for 'Table'.
However, I cannot seem to find any alternative to this, I have looked at passing values up from the derived class but this doesn't seem to fit my problem at all.
Is there an easy way around this or am I looking at this problem the completely wrong way?
I can easily create an initialisation function in the subclass and call it in the main method, but I'm hoping there is a way to do it as I have described above.
Any help is much appreciated.

All times are GMT +5.5. The time now is 22:50.