Click here to Skip to main content
15,880,427 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Error in loading accelerator table Pin
Pryabu20-Jun-10 19:32
Pryabu20-Jun-10 19:32 
QuestionVisual Studio 2010 Release Candidate to Trial? Pin
Paul Hooper20-Jun-10 16:56
Paul Hooper20-Jun-10 16:56 
QuestionC++ syntax: how to init non-default constructor for array? Pin
includeh1020-Jun-10 13:33
includeh1020-Jun-10 13:33 
AnswerRe: C++ syntax: how to init non-default constructor for array? Pin
«_Superman_»20-Jun-10 15:30
professional«_Superman_»20-Jun-10 15:30 
AnswerRe: C++ syntax: how to init non-default constructor for array? Pin
Stephen Hewitt20-Jun-10 17:11
Stephen Hewitt20-Jun-10 17:11 
GeneralRe: C++ syntax: how to init non-default constructor for array? Pin
«_Superman_»20-Jun-10 18:36
professional«_Superman_»20-Jun-10 18:36 
AnswerRe: C++ syntax: how to init non-default constructor for array? Pin
Emilio Garavaglia20-Jun-10 21:00
Emilio Garavaglia20-Jun-10 21:00 
AnswerRe: C++ syntax: how to init non-default constructor for array? Pin
Aescleal20-Jun-10 22:58
Aescleal20-Jun-10 22:58 
If you don't have to use an array (and generally there's no point), can use a vector and want each element initialised to the same value:

class YourClass
{
    public:
        YourClass() : mys( 2, MyClass( 7 ) )
        {}

        std::vector<MyClass> mys;
};


will do the trick. If you want different values then it's a bit trickier unless they're known at compile time. I won't bother going over that here though, please post if you want to know how to do it.

If you want them to all have different values and have an array set at compile time you're going to have to manage the construction and destruction yourself into a raw memory buffer:

class B
{
	public:
		B( int ) {}
};

class A
{
	public:
		A() : values_( reinterpret_cast<B (&)[2]>( raw_buff_[ 0 ] ) )
		{
		}

	private:
		char raw_buff_[ sizeof( B[2] ) ];
		B (&values_)[2];
};


How you write the body of the constructor for A then depends on whether you want one value or different values. If you want the same value you can use initialized_fill:

A() : values_( reinterpret_cast<B (&)[2]>( raw_buff_[ 0 ] ) )
{
	std::uninitialized_fill( &values_[ 0 ], &values_[ 2 ], B( 7 ) );
}


If you want to have different values you'll have to use something like construct (if your standard library supports it, it was in the SGI STL but not in the standard) or write your own:

template <class T1, class T2> void construct(T1* p, const T2& value)
{
	new( p ) T1( value );
}


and then use it to implement your constructor:

A() : values_( reinterpret_cast<B (&)[2]>( raw_buff_[ 0 ] ) )
{
	construct( &values_[ 0 ], B( 1 ) );
	construct( &values_[ 1 ], B( 2 ) );
}


In either case you're going to want a destructor as you have to manually run the destructor for the elements in the array:

template <class T> void destroy(T* p)
{
    p->~T();
}

~A()
{
    destory( &values_[ 0 ] );
    destroy( &values_[ 1 ] );
}
Generally don't use C-style arrays unless you really have to. There's usually a solution in the standard library that's (almost) as good and far less fiddly to use.

Cheers,

Ash

Edited: Forgot the destructor.
QuestionCString::GetAt() problem Pin
ForNow20-Jun-10 8:33
ForNow20-Jun-10 8:33 
AnswerRe: CString::GetAt() problem Pin
«_Superman_»20-Jun-10 15:35
professional«_Superman_»20-Jun-10 15:35 
GeneralRe: CString::GetAt() problem Pin
ForNow21-Jun-10 1:59
ForNow21-Jun-10 1:59 
Questionreceiving data from serial port into two dialogs in an application Pin
l_d20-Jun-10 6:48
l_d20-Jun-10 6:48 
AnswerRe: receiving data from serial port into two dialogs in an application Pin
«_Superman_»20-Jun-10 15:38
professional«_Superman_»20-Jun-10 15:38 
AnswerRe: receiving data from serial port into two dialogs in an application Pin
Cedric Moonen20-Jun-10 20:35
Cedric Moonen20-Jun-10 20:35 
QuestionCan someone give a detail of this sample coder why the result is 9,and not 4?? Pin
wbgxx20-Jun-10 4:35
wbgxx20-Jun-10 4:35 
AnswerRe: Can someone give a detail of this sample coder why the result is 9,and not 4?? Pin
CPallini20-Jun-10 4:51
mveCPallini20-Jun-10 4:51 
AnswerRe: Can someone give a detail of this sample coder why the result is 9,and not 4?? Pin
hasani200720-Jun-10 4:51
hasani200720-Jun-10 4:51 
AnswerRe: Can someone give a detail of this sample coder why the result is 9,and not 4?? PinPopular
Richard MacCutchan20-Jun-10 5:49
mveRichard MacCutchan20-Jun-10 5:49 
JokeRe: Can someone give a detail of this sample coder why the result is 9,and not 4?? Pin
Rajesh R Subramanian20-Jun-10 6:42
professionalRajesh R Subramanian20-Jun-10 6:42 
AnswerRe: Can someone give a detail of this sample coder why the result is 9,and not 4?? Pin
Hristo-Bojilov20-Jun-10 6:23
Hristo-Bojilov20-Jun-10 6:23 
GeneralRe: Can someone give a detail of this sample coder why the result is 9,and not 4?? Pin
Luc Pattyn20-Jun-10 11:04
sitebuilderLuc Pattyn20-Jun-10 11:04 
AnswerRe: Can someone give a detail of this sample coder why the result is 9,and not 4?? - sorta repost Pin
Iain Clarke, Warrior Programmer21-Jun-10 0:26
Iain Clarke, Warrior Programmer21-Jun-10 0:26 
QuestionHow to find the correct coordinate? Pin
hasani200720-Jun-10 4:28
hasani200720-Jun-10 4:28 
AnswerRe: How to find the correct coordinate? Pin
Niklas L21-Jun-10 0:44
Niklas L21-Jun-10 0:44 
Questiona problem in maze (the worm pass the walls) Pin
hasani200720-Jun-10 0:25
hasani200720-Jun-10 0:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.