# Non-Standard Constructors

This topic is 4058 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey Guys(and Girls) have hit a snag with a manager i'm writing if you create a basic class such as
class Foo{
private:
public:
Foo(int input);
int Bar;
}

Foo::Foo(int input){
Bar = Input;
}


and create an array of them on the heap
Foo* m_FooArray = new Foo[100];


can you initialise them all with the same non standard constructor? something like this(but this particular syntax doesnt work)
Foo* m_FooArray = new Foo[100](27);


i've found it possible to get the same thing done other ways ie: add a default constructor and a function to do the same thing as the constructor,then cycle through each entry and use that function: like so
class Foo{
private:
public:
BarInit(i);
int Bar;
}

Foo::BarInit(int input){
Bar = Input;
}

Foo* m_FooArray = new Foo[100];

for(int i = 0;i<256;i++){
Foo->BarInit(27);
}


but it just seems so inelegant Thank you in advance for any help ps: if the same thing can be done with std::vector i am open to that as well

##### Share on other sites
Array elements are initialized with the default constructor; there is no away around that. You must take the appropriate extra steps to assign each element the appropriate values, or use a std::vector (which you should be doing anyway):
std::vector< T > listOfT(numberOfElements,T(parameters,to,Ts,constructor));

The above creates a vector of T with numberOfElements copies of the specified T object.

##### Share on other sites
Thank you

The std::vector consrtuctor initialisation thing is exactly what i was looking for

##### Share on other sites
Quote:
 Array elements are initialized with the default constructor; there is no away around that.

Technically untrue; see placement new. But, well, I don't consider placement new "elegant" as the initial post requested and the vector solution is better anyway, so, </pedantry> and on your way...

##### Share on other sites
Quote:
 Technically untrue; see placement new. But, well, I don't consider placement new "elegant" as the initial post requested and the vector solution is better anyway, so, and on your way...

Placement new does not allow you to initialize array elements wholesale. It allows you to construct a new object reusing the memory of an old, dead object or using a raw pool of memory. These are not the same thing at all.

##### Share on other sites
I did not claim that they were the same. Merely that placement new can be used to construct objects in an array with a non-default constructor, which the previous poster had said was impossible (in fact it's the only way to have objects that do not have a default constructor in an array); at no time did I say or imply that this meant you could construct such objects wholesale. In fact, the fact you still can't do that is exactly what I meant when I said that it's not an elegant solution to the initial problem.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631781
• Total Posts
3002319
×