Archived

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

Trediction

C++ class confusion

Recommended Posts

Trediction    122
Say I have a class, for example: class CSprite { public: CSprite(); ~CSprite(){} protected: bool m_bInitialized; int * m_sequences[8]; }; In the constructor I want to initialize this variable to be {0,0,0,0,0,0,0,0} something like this: CSprite::CSprite(): m_bInitialized(false), m_sequences({0,0,0,0,0,0,0,0}) {} This does not work, so I am wondering if anyone knows the correct syntax to perform such an initialization in the constructor... I want to avoid using the actual body of the constructor, by the way... Thanks "Sometimes I think the surest sign that intelligent exists elsewhere in the universe is that none of it has tried to contact us" -Calvin

Share this post


Link to post
Share on other sites
ZealousElixir    256
Not sure if you can do that at all, but I''m more sure you can''t do it if the member is a pointer. If you already know the number of elements, try just making it a static array. Otherwise, unless this is a school assignment, why are you avoiding using the body of the constructor?

Peace,
ZE.

P.S. I''m probably wrong in some of my reasoning. Anyone, feel free to correct.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

Share this post


Link to post
Share on other sites
fallenang3l    144
Array initializer list can be used only when you declare the array, but in your case, it is impossible to do so because it''s a data member of a class. You will have to use a repetition structure inside the ctor to accomplish your task.

Share this post


Link to post
Share on other sites
Melraidin    128
Another person wondering why you''d like to keep it out of the constructor body... I can''t possibly think of any reason for this. Could you explain please?

Share this post


Link to post
Share on other sites
Ready4Dis    180
You could do this...

m_sequences[0]=m_sequences[1]=m_sequences[2]=m_sequences[3]=m_sequences[4]=m_sequences[5]=m_sequences[6]=m_sequences[7]=0;

Or,

memset(m_sequences,0,sizeof(int*)*8);

Or,

for (int ctr=0;ctr!=8;++ctr)
m_sequences[ctr]=0;



  
CSprite::CSprite()
{
m_bInitialized=0;
memset(m_sequences,0,sizeof(int*)*8);
}


Billy - BillyB@mrsnj.com
(Hey, I''''m no longer anonymous!)

Share this post


Link to post
Share on other sites
Dire.Wolf    122
Trediction,

Here is the proper way (in my opinion) to do what you want:


    
#include <vector>


class CSprite
{
public:
CSprite();
~CSprite(){}

protected:
bool m_bInitialized;
std::vector<int*> m_sequences;
};

CSprite::CSprite() : m_bInitialized(false),
m_sequences(8, 0)
{
}


The above code uses a vector to maintain your array of int-pointers and initialises the vector in the constructor. The vector is initialised with 8 elements and sets them to an initial value of 0.

Hopefully this helps you out.

Dire Wolf
www.digitalfiends.com

[edited by - Dire.Wolf on May 3, 2002 12:10:18 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I have a following question. Why not have it like...

int *m_sequences;

m_sequences = new int[8](0);

// dont need this...
// memset(m_sequences, 0, 8);

Share this post


Link to post
Share on other sites