[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] [C++] new class, fail. Ønskes: NULL pointer



> foo = new bar();
> I ovenstående skal foo så være NULL, hvis bar() fejler.
> Kan man det? Eller griber jeg det forkert an?
Ja, det kan man i hvisse tilfælde.

Hilsen, Hans-Christian Stadler
------------------------------------
#include <iostream>

using namespace std;

class bar {
public:
    static bool fail;

    void *operator new (size_t sz)
        {
            return fail ? 0 : (::operator new(sz));
        }

    void operator delete (void *inst, size_t sz)
        {
            ::operator delete(inst);
        }
};

bool bar::fail;

int main (int argc, char *argv[])
{
    bar *foo = new bar();               // fail er false her -> ok
    cout<<"foo = "<<(void *)foo<<endl;
    delete foo;

    bar::fail = true;
    foo = new bar();                    // fail er true her -> NULL
    cout<<"foo = "<<(void *)foo<<endl;
}



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *