Constructors, newbie question.

This is a discussion on Constructors, newbie question. within the C++ Programming forums, part of the General Programming Boards category; I'm pretty inexperienced when it comes to C++ and I have a question. AFAIK , constructors cannot return anything. So ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220

    Constructors, newbie question.

    I'm pretty inexperienced when it comes to C++ and I have a question.

    AFAIK, constructors cannot return anything. So how can the constructor let the function making an instance of the class in question know that something has gone wrong?

    Iish, that was a mouthful. Sorry.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    The only way would be to set a flag in the class, and hope that the user checks it before trying anything. Or maybe throwing an exception, but I don't know much about that.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    That's easy. Install an error class which handles that. But what are you worried about? Bad alloc's? Programmer error? Using assert() is the way you detect programming errors early on, the only other real issue is memory allocation failure. But then, what really *can* you do about that but to bail out? Sure if you want to log something first, otherwise, just let it die - maybe the user will get a bigger harddrive then.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  4. Newbie question
    By wireless in forum C++ Programming
    Replies: 1
    Last Post: 02-24-2002, 04:25 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

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