Array of objects with no default constructor

I searched around the internet ..couldn't find out if there's any way to create an array of objects from a class with no default constructor. Couldn't find any way of doing it--Does anyone know if it's even possible. I know I could give the class's constructor a default parameter but I want to know if it's possible to feed parameters in some kind of initializer list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

class a{
    public:
        a(string s){;}

    };

int main(){

//ANY WAY OF USING AN INITIALIZER LIST ON NEXT LINE?
a arrayOfA[10];

return 1;
}
No, it is not possible. You must have a default constructor.

That isn't such a big deal, really:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

class a {
    public:
        a( const std::string& s = std::string() ):
            a_s( s )
            { }
    private:
        std::string a_s;
    };

int main() {

    using namespace std;

    a arrayOfA[5];

    ...

    return 0;
    }

Please be consistent with indentation.

The main() function should return zero for success, or non-zero (one, typically) for failure.

Hope this helps.
thanks !
sorry about indents
Yer welcome.

Don't be sorry about indents, just be consistent -- for your own good, get into good indentation habits early. That's all.

:-)
Topic archived. No new replies allowed.