initializing member arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Klaus Rudolph

    initializing member arrays

    Hi all,

    I have 2 classes, the second one should include an
    array of the first on. The first class should not have a default
    constructor. How could I use an array of the first classes?
    If it is impossible to initialize the array before entering the
    constructor , it
    would never be possible to use arrays of classes in C++ whitout using a
    default constructor.
    Is that correct?



    class A {
    protected:
    int a;
    public:
    A(int _a): a(_a) {}
    };

    class B {
    protected:
    A arr[2];
    public:
    B(int _a1, int _a2): arr(??????????? ??) {} //<-------------------
    };

    int main() {
    B b(1,2);
    return 0;
    }

    The marked line is my problem. Is there no way to initialize the array?
    Is using a vector<> the only solution here?

    Regards
    Klaus
  • Ron Natalie

    #2
    Re: initializing member arrays


    "Klaus Rudolph" <lts-rudolph@gmx.de> wrote in message news:4006DA5E.E ED0B518@gmx.de. ..[color=blue]
    > Hi all,
    >
    > I have 2 classes, the second one should include an
    > array of the first on. The first class should not have a default
    > constructor. How could I use an array of the first classes?[/color]

    Can't be done. Arrays are half-baked types in C++. This is
    one of their failings. Your choices are to set the values in the
    body of the constructor, or to replace it with a type (like vector)
    that has reasonable initialization semantics.

    Comment

    • David Fisher

      #3
      Re: initializing member arrays

      "Klaus Rudolph" <lts-rudolph@gmx.de> wrote:
      [color=blue]
      > class A {
      > protected:
      > int a;
      > public:
      > A(int _a): a(_a) {}
      > };
      >
      > class B {
      > protected:
      > A arr[2];
      > public:
      > B(int _a1, int _a2): arr(??????????? ??) {} //<-------------------
      > };
      >
      > int main() {
      > B b(1,2);
      > return 0;
      > }
      >
      > The marked line is my problem. Is there no way to initialize the array?
      > Is using a vector<> the only solution here?[/color]

      You could create a protected default constructor for class A, and make B a
      friend of A ... you might also want an init() function which acts as a
      constructor in this case (unless you are happy to initialise arr[] with
      assignments to temporary objects which use the non-default constructor).

      David F


      Comment

      • Jerry Coffin

        #4
        Re: initializing member arrays

        In article <4006DA5E.EED0B 518@gmx.de>, lts-rudolph@gmx.de says...[color=blue]
        > Hi all,
        >
        > I have 2 classes, the second one should include an
        > array of the first on. The first class should not have a default
        > constructor. How could I use an array of the first classes?
        > If it is impossible to initialize the array before entering the
        > constructor , it
        > would never be possible to use arrays of classes in C++ whitout using a
        > default constructor.
        > Is that correct?[/color]

        Yes, it is.

        [ ... ]
        [color=blue]
        > The marked line is my problem. Is there no way to initialize the array?
        > Is using a vector<> the only solution here?[/color]

        It may not be the only one but it's almost certainly the preferred one.

        --
        Later,
        Jerry.

        The universe is a figment of its own imagination.

        Comment

        Working...