Rule of thumb for constructors?

This is a discussion on Rule of thumb for constructors? within the C++ Programming forums, part of the General Programming Boards category; When my program starts up it loads several graphics files, The variables that let me access the loaded files are ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    Rule of thumb for constructors?

    When my program starts up it loads several graphics files, The variables that let me access the loaded files are members of the class sec, should I put the code that loads the files in the constructor for sec, or in a seperate init function, and call that function when the program starts? There will only be one instance of this class, so redundant loading isnt the concern, I just curious which way presents better code?

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    It's a little unclear what exactly your asking. what environment are your programming in? MFC? Don't know too much about that, Anyways, it doesn't sound like your using classes for the right reason if you had to create an init function for a class; but, if you ask me, go with the constructor. They tend to be more self-contained and less worrisome then Init functions.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    I always thought constructors were more for inililizing the members of a class. What im doing is loading external data, the variables that access the data after loaded just happen to be in a class, I was just wondering what was teh proper way to code it?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I would do as you suggest Eber. There is nothing to gain by paying for another function call if you dont have to.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    it doesn't sound like your using classes for the right reason if you had to create an init function for a class; but, if you ask me, go with the constructor. They tend to be more self-contained and less worrisome then Init functions.
    To the contrary. I think Eber asked the right question. Constructors are for initializing variables. BUT: A constructor has no return value. Therefore, if you do anything that can fail at initialization, use a boolean Init function.

    >What im doing is loading external data

    Then you basically have two choices. Keep a state and query that like this:

    Code:
    YourClass c( "externat.dat" );
    
    if( c.failed() ) return false;
    or have an Init function:

    Code:
    YourClass c;
    
    if( ! c.Init("External.dat" ) ) return false;
    I personally would prefer the Init function, and if you look at bigger suppliers of class libraries that have to load externals, like databases or datafiles, they seem to prefer that, too.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    A constructor has no return value. Therefore, if you do anything that can fail at initialization, use a boolean Init function.
    That's what exceptions are for:
    Code:
    try {
        YourClass c("externat.dat");
    } catch(YourClass::InitFailed) { // or whatever exception system you use
        cerr << "Couldn't initialize YourClass." << endl;
        exit(1);
    }
    - lmov

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Throwing an exception ? For a perfectly legal thing ?
    A network path not available, or a file not where you want it to be is nothing exceptional. It's a normal error condition.

    Oh, and by the way:

    If you need parameters for initialization, that means you will always have to create your objects with new if they are part of a class.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Misra Rule 11.2
    By hammer1234 in forum C Programming
    Replies: 1
    Last Post: 04-06-2006, 07:28 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Rule of 72
    By sonict in forum C++ Programming
    Replies: 12
    Last Post: 01-23-2003, 07:31 PM
  4. Who should rule the world?
    By CoderBob in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 02-07-2002, 06:01 AM

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