multiple constructors and class reference variables

Discussion in 'C++' started by Neil Zanella, Apr 1, 2004.

  1. Neil Zanella

    Neil Zanella Guest

    Hello,

    AFAIK the only way to initialize a reference variable defined inside a class
    is to initialize it in an initializer list. However, when there are multiple
    constructors, this means that the initializer lists have to be cut and pasted
    from one constructor to another. This does not seem to lend itself particularly
    well to maintainablility. Calling a constructor from another in C++ is not legal
    unlike in Java. Also, functions other than constructors cannot have initializer
    lists. So, if there are multiple constructors, it seems to me that the references
    need be copied and constructed the same in each initializer list. This seems
    somewhat tedious. Of course, one could always use the preprocessor and
    declare the list of common reference initializers in a macro and
    then paste the macro instead. I don't regard this particuar
    style very good either.

    Comments welcome,

    Regards,

    Neil
     
    Neil Zanella, Apr 1, 2004
    #1
    1. Advertising

  2. > AFAIK the only way to initialize a reference variable defined inside a
    class
    > is to initialize it in an initializer list. However, when there are

    multiple
    > constructors, this means that the initializer lists have to be cut and

    pasted
    > from one constructor to another. This does not seem to lend itself

    particularly
    > well to maintainablility. Calling a constructor from another in C++ is not

    legal
    > unlike in Java. Also, functions other than constructors cannot have

    initializer
    > lists. So, if there are multiple constructors, it seems to me that the

    references
    > need be copied and constructed the same in each initializer list. This

    seems
    > somewhat tedious. Of course, one could always use the preprocessor and
    > declare the list of common reference initializers in a macro and
    > then paste the macro instead. I don't regard this particuar
    > style very good either.


    You might create an additional constructor as a constructor in a second
    class:

    class a {
    int & n;
    public:
    a(int & i):
    n(i)
    {
    ...
    }
    };

    class b: public a {
    public:
    b(int & i; int j):
    a(i)
    {
    ...
    }
    };

    This would actually work as if the constructors can call each other (which
    they do).

    Niels Dybdahl
     
    Niels Dybdahl, Apr 1, 2004
    #2
    1. Advertising

  3. "Neil Zanella" <> wrote in message
    news:...
    > Hello,
    >
    > AFAIK the only way to initialize a reference variable defined inside a

    class
    > is to initialize it in an initializer list. However, when there are

    multiple
    > constructors, this means that the initializer lists have to be cut and

    pasted
    > from one constructor to another. This does not seem to lend itself

    particularly
    > well to maintainablility. Calling a constructor from another in C++ is not

    legal
    > unlike in Java. Also, functions other than constructors cannot have

    initializer
    > lists. So, if there are multiple constructors, it seems to me that the

    references
    > need be copied and constructed the same in each initializer list. This

    seems
    > somewhat tedious. Of course, one could always use the preprocessor and
    > declare the list of common reference initializers in a macro and
    > then paste the macro instead. I don't regard this particuar
    > style very good either.
    >
    > Comments welcome,
    >
    > Regards,
    >
    > Neil


    Don't put references in classes - use pointers.
    - people reading the impl wont realize it is a ref
    - you can't copy references so you can't implement copy ctor or assignment
    properly
     
    Nick Hounsome, Apr 1, 2004
    #3
    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. Dave Rudolf
    Replies:
    12
    Views:
    8,401
    Martijn Lievaart
    Feb 6, 2004
  2. Neil Zanella
    Replies:
    0
    Views:
    308
    Neil Zanella
    Apr 1, 2004
  3. Neil Zanella
    Replies:
    0
    Views:
    261
    Neil Zanella
    Apr 1, 2004
  4. Jeremy Smith
    Replies:
    2
    Views:
    622
    Jeremy Smith
    Aug 3, 2006
  5. Peng Yu
    Replies:
    5
    Views:
    411
    Juha Nieminen
    Sep 19, 2008
Loading...

Share This Page