Jump to content
  • Advertisement
Sign in to follow this  
Wardyahh

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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, </pedantry> 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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!