Thread: Derived class constructor only works if it invokes base class constructor

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    216

    Question Derived class constructor only works if it invokes base class constructor

    Hi,

    Check out the following code the declaration and definition. (They are in separate files in the project):

    Code:
    class stack_size_error : public std::length_error {
    public:
        explicit stack_size_error(const std::string &msg);
    };
    
    stack_size_error::stack_size_error(const std::string &msg) : std::length_error(msg){};
    So a user made exception that descends from one in the standard library. Ok. It seems it calls its base class constructor when used and just passes the string argument into that. However if I try and write a custom constructor (which is empty) for it like so:

    Code:
    stack_size_error::stack_size_error(const std::string &msg) {};
    The compiler states that it's not a matching function call. I don't know why though. Surely I can make a derived class and then write my own valid constructor for it? Compiler won't accept it though. I'm aware that when one calls a derived class constructor it also automatically calls its parent constructor. Up to now though I've never had to explicitly state that in code.

    I'm wondering does the base class std::length_error perhaps not have a constructor defined that takes no arguments? Maybe that's it.

    I'm confused, thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,652
    I'm wondering does the base class std::length_error perhaps not have a constructor defined that takes no arguments?
    You're ... wondering??
    Have you tried looking it up?
    std::length_error - cppreference.com

    It wouldn't make sense for length_error to have a ctor that doesn't take a string since it has a what() method that needs to print a string.
    std::exception's ctor doesn't take a string, but in that case you have to provide your own what() method (although MS seems to have a non-standard ctor that takes a string and a default what() that prints "Unknown exception" if no string is given).
    And that's the world in a nutshell, an appropriate receptacle.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    216
    Lol John I suppose I should've just gone and looked it up given it's a commonly defined standard thing. I noticed just as you said that std::exception can be called without an argument but doesn't produce any specific data when thrown and caught.

    Thanks again John

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-06-2015, 09:04 AM
  2. Base and Derived class copy constructor help.
    By byebyebyezzz in forum C++ Programming
    Replies: 4
    Last Post: 08-04-2011, 07:10 AM
  3. Replies: 5
    Last Post: 07-25-2008, 04:37 AM
  4. Replies: 9
    Last Post: 06-20-2008, 02:41 AM
  5. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM

Tags for this Thread