C++ class confusion

Started by
8 comments, last by Trediction 21 years, 11 months ago
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
"Sometimes I think the surest sign that intelligent exists elsewhere in the universe is that none of it has tried to contact us" -Calvin
Advertisement
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.]

[twitter]warrenm[/twitter]

You can''t initialize an array in an initializer list. You''ll have to find some alternative.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
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.
just memset it to zero in your constrcutor. That is prolly the easiest way to initialize them all to 0.

--michael
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?
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!)
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]
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
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);
quote:Original post by Anonymous Poster
I have a following question. Why not have it like...

I usually find the fact that it''s illegal syntax is sufficient to put most people off.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]

This topic is closed to new replies.

Advertisement