Thread: Constructor Inheritance with arguments does not allow a body definition

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    8

    Constructor Inheritance with arguments does not allow a body definition

    I want to have a derived class with a custom constructor. What I am trying is this:

    Code:
    class	Base
    {
    	protected:
    	int  Number;
    	int  MaxMemory;
    
    	Base(int inNumber,int inMaxMemory);	
    };
    
    class	Leaf : public Base
    {
            protected:
            void  *ExtraData;
    	Leaf(int inNumber,int inMaxMemory) : Base(inNumber,inMaxMemory);  // putting the semicolon here gives an error (It says that I should put "{")
    };
    
    Leaf::Leaf(int inNumber,int inMaxMemory) : Base(inNumber,inMaxMemory)
    {
            ExtraData = malloc(100);  // Do some extra initalization of members
    }
    But unfortunately I seem to only be allowed to do this:
    Code:
    class	Leaf : public Base
    {
            protected:
            void  *ExtraData;
    	Leaf(int inNumber,int inMaxMemory) : Base(inNumber,inMaxMemory){}
    };
    So I can call the proper inherrited constructor with the proper arguments, but I can't do any further initialization of any extra members in the class. How to solve this?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The ": Base(...)" part should go in the definition of the constructor. So if you decide to put the constructor in another file, you might have something like this:
    Code:
    class Base {
    private:
        int data;
    public:
        Base(int data) : data(data) {}
    };
    
    class Derived : public Base {
    private:
        int d_data;
    public:
        Derived(int data);
    };
    and in a .cpp file,
    Code:
    Derived::Derived(int data) : Base(data), d_data(data * 2) {
        /* ... */
    }
    Note that you have to put the constructor calling and the variable initializing in a specific order. I think I got the order right.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    8
    Thanks that worked,

    I only had to put the call to the inherited constructor in the function definition and not in the function prototype.
    as:
    Code:
    class	Leaf : public Base
    {
    	void *ExtraData;
    	Leaf(int inNumber,int inMaxMemory); 
    };
    
    Leaf::Leaf(int inNumber,int inMaxMemory) : Base(inNumber,inMaxMemory)
    {
            ExtraData = malloc(100);  // Do some extra initalization of members
    }

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Note: You can actually do:

    Code:
    class	Base
    {
    	protected:
    	int  Number;
    	int  MaxMemory;
    
    	Base(int inNumber,int inMaxMemory);	
    };
    
    class	Leaf : public Base
    {
            protected:
            void  *ExtraData;
    	Leaf(int inNumber,int inMaxMemory) : Base(inNumber,inMaxMemory) { }
    };
    If you don't feel like cluttering up your source code with a useless constructor definition, unless, of course, you will be putting something else in it as well. It will also, in my opinion, make your code easier to read, in the case that someone simply takes a look at your class declaration to understand what it's doing in general.
    Last edited by IceDane; 09-16-2009 at 06:47 AM.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Uh, no, you can't do that. You have two definitions of the same constructor here.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by CornedBee View Post
    Uh, no, you can't do that. You have two definitions of the same constructor here.
    My bad, simply copy-pasted his stuff and forgot to remove the other definition. Will fix it.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM