Boost multi_array

Started by
6 comments, last by Zahlman 17 years, 1 month ago
What I want is a multi_array as a class member which I initialize in the constructor. Unfortunately when I attempt to do this the program crashes immediately.

class CTerrainMap
{
private:
boost::multi_array<float, 2> heightmap;

int m_ns; //north-south map size
int m_ew; //east-west map size
public:
CTerrainMap(int ns,int ew) : m_ns(ns),m_ew(ew)
{
  heightmap = boost::multi_array<float, 2>(boost::extents[m_ns][m_ew]);
}
};

Probably something to do with it making a copy of the multi_array on the local stack. But if I cannot do it this way, how can I do it? I can get it to work if the member variable is a pointer to a multi_array boost::multi_array<float, 2>* heightmap; heightmap = new boost::multi_array<float, 2>(boost::extents[m_ns][m_ew]); But then I have to access array elements using (*heightmap)[3][5] Couldn't find a solution on google or searching the forums..
Advertisement
CTerrainMap(int ns,int ew) : m_ns(ns),m_ew(ew),heightmap(boost::extents[ns][ew]{}};

I'm not sure if that will work, but maybe something like that might work.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

You are right this does work, thanks, but I am still interested if there is a way to initialize the multi_array in the constructor body so that it will work like it does when in the initialization list.
A boost::multi_array does have modifier functions, such as resize() and reshape(), that might suit your needs better than a simple assignment.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
I also just noticed on boost::multi_array documentation for operator = that a precondition is that:
std::equal(this->shape(),this->shape()+this->num_dimensions(),x.shape());

Thus, the array target needs to be the same shape as the source. The assignment doesn't really assign the overall source to the target, just the source elements. So you were getting the crash because the default-constructed array was a different shape than the temporary array on the stack.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Quote:Original post by hydrogen
You are right this does work, thanks, but I am still interested if there is a way to initialize the multi_array in the constructor body so that it will work like it does when in the initialization list.

You can't initialize the object in the constructor body. Initialization only happens once, and it happens before the body is entered. The array will be initialized by its default constructor.

Why don't you want to use initialization lists? You should really be using them if you can. See this page for more info.

[Edited by - MumbleFuzz on March 4, 2007 5:44:38 PM]
MumbleFuzz
Thanks, initialization list works fine. Agony's explaination explains why the initialization in the body failed. I just wanted to make sure there wasn't a common initialization method I didn't know about that would allow me the initialize it in the body (well re-initialize it).

Quote:Original post by hydrogen
Thanks, initialization list works fine. Agony's explaination explains why the initialization in the body failed. I just wanted to make sure there wasn't a common initialization method I didn't know about that would allow me the initialize it in the body (well re-initialize it).


The point was, there is *no* "initialization method", common or not, that allows you to initialize *anything* in the constructor body, because that's not what initialization is. The body is additional setup code that is called to initialize *the object*; initialization of *each member* is a separate concept, which is handled using the initialization list if at all.

This topic is closed to new replies.

Advertisement