initialization lists trouble

Discussion in 'C++' started by Jacek Dziedzic, Dec 5, 2005.

  1. Hello!

    Suppose I have a class Foo that defines a default c'tor
    that initializes some data using an initialization list:

    Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20)
    // and so on, quite a few members
    {}

    and does nothing else.

    Then it turns out that I need two more c'tors that would
    do some extra initialization apart from the one performed
    by the default c'tor, like

    Foo::Foo(string filename) {
    // do something with filename
    // * now wants to initialize the members in the same
    // way Foo::Foo() does
    }

    Foo::Foo(int n) {
    // do something with n
    // * now wants to initialize the members in the same
    // way Foo::Foo() does
    }

    As I can't call the default c'tor from the other c'tors,
    except for a base-class c'tor, I am faced with a choice
    of either repeating the initialization lists in all c'tors
    or moving the initialization to an init() method altogether.

    But in an init() method I can't use the initialization
    list, as it is only allowed in a constructor.

    It somehow turns out that all my classes wind up with
    at least two constructors and at some point I always have
    to resort to an init() method and get rid of the initialization
    lists in favor of a screenful of

    member1=0;
    member2=0;
    member3=NULL;
    member4=20;

    lines.

    Am I missing something or do the initialization-lists turn
    out to be useless that often?

    thanks in advance,
    - J.
     
    Jacek Dziedzic, Dec 5, 2005
    #1
    1. Advertising

  2. Jacek Dziedzic

    mlimber Guest

    Jacek Dziedzic wrote:
    > Hello!
    >
    > Suppose I have a class Foo that defines a default c'tor
    > that initializes some data using an initialization list:
    >
    > Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20)
    > // and so on, quite a few members
    > {}
    >
    > and does nothing else.
    >
    > Then it turns out that I need two more c'tors that would
    > do some extra initialization apart from the one performed
    > by the default c'tor, like
    >
    > Foo::Foo(string filename) {
    > // do something with filename
    > // * now wants to initialize the members in the same
    > // way Foo::Foo() does
    > }
    >
    > Foo::Foo(int n) {
    > // do something with n
    > // * now wants to initialize the members in the same
    > // way Foo::Foo() does
    > }
    >
    > As I can't call the default c'tor from the other c'tors,
    > except for a base-class c'tor, I am faced with a choice
    > of either repeating the initialization lists in all c'tors
    > or moving the initialization to an init() method altogether.
    >
    > But in an init() method I can't use the initialization
    > list, as it is only allowed in a constructor.
    >
    > It somehow turns out that all my classes wind up with
    > at least two constructors and at some point I always have
    > to resort to an init() method and get rid of the initialization
    > lists in favor of a screenful of
    >
    > member1=0;
    > member2=0;
    > member3=NULL;
    > member4=20;
    >
    > lines.
    >
    > Am I missing something or do the initialization-lists turn
    > out to be useless that often?
    >
    > thanks in advance,
    > - J.


    There's no way around it that I know of. The FAQ agrees:

    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.3

    It's particularly annoying when members *must* be initialized there
    because they are references or const.

    Cheers! --M
     
    mlimber, Dec 5, 2005
    #2
    1. Advertising

  3. Jacek Dziedzic wrote:
    > Suppose I have a class Foo that defines a default c'tor
    > that initializes some data using an initialization list:
    >
    > Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20)
    > // and so on, quite a few members
    > {}
    >
    > and does nothing else.
    >
    > Then it turns out that I need two more c'tors that would
    > do some extra initialization apart from the one performed
    > by the default c'tor, like
    >
    > Foo::Foo(string filename) {


    Foo::Foo(const string& filename)

    is preferred.

    > // do something with filename
    > // * now wants to initialize the members in the same
    > // way Foo::Foo() does
    > }
    >
    > Foo::Foo(int n) {
    > // do something with n
    > // * now wants to initialize the members in the same
    > // way Foo::Foo() does
    > }
    >
    > As I can't call the default c'tor from the other c'tors,
    > except for a base-class c'tor, I am faced with a choice
    > of either repeating the initialization lists in all c'tors
    > or moving the initialization to an init() method altogether.
    >
    > But in an init() method I can't use the initialization
    > list, as it is only allowed in a constructor.
    >
    > It somehow turns out that all my classes wind up with
    > at least two constructors and at some point I always have
    > to resort to an init() method and get rid of the initialization
    > lists in favor of a screenful of
    >
    > member1=0;
    > member2=0;
    > member3=NULL;
    > member4=20;
    >
    > lines.
    >
    > Am I missing something or do the initialization-lists turn
    > out to be useless that often?


    No, you're not missing anything. Perhaps you should review your class
    design and possibly wrap those [common] things that you initialise
    similarly into another struct, whose default c-tor would actually keep
    all those other members initialised to 0?...

    V
     
    Victor Bazarov, Dec 5, 2005
    #3
  4. * Jacek Dziedzic:
    >
    > It somehow turns out that all my classes wind up with
    > at least two constructors and at some point I always have
    > to resort to an init() method and get rid of the initialization
    > lists in favor of a screenful of
    >
    > member1=0;
    > member2=0;
    > member3=NULL;
    > member4=20;
    >
    > lines.
    >
    > Am I missing something or do the initialization-lists turn
    > out to be useless that often?


    Yes, you're missing (1) the possibility of default values for arguments,
    and (2) the base data class trick.

    struct Base
    {
    int m1;
    ...
    Base( int v1, and so on )
    };

    struct Foo: Base
    {
    Foo( int v1, and so on ): Base( whatever ) {}
    };

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Dec 5, 2005
    #4
  5. Jacek Dziedzic

    BobR Guest

    Jacek Dziedzic wrote in message
    <4c2fa$4394b510$540a329e$>...
    > Hello!
    > Suppose I have a class Foo that defines a default c'tor
    >that initializes some data using an initialization list:
    >
    >Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20)
    > /* and so on, quite a few members */ {}
    >
    >and does nothing else.
    >
    > Then it turns out that I need two more c'tors that would
    >do some extra initialization apart from the one performed
    >by the default c'tor, like
    >
    >Foo::Foo(string filename) {
    > // do something with filename
    > // * now wants to initialize the members in the same
    > // way Foo::Foo() does
    >}


    If you do:

    class Foo{
    public:
    Foo();
    Foo(std::string const filename = ""); // note the default
    };
    // .......
    /* Foo::Foo definitions */
    // .......
    Foo MyFoo;

    .....your compiler should complain. It can't tell the difference between the
    two C'tors!

    So, could you possibly do?:

    class Foo{
    public:
    Foo(std::string const filename = "", int n = 0);
    // .......
    };
    // .......
    Foo::Foo(std::string const filename, int n) : member1(0),
    member2(0), member3(20), name(filename), number(n) // and so on
    {
    if( not name.empty() ){
    std::ifstream GetFile( name.c_str() );
    /* test, read and store file, whatever */
    }
    if( number ){
    /* do something with number */
    }
    } // Foo constructor
    // .......
    Foo MyFooA;
    Foo MyFooB("MyData.txt");
    Foo MyFooC("MyData.txt", 7);
    Foo MyFooD("", 7); // note: the "" is needed here

    [ untested, but you should get the idea ]
    --
    Bob R
    POVrookie
     
    BobR, Dec 6, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    10
    Views:
    962
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    425
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Matthias Kaeppler
    Replies:
    2
    Views:
    457
    Victor Bazarov
    Jul 18, 2005
  4. Replies:
    6
    Views:
    473
    Ron Natalie
    Dec 11, 2005
  5. toton
    Replies:
    5
    Views:
    945
    Victor Bazarov
    Sep 28, 2006
Loading...

Share This Page