Thread: Class constructor question

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    22

    Class constructor question

    Below is a code snippet for an exception code, see below:

    Code:
    #include <iostream>
    
    #define WIN32_MEAN_AND_LEAN
    #include <winsock2.h>
    #include <windows.h>
    
    using namespace std;
    
    class HRException
    {
    public:
        HRException() :
             m_pMessage("") {}
        virtual ~HRException() {}
        HRException(const char *pMessage) :
             m_pMessage(pMessage) {}
        const char * what() { return m_pMessage; }
    private:
        const char *m_pMessage;
    };
    
    int main(int argc, char* argv[])
    {
        // main program
    }
    After the first constructor, there is a colon. See below:
    Code:
    HRException() :
             m_pMessage("") {}
    My question is this, is this another way to 'inline' a function.

    I've not seen it used before, can anyone care to explain? Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's called an initialisation list.
    Initialization Lists in C++ - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    22
    Aw, thanks. that makes heaps more sense.

    I got this code from a tutorial online, which obviously wouldn't compile if it is trying to send an argument to a base class function if its not inheriting from it.

    What is the general exception class that you inherit from?

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I got this code from a tutorial online, which obviously wouldn't compile if it is trying to send an argument to a base class function if its not inheriting from it.

    What is the general exception class that you inherit from?
    Yes.. that's what confused me.. (I deleted my reply .. so it won't confuse others ).
    AFAIK Generally exception handling classes are declared by deriving from the ones provided in <exception> .

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Freem View Post
    Aw, thanks. that makes heaps more sense.

    I got this code from a tutorial online, which obviously wouldn't compile if it is trying to send an argument to a base class function if its not inheriting from it.
    So you understand why the sample code uses an initialization list then? Not because it is inheriting from anywhere, but rather since it is initializing a constant member value.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AndrewHunter
    Not because it is inheriting from anywhere, but rather since it is initializing a constant member value.
    Actually, the member variable m_pMessage is not const. It is what it points to that is const.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by laserlight View Post
    Actually, the member variable m_pMessage is not const. It is what it points to that is const.
    D'oh, thanks for the correction Laser.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 40
    Last Post: 06-02-2010, 10:08 AM
  2. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  3. Class Constructor/Destructor Question
    By Sentral in forum C++ Programming
    Replies: 6
    Last Post: 05-16-2006, 04:13 AM
  4. Newbe Question - Class constructor
    By (Slith++) in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2006, 05:20 AM
  5. Class Constructor question
    By bman1176 in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2002, 06:52 AM

Tags for this Thread