Thread: Arguments up constructor tree?

  1. #1
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    Arguments up constructor tree?

    Still toying with inheritance, and another Q:

    How would I go about declaring an instance of a subclass and send a constructor argument to the constructor of a base class?

    Specifically I need to modify a data member, declared as private in a base class. I think this requires the use of that class' constructor, but if I apply the argument to the derived class' constructor, I get a no matching funciton to call. If I try to have the derived class' constructor be overloaded to accept an argument, and change the data member, it tells me I can't touch a data member from outside of the class.

    Anyone?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    class base {
    	int baseInt;
    public:
    	base(int j) : baseInt(j) { }
    };
    
    class derived: public base{
    	double derivedDouble;
    public:
    	derived(double d, int j) : base(j), derivedDouble(d) { }
    };
    
    int main() {
    	base b(0);
    	derived d(3.14159265, 2);
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM