Newbie question: arrays of objects

Discussion in 'C++' started by caypea@yahoo.com, Nov 9, 2004.

  1. Guest

    I am having problems declaring an array of objects of a class that
    does not have a default constructor. Could someone post the correct
    syntax please?

    Also for classes that have multiple constructors, is there a way to
    declare an array of objects invoking a specific constructor?

    Ciao,
    Karan
    , Nov 9, 2004
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    >I am having problems declaring an array of objects of a class that
    > does not have a default constructor. Could someone post the correct
    > syntax please?


    There is no syntax.

    >
    > Also for classes that have multiple constructors, is there a way to
    > declare an array of objects invoking a specific constructor?
    >


    It is impossible to declare an array with anything that does not have a
    default constructor, or to invoke any constructor other than the default
    constructor.

    One answer is to use a vector instead. Do you know about vectors?

    john
    John Harrison, Nov 9, 2004
    #2
    1. Advertising

  3. Sharad Kala Guest

    "John Harrison" <> wrote in message >
    > <> wrote in message
    > news:...
    > >I am having problems declaring an array of objects of a class that
    > > does not have a default constructor. Could someone post the correct
    > > syntax please?

    >
    > There is no syntax.
    >
    > >
    > > Also for classes that have multiple constructors, is there a way to
    > > declare an array of objects invoking a specific constructor?
    > >

    >
    > It is impossible to declare an array with anything that does not have a
    > default constructor, or to invoke any constructor other than the default
    > constructor.


    Not quite. It is possible even if it isn't very convenient.

    MyClass arr[3] = {MyClass(2), MyClass(3), MyClass(4)};

    Sharad
    Sharad Kala, Nov 9, 2004
    #3
  4. Sharad Kala Guest

    <> wrote in message
    > I am having problems declaring an array of objects of a class that
    > does not have a default constructor. Could someone post the correct
    > syntax please?


    > Also for classes that have multiple constructors, is there a way to
    > declare an array of objects invoking a specific constructor?


    Yes, even if it's not very convenient.

    struct MyClass{
    MyClass(int i){
    }
    };

    int main()
    {
    MyClass arr[3] = {MyClass(2), MyClass(3), MyClass(4)};
    }

    Sharad
    Sharad Kala, Nov 9, 2004
    #4
  5. "Sharad Kala" <> wrote in message
    news:...
    >
    > "John Harrison" <> wrote in message >
    >> <> wrote in message
    >> news:...
    >> >I am having problems declaring an array of objects of a class that
    >> > does not have a default constructor. Could someone post the correct
    >> > syntax please?

    >>
    >> There is no syntax.
    >>
    >> >
    >> > Also for classes that have multiple constructors, is there a way to
    >> > declare an array of objects invoking a specific constructor?
    >> >

    >>
    >> It is impossible to declare an array with anything that does not have a
    >> default constructor, or to invoke any constructor other than the default
    >> constructor.

    >
    > Not quite. It is possible even if it isn't very convenient.


    Sorry, me being completely stupid. No cup of coffee yet. I was of course
    thinking of dynamic memory allocation not a statically or locally declared
    array.

    >
    > MyClass arr[3] = {MyClass(2), MyClass(3), MyClass(4)};


    Assuming MyClass has a non-explicit constructor then simply

    MyClass arr[3] = {2, 3, 4};

    OP, perhaps you could post the code with which you are having trouble. It's
    the best way to get high quality answers (and helps avoid the odd completely
    incorrect answer like my previous one).

    john
    John Harrison, Nov 9, 2004
    #5
  6. Andre Dajd Guest

    wrote in message news:<>...
    > I am having problems declaring an array of objects of a class that
    > does not have a default constructor. Could someone post the correct
    > syntax please?
    >
    > Also for classes that have multiple constructors, is there a way to
    > declare an array of objects invoking a specific constructor?
    >
    > Ciao,
    > Karan


    You actually, only need copy constructibility, which is provided
    automatically, unless you prohibit it. That is, you can create an
    object of a class somehow and then, provided that a copy-constructor
    is implemented or autogenerated, use the object as the initializer

    struct s
    {
    int i;

    s(int j): i(j) {}

    private:
    s();
    };

    int main()
    {
    s i(10);

    s* x= new s[10](i); // using automatically generated copy
    constructor

    delete[] x;

    return 1;
    }

    This will not work if you deliberately prohibit copy constructor in
    the same way i did prohibit the default one.
    Andre Dajd, Nov 9, 2004
    #6
  7. "Andre Dajd" <> wrote in message
    news:...
    > wrote in message

    news:<>...
    > > I am having problems declaring an array of objects of a class that
    > > does not have a default constructor. Could someone post the correct
    > > syntax please?
    > >
    > > Also for classes that have multiple constructors, is there a way to
    > > declare an array of objects invoking a specific constructor?
    > >
    > > Ciao,
    > > Karan

    >
    > You actually, only need copy constructibility, which is provided
    > automatically, unless you prohibit it. That is, you can create an
    > object of a class somehow and then, provided that a copy-constructor
    > is implemented or autogenerated, use the object as the initializer
    >
    > struct s
    > {
    > int i;
    >
    > s(int j): i(j) {}
    >
    > private:
    > s();
    > };
    >
    > int main()
    > {
    > s i(10);
    >
    > s* x= new s[10](i); // using automatically generated copy
    > constructor
    >


    That is not legal C++. You might have a compiler that accepts it, but that
    doesn't make it legal.

    john
    John Harrison, Nov 9, 2004
    #7
  8. Andre Dajd Guest

    "John Harrison" <> wrote in message news:<>...
    > "Andre Dajd" <> wrote in message
    > news:...
    > > wrote in message

    > news:<>...

    [snip]
    > > struct s
    > > {
    > > int i;
    > >
    > > s(int j): i(j) {}
    > >
    > > private:
    > > s();
    > > };
    > >
    > > int main()
    > > {
    > > s i(10);
    > >
    > > s* x= new s[10](i); // using automatically generated copy
    > > constructor
    > >

    >
    > That is not legal C++. You might have a compiler that accepts it, but that
    > doesn't make it legal.
    >
    > john


    Interesting...

    Standard 5.3.4 (first version, though) apparently provides for this
    form, i.e. you may have optional "new-initializer", which should not
    necessarily be empty, as explained in subsection 15, point 3... Maybe
    I misinterpret it, but this meaning appears quite natural. The
    standard is, indeed, a bit vague there, as it use wording "initializes
    that object", not, ruling out a possibility of combining the array
    declarator with initializer.

    The compiler used was GCC 3.3 under the latest Dev-CPP. Out of
    curiosity, I have added the copy constructor to the above example and
    put a static counter into the class, to see if the copy constructor
    would be called. It was.

    At the same time the code did not compile with VS6 and Comeau (through
    their web interface).

    Anyway, I have to withdraw my comment in it's generality and confine
    it only to the lucky GCC users :)

    Rgds
    d
    Andre Dajd, Nov 9, 2004
    #8
  9. Old Wolf Guest

    "John Harrison" <> wrote:
    >
    > Assuming MyClass has a non-explicit constructor then simply
    >
    > MyClass arr[3] = {2, 3, 4};
    >


    Does that always work?

    Here is my code: (Sorry if this is a report -- I posted a few
    days ago under another thread but it didn't seem to show up).

    #include <string>

    typedef std::string M[3][2];
    //typedef char M[3][2][33];

    struct X
    {
    M a;
    };

    int main()
    {
    M p = { {"a","b"}, {"c","d"}, {"e","f"} };
    M q = { {"a","b"}, {"c","d"}, {"e"} };
    M r = { {"a","b"}, {"c","d"} };

    X x = { { {"a","b"}, {"c","d"}, {"e","f"} } };
    X y = { { {"a","b"}, {"c","d"}, {"e"} } };
    X z = { { {"a","b"}, {"c","d"} } };
    }

    g++ 3.4.1 compiles p,q,r,x,y OK but gives an ICE on z.
    bcc 5.5.1 compiles p,q, ICEs on r, and says 'Syntax error' on x,y,z.
    If I change the typedef back to char from string, it all works.
    Old Wolf, Nov 10, 2004
    #9
  10. "Old Wolf" <> wrote in message
    news:...
    > "John Harrison" <> wrote:
    >>
    >> Assuming MyClass has a non-explicit constructor then simply
    >>
    >> MyClass arr[3] = {2, 3, 4};
    >>

    >
    > Does that always work?
    >
    > Here is my code: (Sorry if this is a report -- I posted a few
    > days ago under another thread but it didn't seem to show up).
    >
    > #include <string>
    >
    > typedef std::string M[3][2];
    > //typedef char M[3][2][33];
    >
    > struct X
    > {
    > M a;
    > };
    >
    > int main()
    > {
    > M p = { {"a","b"}, {"c","d"}, {"e","f"} };
    > M q = { {"a","b"}, {"c","d"}, {"e"} };
    > M r = { {"a","b"}, {"c","d"} };
    >
    > X x = { { {"a","b"}, {"c","d"}, {"e","f"} } };
    > X y = { { {"a","b"}, {"c","d"}, {"e"} } };
    > X z = { { {"a","b"}, {"c","d"} } };
    > }
    >
    > g++ 3.4.1 compiles p,q,r,x,y OK but gives an ICE on z.
    > bcc 5.5.1 compiles p,q, ICEs on r, and says 'Syntax error' on x,y,z.
    > If I change the typedef back to char from string, it all works.


    Should do, although because some of your initialisers are 'short' you need a
    default constructor for the missing items (std::string has this of course).
    VC++ 7.1 and Comeau compile all you examples without problems.

    john
    John Harrison, Nov 10, 2004
    #10
    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. Alexandra Stehman
    Replies:
    5
    Views:
    30,589
    Chris Smith
    Jun 17, 2004
  2. Robert
    Replies:
    5
    Views:
    353
    Grizlyk
    Jan 14, 2007
  3. 7stud
    Replies:
    11
    Views:
    684
    Dennis Lee Bieber
    Mar 20, 2007
  4. Philipp
    Replies:
    21
    Views:
    1,123
    Philipp
    Jan 20, 2009
  5. ThomasW
    Replies:
    11
    Views:
    282
    ThomasW
    Sep 28, 2009
Loading...

Share This Page