[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



On Tue, Jan 13, 2004 at 12:48:12PM +0100, Hans Schou wrote:
> 
> Hej
> 
> Æv, så fik jeg en segmentation fault.
> 
> Jeg opretter et object (class). Under init finder jeg ud af at den går 
> ikke, og så vil jeg gerne have returneret NULL.
> 
> 	foo = new bar();

'init' - mener du i konstruktøren?

I så fald smider du bare en exception der, og fanger den der hvor det nu
passer.

> 
> I ovenstående skal foo så være NULL, hvis bar() fejler.

'NULL' findes ikke i C++ - du mener nok 0  ;)

> 
> Kan man det? Eller griber jeg det forkert an?
> 
> I virkeligheden er det en hægtet liste,
> så det er nemt at håndtere senere.
> 	link.additem(new bar());

Hvad med noget i stil med

---------------------------

struct bar {
 bar() {
  /// ...
  throw std::string("nixen bixen du"); // fejl!
 }
};


void build_my_list()
try {
 // ....
 for (.....) {
  link.additem(new bar());
 }
 // ...
} catch (std::string e) {
 std::cerr << "Den gik ikke " << e << std::endl;
}

---------------------------

Det er noget rod at skulle checke for null pointere senere hen, synes
jeg.   Normalt kan man lave sin kode så fejl i konstruktører enten ikke
forekommer, eller ihvertfald er så fatale hvis de forekommer at man
alligevel lukker programmet ned.  I eksemplet ovenfor vil man helt
opgive at konstruere listen hvis konstruktionen af et enkelt element
fejler.   Passer det i din situation?

Alternativt ville jeg skrive følgende:

------------------------------

struct bar {
 bar(std::list<bar> &l) {
  // .. initialisering
  if (!fejl)
   l.push_back(this);
 }
 // ...
};

void build_my_list()
{
 // ....
 for (.....) {
  new bar(my_list);
 }
 // ...
}

------------------------------

Her vil bar() ligge sig selv på listen hvis alt gik godt, og ellers
ikke.

Du vil dog lække de elementer som ikke kommer på listen. Det kan løses
med at smide elementerne på to lister (det er ikke god stil at lave
'delete this' efter testen i bar konstruktøren).

Hvad er det egentligt du skal?   ;)


 / jakob



 
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] *