• Advertisement
Sign in to follow this  

initilse array in a constructor?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

class foo()
{
public :
     foo();
     ~foo()

private:
     int mCount;
     int  mData[5];

};

foo::foo():
mCount(0)
{


}
 

 

 

 

how can i use the same type of inilitisation with mData as i do with mCount

 

i know you can do mData() , but this give a warning , and will not let me  inilise it to anything else

Edited by the dodger uk

Share this post


Link to post
Share on other sites
Advertisement
You can’t without C++0x.
Even if you wrapped the array inside a structure, you could initialize the structure with an instance of another structure, even a temporary, but inside the constructor of the structure you would have the same problem.
 
 
L. Spiro

Share this post


Link to post
Share on other sites

On the other hand, with C++11, it's easy to do. The syntax is this:

foo::foo():mData{1,2,3,4,5}{
}

In general {} syntax was designed to be consistent, and can be used in almost all cases, including in place of ().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement