Declare array of objects with constructor having arguments

Discussion in 'C++' started by b83503104@yahoo.com, Jul 20, 2005.

  1. Guest

    Previously, when my constructor had no arguments, I used this to
    declare my objects:
    MyClass myObject[3];

    Now, my constructor has arguments (MyClass::MyClass(int someVariable)),
    how do I declare my objects?
    I tried

    MyClass myObject[3](100);
    and

    MyClass myObject(100)[3];

    Neither of them work.
    I also want to avoid using new (if that is the solution), because (?!)
    then I have to write a destructor (?!).

    Thanks in advance
    , Jul 20, 2005
    #1
    1. Advertising

  2. * :
    > Previously, when my constructor had no arguments, I used this to
    > declare my objects:
    > MyClass myObject[3];
    >
    > Now, my constructor has arguments (MyClass::MyClass(int someVariable)),
    > how do I declare my objects?
    > I tried
    >
    > MyClass myObject[3](100);
    > and
    >
    > MyClass myObject(100)[3];
    >
    > Neither of them work.


    MyClass objects[] = {100, 200, 300};


    > I also want to avoid using new (if that is the solution), because (?!)
    > then I have to write a destructor (?!).


    No, you don't have to, unless there are external resources to free. The
    compiler supplies a destructor for you. And that destructor is usually
    enough.

    --
    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, Jul 20, 2005
    #2
    1. Advertising

  3. Zorro Guest

    I have not recently used C++ to that level of detail. So your insight
    is appreciated.

    In the past, I found no mechanism (elegant or otherwise) to do the same
    when a constructor takes several arguments, especially of user-defined
    types. For that reason, I extended C++ to do something like this:

    MyClass objects[n](arg1, arg2);

    As I remember, this was not possible in C++ in any form (as
    initialization in the course of a construction, as you are showing in
    this post).

    Naturally, I am more interested in knowing that it cannot be done in
    C++. But I really do not know that for sure.

    Regards,
    Z.
    Zorro, Jul 20, 2005
    #3
  4. * Zorro:
    > I have not recently used C++ to that level of detail. So your insight
    > is appreciated.
    >
    > In the past, I found no mechanism (elegant or otherwise) to do the same
    > when a constructor takes several arguments, especially of user-defined
    > types. For that reason, I extended C++ to do something like this:
    >
    > MyClass objects[n](arg1, arg2);


    Really? You found it so hard to figure out the syntactical rules of C++
    that instead you did the much easier thing, delving into the source code of
    your nearest compiler and _extending_ the language?


    > As I remember, this was not possible in C++ in any form (as
    > initialization in the course of a construction, as you are showing in
    > this post).


    Exactly what is it you want to achieve? A variable 'n'? Initialization
    with arg1 and arg2 of every element?

    Then use a std::vector.


    > Naturally, I am more interested in knowing that it cannot be done in
    > C++. But I really do not know that for sure.


    Hm.

    --
    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, Jul 21, 2005
    #4
  5. Zorro Guest

    Sorry about the delay, I had to participate in bible study.

    I was hoping to get a yes here is how from someone, or no response at
    all. I must politely reply to you, however. I am not sure how else I
    could have posted the question. The 'n' was not intentional, but now
    that you mentioned it, yes, I have extended C++ in that direction as
    well (these are dynamic arrays as apart from using new, and "delete
    []", more like ADA's).

    The only source for compiler that I dealt with was the experimental
    compiler between the years of 1988 and 1990. It was not possible to get
    this (an a lot more) into that hack. For that reason I set out to do my
    own, which took more than 6 years.

    I cannot say that I do not know about STL, but I have never used it.
    Since 1991 I have used my own template library that I did for teaching
    a course in C++ (using the experimental compiler).

    I got too old to chase journals for techniques, so I thought you may
    have something for this case. Or, if I got no answer, I would know I
    have something not yet easily possible in C++.

    I tried to state the question as well as I could. The issue was
    initializing an array using a non-trivial constructor. You indicated a
    way of doing it for integers. Actually, I did not know that could be
    done when the cells of array were classes (and I have not tried it
    yet). Evidently the compiler is calling the constructor on each cell
    using the sequence of numbers. Well then, may be there is a way to do
    it for more complicated constructors.

    The extension I am speaking of is not extending a particular source for
    a compiler. I dropped that in 1990. I was speaking of extensions to the
    language C++.

    Thanks for your time.

    Regards,
    Dr. Z.
    Chief Scientist

    http://www.zhmicro.com
    http://distributed-software.blogspot.com
    Zorro, Jul 21, 2005
    #5
  6. Guest

    Thanks a lot. Since I cannot find this topic in my book, I have a
    question on how to extend this to a constructor with 2 variables like
    this:

    MyClass::MyClass(int Var1, int Var2);

    Is it like this?

    MyClass objects[ ] = {{100, 200}, {300, 400}, {500, 600}};

    Thanks again.
    , Jul 21, 2005
    #6
  7. * :
    > Thanks a lot. Since I cannot find this topic in my book,


    Which book is that?


    > I have a
    > question on how to extend this to a constructor with 2 variables like
    > this:
    >
    > MyClass::MyClass(int Var1, int Var2);
    >
    > Is it like this?
    >
    > MyClass objects[ ] = {{100, 200}, {300, 400}, {500, 600}};
    >


    No. You can use that syntax for POD types, essentially types you could
    have defined in C if C had the same repertoire of basic types as C++. But
    when you have a constructor (non-POD) you'll have to do something like

    MyClass objects[] = {
    MyClass( 100, 200 ), MyClass( 300, 400 ), MyClass( 500, 600 )
    };

    Using raw arrays there's no way to specify a repeat of a given value n
    times, but you can do that using a std::vector:

    std::vector<MyClass> objects( n, MyClass( 123, 456 ) );

    Using a std::vector there is, on the other hand, no way to specify a list of
    specific values like with a raw array, so one solution when that is required
    and you want to use a std::vector is to specify the list of initial values
    as a raw array constant, and use that to initialize the std::vector. The
    Boost library has at least one other solution, as I understand it based on
    dynamically constructing a list of values. It just gives a shorter spec.

    --
    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, Jul 21, 2005
    #7
  8. "Alf P. Steinbach" <> wrote in message
    news:...

    >* :


    >> I also want to avoid using new (if that is the solution), because
    >> (?!)
    >> then I have to write a destructor (?!).

    >
    > No, you don't have to, unless there are external resources to free.
    > The
    > compiler supplies a destructor for you. And that destructor is
    > usually
    > enough.



    I would like to add, that having to write a dtor does not have
    anything to do with using new or new[]. If you have external resources
    to free, you need a dtor in either case.

    regards
    --
    jb

    (reply address in rot13, unscramble first)
    Jakob Bieling, Jul 21, 2005
    #8
    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. Philipp
    Replies:
    5
    Views:
    6,460
    Ron Natalie
    Oct 16, 2003
  2. Andy Buckley
    Replies:
    5
    Views:
    335
    Victor Bazarov
    Aug 26, 2004
  3. BillJosephson
    Replies:
    5
    Views:
    1,623
    Thomas Hawtin
    Dec 14, 2006
  4. Replies:
    16
    Views:
    487
    Mike Smith
    Jul 11, 2005
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,217
Loading...

Share This Page