CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Constructors

  1. #1
    Join Date
    Mar 2009
    Posts
    58

    Constructors

    Whats the advantages of writing the constructor this way

    CException( char* m ) { message = m; };


    instead of the old fashion way?

    Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Bulgaria
    Posts
    63

    Re: Constructors

    Be more descriptive.
    By the way I'd use initialization list for constructing 'message' and I'd prefer string rather than char*. And I definitely don't know which is "the old fashion way".

  3. #3
    Join Date
    Mar 2009
    Posts
    58

    Re: Constructors

    Dont worry about the parameters, it was just an example, I describe it better this way:

    This is one way:
    CException( char* m ) { message = m; };


    or other way I know is:

    CException::CException(char *m)
    {
    message = m;
    }

    As i said ignore the actually parameters, its just the different ways of writing the constructor i am interested in.

    Thanks

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Constructors

    There is absolutely no difference whatsover in that case. You're just putting the same code on a different line.

    This would be slightly different and generally better:
    Code:
    CException( char* m ): message(m)
    {}

  5. #5
    Join Date
    Mar 2009
    Posts
    58

    Re: Constructors

    Hi;

    Is there no advantage to it at all?

    Just a handier way of writing it basically so?

    Thanks

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Constructors

    Quote Originally Posted by newbie30 View Post
    Hi;

    Is there no advantage to it at all?

    Just a handier way of writing it basically so?

    Thanks
    It's EXACTLY the same code. All you did was put the braces and statement on one line, which IMHO is a bad habit as it makes code harder to read and debug.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Constructors

    I suppose declaring it inline in the class might make the compiler more likely to inline the function, same as any class routine. But otherwise it doesn't matter.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured