Re: Constructor denial?
- From: "Bruno van Dooren" <bruno_nos_pam_van_dooren@xxxxxxxxxxx>
- Date: Wed, 4 Jan 2006 21:46:30 +0100
Or you can supply your own 'new' operator for that class. in that case you
can use a flag or an exception in your constructor to signal that
construction has failed, and let the 'new' operator return a NULL pointer.
kind regards,
Bruno.
"Tamas Demjen" <tdemjen@xxxxxxxxx> wrote in message
news:%23kJEY6WEGHA.916@xxxxxxxxxxxxxxxxxxxxxxx
> Peter Oliphant wrote:
>
>> I'm guessing it's not possible to actually just return 'nullptr' in a
>> construction attempt like 'malloc' does...
>
> You can add a static member to the class, and call that instead of the
> constructor. It doesn't require a separate factory class. You can
> optionally make the constructor private, if you want to force your own
> function instead.
>
> class C
> {
> public:
> C();
> static C* TryCreate()
> {
> try
> {
> C* c = new C;
> return c;
> }
> catch(...)
> {
> return 0;
> }
> }
> };
>
> You can apply the same idea to C++/CLI. It's not that cumbersome, but I
> don't see why you prefer nullptr to an exception. If construction fails,
> that's almost always an error, and therefore the exception is valid.
>
> Optionally, you can set a failed flag in the constructor, and add a bool
> IsValid() const member function. It's very good for classes that can fail
> for a valid reason, which as a FileStream.
>
> Tom
.
- Follow-Ups:
- Re: Constructor denial?
- From: Bronek Kozicki
- Re: Constructor denial?
- References:
- Constructor denial?
- From: Peter Oliphant
- Re: Constructor denial?
- From: Bronek Kozicki
- Re: Constructor denial?
- From: Peter Oliphant
- Re: Constructor denial?
- From: Tamas Demjen
- Constructor denial?
- Prev by Date: Integrating driver development with the Visual Studio 8.0 build environment
- Next by Date: Re: C# C++ integration
- Previous by thread: Re: Constructor denial?
- Next by thread: Re: Constructor denial?
- Index(es):
Relevant Pages
|