Intit an Array of objects with ctor

Discussion in 'C++' started by peterfarge, Mar 27, 2010.

  1. peterfarge

    peterfarge Guest

    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
    peterfarge, Mar 27, 2010
    #1
    1. Advertising

  2. peterfarge

    Ali Karaali Guest

    On 27 Mart, 20:38, 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
    >

    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
    Ali Karaali, Mar 27, 2010
    #2
    1. Advertising

  3. peterfarge

    peterfarge Guest

    Oh! :(
    Thanks for the info Ali :)
    peterfarge, Mar 27, 2010
    #3
  4. * 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
    Alf P. Steinbach, Mar 27, 2010
    #4
  5. peterfarge

    peterfarge Guest

    Ist an interesting idea. Thanks Alf :)
    peterfarge, Mar 27, 2010
    #5
  6. peterfarge

    Pavel Guest

    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
    Pavel, Mar 27, 2010
    #6
  7. peterfarge

    James Kanze Guest

    On Mar 28, 8:15 am, Juha Nieminen <> 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
    James Kanze, Mar 28, 2010
    #7
  8. 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/libs/assign/doc/index.html


    btw why not use vector instead of raw arrays?
    Vladimir Jovic, Apr 12, 2010
    #8
  9. peterfarge

    Puppet_Sock Guest

    On Mar 27, 7:49 pm, Pavel
    <> 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
    Puppet_Sock, Apr 12, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apricot
    Replies:
    4
    Views:
    523
    velthuijsen
    Apr 16, 2004
  2. NVH
    Replies:
    8
    Views:
    484
    mlimber
    Jul 6, 2006
  3. Grizlyk
    Replies:
    8
    Views:
    481
    Grizlyk
    Nov 29, 2006
  4. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    405
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
  5. Anonymous
    Replies:
    2
    Views:
    375
    Victor Bazarov
    Aug 28, 2007
Loading...

Share This Page