Thread: simple inheritance question

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    simple inheritance question

    I have a base class foo:

    Code:
    class foo {
         public:
              foo(char *val);
    };
    from which I want to derive bar. For now, bar can be identical to foo:
    Code:
    class bar : public foo {
    };
    However, when I use the constructor I get told:

    error: no matching function for call to ‘bar::bar(char*&)’
    note: candidates are: bar::bar()
    note: bar::bar(const donor&)


    How do I set this up so I can use the constructor for foo as defined:
    Code:
    foo::foo (char *ptr) {
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Constructors can't be inherited. You'd need to define a constructor for bar, and invoke foo's constructor:

    Code:
    class bar : public foo {
    public:
        bar(char* val): foo(val) {}
    };
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Thanks!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Simple Question, I think
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2002, 10:36 AM

Tags for this Thread