Derived class variable initialisation.

Discussion in 'C' started by Dench, Nov 13, 2009.

  1. Dench

    Dench New Member

    Joined:
    Nov 13, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have code with the following layout:

    Code:
    class Table
    {
        protected:
            // variables for all subclasses
        public:
            Table(int sizeOfTable);
    }
    Table::Table(int capacity)
    {
      sizeOfTable = capacity
      // initialise other variables
    }
    
    class roundTable: public Table
    {
        public:
           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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice