Welcome to Emulationworld

Forum Index | FAQ | New User | Login | Search

Make a New PostPrevious ThreadView All ThreadsNext ThreadShow in Flat Mode*


SubjectRe: How do you manually call a constructor Reply to this message
Posted byfinaldave
Posted on03/24/04 07:30 PM



> >
> >
> > How do you manually call constructors in C++?
> >
> > e.g. if you have allocated an array of class instances but weren't allowed to
> > use new(), and the classes have stuff in the constructor which is important,
> but
> > you can't move it into an .Init() function because they are other classes
> within
> > the private: data of the class.
> > (It's great working with other peoples code isn't it!)
> >
> >
> > I tried doing
> > for (i=0;i.MyClass();
> >
> > But no luck:
> > "error C2274: 'function-style cast' : illegal as right side of '.' operator"
>
> You want.. placement new!
>
> For example,
>
> void* mem=malloc(sizeof(FooClass));
>
> FooClass* p = new(mem) FooClass(999);
>
> To delete the object, as you found you can call destructors, then just free the
> memory.


Brilliant! Thanks, that's just what I was after. You learn something new everyday

In fact I learnt two new things: strrchr is an ANSI function to find the last occurance of a character. Hurrah! The amount of times I needed that and ending up writing my own tacky routine :p
Now if only Microsoft had used it in Outlook...

> >
> >
> > Weirdly:
> > for (i=0;i.~MyClass();
> > works!
> >
> >
> > You learn something old everyday...
> >
>


You learn something old everyday...


-
Entire Thread
Subject  Posted byPosted On
*How do you manually call a constructor  finaldave03/23/04 11:00 AM
.*Re: How do you manually call a constructor  MrJeff03/23/04 12:20 PM
...Re: How do you manually call a constructor  finaldave03/24/04 07:30 PM