Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Intit an Array of objects with ctor

Reply
Thread Tools

Intit an Array of objects with ctor

 
 
peterfarge
Guest
Posts: n/a
 
      03-27-2010
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
 
Reply With Quote
 
 
 
 
Ali Karaali
Guest
Posts: n/a
 
      03-27-2010
On 27 Mart, 20:38, peterfarge <peterfa...@gmx.de> wrote:
> 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


 
Reply With Quote
 
 
 
 
peterfarge
Guest
Posts: n/a
 
      03-27-2010
Oh!
Thanks for the info Ali
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-27-2010
* 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
 
Reply With Quote
 
peterfarge
Guest
Posts: n/a
 
      03-27-2010
Ist an interesting idea. Thanks Alf
 
Reply With Quote
 
Pavel
Guest
Posts: n/a
 
      03-27-2010
peterfarge wrote:
> 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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-28-2010
On Mar 28, 8:15 am, Juha Nieminen <nos...@thanks.invalid> wrote:

[...]
> 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.

--
James Kanze
 
Reply With Quote
 
Vladimir Jovic
Guest
Posts: n/a
 
      04-12-2010
peterfarge wrote:
> 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...doc/index.html


btw why not use vector instead of raw arrays?
 
Reply With Quote
 
Puppet_Sock
Guest
Posts: n/a
 
      04-12-2010
On Mar 27, 7:49*pm, Pavel
<pauldontspamt...@removeyourself.dontspam.yahoo> wrote:
[snip]
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reference to pointer used in ctor (using 'this' in ctor) Anonymous C++ 2 08-28-2007 01:10 PM
copy ctor vs default ctor subramanian100in@yahoo.com, India C++ 2 08-15-2007 10:49 AM
ctor/dtor calls and ctor init seq Grizlyk C++ 8 11-29-2006 06:35 AM
Templates, copy ctor and type-conversion ctor NVH C++ 8 07-06-2006 07:19 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57