Intit an Array of objects with ctor

P

peterfarge

Hello People,

I need a hint: I want to create a game map. Every place on this map is
a PlaceClass. This PlaceClass has one ctor with a pointer to a init
object. Now I want to create my map as a 2D of PlaceClass

class Map {
public::
Map(cInit *ptrInitObj) : m_map[100][100](ptrInitObj) { // <-- How to
init the array?
}

private:
PlaceClass m_map[100][100];
};


Thanks

Peter
 
A

Ali Karaali

Hello People,

I need a hint: I want to create a game map. Every place on this map is
a PlaceClass. This PlaceClass has one ctor with a pointer to a init
object. Now I want to create my map as a 2D of PlaceClass
There isn't any :-( sorry

Ali
class Map {
public::
 Map(cInit *ptrInitObj) : m_map[100][100](ptrInitObj) { // <-- How to
init the array?
 }

private:
 PlaceClass m_map[100][100];

};

Thanks

Peter
 
A

Alf P. Steinbach

* peterfarge:
Hello People,

I need a hint: I want to create a game map. Every place on this map is
a PlaceClass. This PlaceClass has one ctor with a pointer to a init
object. Now I want to create my map as a 2D of PlaceClass

class Map {
public::
Map(cInit *ptrInitObj) : m_map[100][100](ptrInitObj) { // <-- How to
init the array?
}

private:
PlaceClass m_map[100][100];
};

It can look like this:

#include <vector>

// ... blah blah

typedef std::vector<Place> PlaceVec;
typedef std::vector<PlaceVec> PlaceMatrix;

class Map
{
public:
Map( Init const& initObject ):
myMap( 100, PlaceVec( 100, initObject ) )
{}
private:
PlaceMatrix myMap;
};


Cheers & hth.,

- Alf
 
P

Pavel

peterfarge said:
Hello People,

I need a hint: I want to create a game map. Every place on this map is
a PlaceClass. This PlaceClass has one ctor with a pointer to a init
object. Now I want to create my map as a 2D of PlaceClass

class Map {
public::
Map(cInit *ptrInitObj) : m_map[100][100](ptrInitObj) { //<-- How to
init the array?
}

private:
PlaceClass m_map[100][100];
};


Thanks

Peter
Functionally, the way suggested by Alf should work fine, but if you want
to squeeze the maximum performance, consider keeping a memory as a
1-dimensional vector (of 10000, in your case). This way, you will avoid
an extra indirection when accessing a field. (vector of vectors is
actually more flexible than the rectangular game field you want: you can
have every of lower-level vectors of different size; but this unwanted
flexibility comes at little space and sometimes significant time costs).

Just 2c,
-Pavel
 
J

James Kanze

[...]
If you *really* need it to be a member array (rather than a
dynamically allocated one, eg. by using std::vector), for example for
efficiency reasons (although in this particular case I suspect
efficiency is not a concern, as I assume this class will probably be
instantiated eg. once per game level or such, ie. the efficiency of its
instantiation being a few hundreds of clock cycles slower is completely
irrelevant), it is possible to construct the objects inside the array
"in-place", but it's not completely trivial.
It happens by using a byte array of the proper size and then in the
constructor of the class initializing its members using placement-new.
The destructor of the class must destroy the objects in the array
explicitly (because it won't happen automatically). Accessing the array
has to be done using a reinterpret cast.
While it's *possible* to do this, it's very error-prone and thus not
recommended unless you really, *really* need to do it like that. You are
probably better just using a std::vector.

In addition to Juha's warnings, it's also necessary to ensure
proper alignment: if you just declare a byte array, the compiler
won't do it for you.
 
V

Vladimir Jovic

peterfarge said:
Hello People,

I need a hint: I want to create a game map. Every place on this map is
a PlaceClass. This PlaceClass has one ctor with a pointer to a init
object. Now I want to create my map as a 2D of PlaceClass

class Map {
public::
Map(cInit *ptrInitObj) : m_map[100][100](ptrInitObj) { // <-- How to
init the array?
}

private:
PlaceClass m_map[100][100];
};

Take a look here:
http://www.boost.org/doc/libs/1_42_0/libs/assign/doc/index.html


btw why not use vector instead of raw arrays?
 
P

Puppet_Sock

On Mar 27, 7:49 pm, Pavel
Functionally, the way suggested by Alf should work fine, but if you want
to squeeze the maximum performance, consider keeping a memory as a
1-dimensional vector (of 10000, in your case). This way, you will avoid
an extra indirection when accessing a field. (vector of vectors is
actually more flexible than the rectangular game field you want: you can
have every of lower-level vectors of different size; but this unwanted
flexibility comes at little space and sometimes significant time costs).

Ok, it's not obvious to me that myData[x][y] will be slower
than yourData[x*width+y]. It's also not obvious it won't be.
Especially if you have to do some passing of values into
functions that de-ref the array. And depending on how much
data is actually contained in the arrray, and if the platform
has to do some work (swapping etc.) to get data that is very
far away.

Before choosing between these two, some considerations:
- Is there in fact a difference at all? And is it significant?
- How important is the relative difference to the performance
of the specific application?

This would require some actual benchmark tests on the
platform involved, using test code that was as realistic
as reasonably achievable. And it would require some
profiling work on the code to find out if it spends a
lot of time in this specific aspect. If it does just
*tonnes* of references into the array then it *might*
be worthwhile. On the other hand, if it does one
reference into the array each time the player makes
a move, then you might not see any noticible improvement
from the player's point of view.

Which brings up another point: It may be that the code
is easier for the current crop of coders to read if one
or the other form is used. So either way might easily
be preferred from the point of view of maintenance.
Maintenance may, or may not, be hugely important to a
game code. Frequently game code is pitched out and you
start over for the next version, because frequently you
are trying to cozy up to the fastest game machines.

So, with respect to this suggestion, it depends.
Socks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top