• Popular Now

• 13
• 16
• 27
• 9
• 9

Archived

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

C++ class confusion

This topic is 5797 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 on other sites
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.//

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

Share on other sites
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! ]

Share on other sites
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 on other sites
just memset it to zero in your constrcutor. That is prolly the easiest way to initialize them all to 0.

--michael

Share on other sites
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 on other sites
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 on other sites
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 on other sites
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 on other sites
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! ]