News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

C++ array tips - Array construction and intiialization

Started by ben2ong2, October 06, 2006, 04:55:24 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2

PROBLEM: Josh Mittleman (mittle@watson.ibm.com), 17 Sep 92

According to ARM, an array of class objects can be declared with an
initializer list (p.289), but cannot be allocated with new only if the
class has a default constructor (p.61).  Thus:

class thing {
  public:
    thing(int);
    thing(char*, int = 0);
};

thing s[3] = { 1, "hello", thing("bye", 3) };  // correct
thing *t = new thing[3];  // error: no thing::thing()


class thing2 { /* no explicit constructor ... */ };

thing2 u[3];                // correct
thing2 *v = new thing2[3];  // correct


[ In summary, global/automatic arrays can have initializers, but
  heap-allocated arrays cannot. These use the default constructor
  (which must be supplied). -adc ]


You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login