Like Tree1Likes
  • 1 Post By laserlight

Class constructor question

This is a discussion on Class constructor question within the C++ Programming forums, part of the General Programming Boards category; 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> ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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> .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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
    21,394
    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.
    AndrewHunter likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,866
    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, 04: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21