Click here to Skip to main content
15,893,622 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Nested structs problem Pin
manchukuo14-Feb-11 12:48
manchukuo14-Feb-11 12:48 
GeneralRe: Nested structs problem Pin
Richard MacCutchan13-Feb-11 13:28
mveRichard MacCutchan13-Feb-11 13:28 
GeneralRe: Nested structs problem Pin
Niklas L13-Feb-11 19:44
Niklas L13-Feb-11 19:44 
Questionhow to init member class array if the class has no default constrctor? Pin
includeh1013-Feb-11 5:16
includeh1013-Feb-11 5:16 
AnswerRe: how to init member class array if the class has no default constrctor? Pin
Iain Clarke, Warrior Programmer13-Feb-11 6:34
Iain Clarke, Warrior Programmer13-Feb-11 6:34 
AnswerRe: how to init member class array if the class has no default constrctor? Pin
jschell13-Feb-11 8:00
jschell13-Feb-11 8:00 
AnswerRe: how to init member class array if the class has no default constrctor? Pin
CPallini13-Feb-11 21:40
mveCPallini13-Feb-11 21:40 
AnswerRe: how to init member class array if the class has no default constrctor? Pin
Stefan_Lang13-Feb-11 23:19
Stefan_Lang13-Feb-11 23:19 
The only way to initialize standard arrays is aggregate initialization:
My my[] = {3,5,11}; // correct: this will invoke My::My(int) for each member
My my3[3] = {3,5,11}; // correct: predetermined array size sorresponds to size of initializer list
My my4[4] = {3,5,11}; // error: elements of partially initialized array must have default constructor

The easiest way to solve your problem thus would be to add a default constructor to class My.

If that isn't an option, you can work around this issue by separating the allocation of the required memory and its initialization like this:
class My {
   int val;
public:
   My(int i) : val(i) {}
   int get() const {return val;}
};
class Your {
   char myPlaceHolder[3][sizeof(My)];
   My* my;
public:
   Your()
   { 
      my = (My*)(myPlaceHolder); // just to assign the address
      new (&my[0]) My(3);         // calls My::My(int) in-place using the memory allocated at myPlaceHolder[0]
      new (&my[1]) My(5);
      new (&my[2]) My(11);
   }
   int operator[](int i) const { return my[i].get(); }
};
void testYour() {
   Your your;
   std::cout << "my[0]="<<your[0] << " ; my[1]="<<your[1] << " ; my[2]=" <<your[2] << std::endl;
}

The function testYour() will print "my[0]=3 ; my[1]=5 ; my[2]=11"
QuestionMFC: Use firefox in MFC Pin
msn9212-Feb-11 22:39
msn9212-Feb-11 22:39 
AnswerRe: MFC: Use firefox in MFC Pin
wangningyu12-Feb-11 22:47
wangningyu12-Feb-11 22:47 
AnswerRe: MFC: Use firefox in MFC Pin
Andrew Brock12-Feb-11 23:41
Andrew Brock12-Feb-11 23:41 
AnswerRe: MFC: Use firefox in MFC Pin
Richard MacCutchan12-Feb-11 23:42
mveRichard MacCutchan12-Feb-11 23:42 
AnswerRe: MFC: Use firefox in MFC Pin
Maximilien13-Feb-11 1:02
Maximilien13-Feb-11 1:02 
QuestionHow to free the variable Pin
goldenrose912-Feb-11 4:47
goldenrose912-Feb-11 4:47 
AnswerRe: How to free the variable Pin
Yusuf12-Feb-11 5:04
Yusuf12-Feb-11 5:04 
GeneralRe: How to free the variable Pin
goldenrose912-Feb-11 5:10
goldenrose912-Feb-11 5:10 
GeneralRe: How to free the variable Pin
Aescleal12-Feb-11 5:19
Aescleal12-Feb-11 5:19 
GeneralRe: How to free the variable Pin
goldenrose912-Feb-11 6:23
goldenrose912-Feb-11 6:23 
GeneralRe: How to free the variable Pin
Aescleal12-Feb-11 10:07
Aescleal12-Feb-11 10:07 
AnswerRe: How to free the variable PinPopular
Aescleal12-Feb-11 5:17
Aescleal12-Feb-11 5:17 
GeneralRe: How to free the variable [modified] Pin
Niklas L12-Feb-11 6:30
Niklas L12-Feb-11 6:30 
GeneralRe: How to free the variable [modified] Pin
goldenrose912-Feb-11 7:30
goldenrose912-Feb-11 7:30 
GeneralRe: How to free the variable Pin
Niklas L12-Feb-11 8:18
Niklas L12-Feb-11 8:18 
GeneralRe: How to free the variable Pin
Aescleal12-Feb-11 10:02
Aescleal12-Feb-11 10:02 
GeneralRe: How to free the variable Pin
Niklas L12-Feb-11 12:56
Niklas L12-Feb-11 12:56 

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.