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

C / C++ / MFC

 
AnswerRe: Error in loading accelerator table Pin
Stephen Hewitt20-Jun-10 19:11
Stephen Hewitt20-Jun-10 19:11 
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 
Unfortunately, the language don't specify any construct to pass parameters to the array constructing elements.

std::vector does this, essentially with a trick (to allow array grow and shrink it doesn't allocate "array" but "bulk buffers".

You can emulate this trick yourself this way:
///Elements requiring explicit initialization
/// \note Elements don't have a default Ctor.
class Element
{
public:
    /// init an element
    Element(ParamType _a)
    { ... }
private:
    //your implementation
};

static const size_t BUFSZ = ...;

/// a class aggragating \c BUFSZ elements
class Aggregate
{
public:
    /// initialize with _a
    Aggregate(ParamType _a) :vector()
    { 
        vector = reinterpret_cast<Element*>(buffer);
        for(size_t i=0; i<BUFSZ; ++i)
            new(vector+i)Element(_a); //< USE PLACEMENT NEW
    }
    /// do proper element copy (not just bulk memory copy)
    Aggregate(const Aggregate& _s)
    {
        vector = reinterpret_cast<Element*>(buffer);
        for(size_t i=0; i<BUFSZ; ++i)
            new(vector+i)Element(_s.vector[i]); //< USE PLACEMENT NEW to construct copies
    }
    /// do proper element destruction
    ~Aggregate()
    {
        for(size_t i=0; i<BUFSZ; ++i)
            vector[i].~Element(); //< DO EXPLICIT DESTRUCTION
    }
private:
    char buffer[BUFSZ*sizeof(Element)];
    Element* vector;    
};


The advantage respect to std::vector is that memory is not allocated dynamically, but - Being BUFZ statically note, it can stay into the aggregate space.

The "other way" is to let Elements to default-construct someway, and than assign them the required values.

It mostly depend on the fact you can -or not- accept a default initialization for Element-s putting them in a sort of invalid state until they are assigned.

2 bugs found.
> recompile ...
65534 bugs found.
D'Oh! | :doh:


AnswerRe: C++ syntax: how to init non-default constructor for array? Pin
Aescleal20-Jun-10 22:58
Aescleal20-Jun-10 22:58 
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?? Pin
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 

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.