Sign in to follow this  
derek7

vector

Recommended Posts

derek7    100
if I have a set object how to call constructor ? like this; If just one object I know : skinnedmesh skin(filename,.....); but this way ,how to do ? skinnedmesh skinmesh[10]??

Share this post


Link to post
Share on other sites
Roboguy    794
Assuming I've understood your question properly, something like this (also, what does this have to do with vector?) [untested, but it should work]:

for (int i = 0; i < 10; i++) {
skinmesh[i] = skinnedmesh(filename, /* etc */);
}

Share this post


Link to post
Share on other sites
Programmer16    2321
Roboguy's way will work (I tested it) and here is another way to do it (also tested):

class WithCon
{

public:
int a;

WithCon() { a = 0; }
WithCon(int b) { a = b; }
};

WithCon array[10] = {WithCon(1), WithCon(2), WithCon(3), WithCon(4), WithCon(5), WithCon(6), WithCon(7), WithCon(8), WithCon(9), WithCon(10) };

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
for(unsigned int nIndex = 0; nIndex < 10; ++nIndex)
{
char str[4];
itoa(array[nIndex].a, str, 10);
MessageBox(0, str, 0, MB_OK);
}
return 0;
}

Share this post


Link to post
Share on other sites
BitMaster    8651
Roboguy's way does only work if the classes in question have a default constructor and a meaningful assignment operator. In non-trivial cases, that might require making complex deep copies of objects or implementing some kind of reference count. That does not invalidate what Roboguy is saying but it's a limitation that should be added.

Programmer16: Your suggestion does not have that problem (although I must admit a personal dislike to static initializer lists), but it fails if you want to use a std::vector instead or need to allocate an array of dynamic length.

In general, there is no good way to do this. It might be sensible to use Roboguy's method and just rely on assignment operators. It might be better to use an array of pointers instead and manually allocate the instances you need.

Share this post


Link to post
Share on other sites
Programmer16    2321
Quote:
Original post by BitMaster
Roboguy's way does only work if the classes in question have a default constructor and a meaningful assignment operator. In non-trivial cases, that might require making complex deep copies of objects or implementing some kind of reference count. That does not invalidate what Roboguy is saying but it's a limitation that should be added.

Programmer16: Your suggestion does not have that problem (although I must admit a personal dislike to static initializer lists), but it fails if you want to use a std::vector instead or need to allocate an array of dynamic length.

In general, there is no good way to do this. It might be sensible to use Roboguy's method and just rely on assignment operators. It might be better to use an array of pointers instead and manually allocate the instances you need.


I tested Roboguy's way and I didn't use an assignment operator.
I also dislike using static initializer lists, but that is what he asked for.
I'd also like to point out that my way will not work unless it is used when the array is being defined.
I also agree with your array of pointers method.
You mean something like this, correct?:

class WithCon
{

public:
int i;
WithCon(){}
WithCon(int _i){i = _i;}
};

WithCon** pPointers = 0;

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
pPointers = new WithCon*[10];
for(unsigned int nIndex = 0; nIndex < 10; ++nIndex)
{
pPointers[nIndex] = new WithCon(nIndex);
}

for(nIndex = 0; nIndex < 10; ++nIndex)
{
char str[4];
itoa(pPointers[nIndex]->i, str, 10);
MessageBox(0, str, 0, MB_OK);
}

for(nIndex = 0; nIndex < 10; ++nIndex)
{
delete pPointers[nIndex];
}
delete pPointers;
return 0;
}


Share this post


Link to post
Share on other sites
BitMaster    8651
Quote:
Original post by Programmer16
I tested Roboguy's way and I didn't use an assignment operator.


It will compile without an explicit assignment operator defined, correct. However, the automatically generated assignment operator and copy constructor is limited and can in general cases cause problems.
A very simple example to this problem is allocating data in the constructor and deleting it inside the destructor. After the first object is destroyed, all copies of it point to an invalid memory location and they will try to free this invalid memory location in their destructor as well.
Of course, you can avoid this problem but you need to think about it before you allow copy constructors/assignment operators. This is one of the reason why boost::noncopyable exists: there are cases where it's much simpler to forbid copying at all.

Share this post


Link to post
Share on other sites
Programmer16    2321
Quote:
Original post by BitMaster
Quote:
Original post by Programmer16
I tested Roboguy's way and I didn't use an assignment operator.


It will compile without an explicit assignment operator defined, correct. However, the automatically generated assignment operator and copy constructor is limited and can in general cases cause problems.
A very simple example to this problem is allocating data in the constructor and deleting it inside the destructor. After the first object is destroyed, all copies of it point to an invalid memory location and they will try to free this invalid memory location in their destructor as well.
Of course, you can avoid this problem but you need to think about it before you allow copy constructors/assignment operators. This is one of the reason why boost::noncopyable exists: there are cases where it's much simpler to forbid copying at all.


Yeah, duh. I feel really stupid right now (I forgot about the genereated assignment operator).

Share this post


Link to post
Share on other sites
Fruny    1658
You can't pass constructor parameters to an array-new. Only the default constructor can be used. Sorry.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this