Archived

This topic is now archived and is closed to further replies.

edotorpedo

Initialising array of pointers

Recommended Posts

edotorpedo    198
I have an array of 4 pointers in a class called QuadTree. In my constructor, how do I initialize each of these pointers to NULL class QuadTree { public: QuadTree(); ~QuadTree(); private: QuadTree *children[4]; } Like this: QuadTree::QuadTree():children(NULL) {} or like this: QuadTree::QuadTree() { for (int i = 0; i < 4; i++) children = NULL; } Thanks, Edo

Share this post


Link to post
Share on other sites
nail85    122
Like this:

class QuadTree {

public:
QuadTree();
~QuadTree();

private:
(QuadTree*) children[4];

}

QuadTree::QuadTree() {

for (int i = 0; i < 4; i++) children = NULL;

}

Share this post


Link to post
Share on other sites
Trienco    2555
quote:
Original post by edotorpedo
QuadTree::QuadTree():children(NULL) {}



bad idea, will only set the pointer to the pointers to null

for-loop. does the right thing and a good compiler will probably optimize it.

memset(children, 0, 4*sizeof(QuadTree*));
probably too "low level" for some peoples taste *g*

[edited by - Trienco on January 30, 2003 12:21:24 PM]

Share this post


Link to post
Share on other sites
Bullmax    122
ZeroMemory( Children, sizeof( QuadTree ) << 2 );



/* Bullmax */
-------------
Reality has many forms : good and evil, black and white, yin and yang.

Share this post


Link to post
Share on other sites