2016-05-15 7 views
0

Ich habe eine Klasse geschrieben, wo die Objekte und Klassenattribute unveränderlich sind - aber jedes Mal, wenn ich versuche, ein Klassenattribut zu aktualisieren, erhalte ich den folgenden Fehler;C++ Klassenfehler - unveränderbar

"Nicht behandelte Ausnahme ... in ConsoleApplication.exe ... Stack-Überlauf"

Einige Details über das Klassenobjekt. Die Header-Datei der Klasse,

class AnObject { 

public: 

//constructor 
AnObject(); 

AnObject(
    const std::string   AttributeA, 
    const __int32    AttributeB 
    ) 

const AnObject AnObject::SetAttributeA(const std::string AttributeA) const; 
const AnObject AnObject::SetAttributeB(const __int32 AttributeB) const; 
const AnObject AnObject::SetMyAttributes (const std::string AttributeA, const __int32 AttributeB) const; 

private: 

const std::string   AttributeA; 
const __int32    AttributeB; 
}; 

Die Klassendatei,

AnObject::AnObject() : AttributeA("1002"), AttributeB(1) {}; 
AnObject::AnObject(const std::string AttributeA, const __int32 AttributeB) : AttributeA("1002"), AttributeB(1) 
{ 
SetMyAttributes("1002", 1); 
}; 

const AnObject AnObject::SetMyAttributes(const std::string AttributeA, const __int32AttributeB) 
const 
{ 
try { 
      return AnObject 
     (
       // base fields 
       AttributeA, AttributeB 
     ) 
} 
catch (exception e) 
     { 
      throw e; 
     } 
}; 

Das Objekt ist unveränderlich und damit alle Parameter gesetzt, wenn ein Parameter, der von der Setter-Klasse zu ändern. Der Code erzeugt jedoch einen Fehler, wenn ich die Methoden innerhalb von main aufrufen.

+0

Sie rufen den Konstruktor rekursiv auf: - P ... –

Antwort

1

Dieser Konstruktor von Ihnen:

AnObject::AnObject(const std::string AttributeA, const __int32 AttributeB) 

Anrufe

SetMyAttributes("1002", 1); 

, die wieder der Konstruktor ruft ...

const AnObject AnObject::SetMyAttributes(const std::string AttributeA, const __int32AttributeB) const 
{ 
try { 
      return AnObject(AttributeA, AttributeB); // recursive call 
} 
... 

SetMyAttributes scheint eine nutzlose Funktion, da alle Daten zu sein Mitglieder sind const und die Tatsache, dass Sie einezurückgebenObjekt nach Wert.

Normalerweise, Sie können nur ein Datenelement const in der Errichterinitialisierungsliste initialisieren. Sie können sie anschließend nicht ändern.

Same Nutzlosigkeit gilt für diese (außer wenn Sie etwas anderes haben, aus der Norm, die Ärmel hoch:

const AnObject AnObject::SetAttributeA(const std::string AttributeA) const; 
const AnObject AnObject::SetAttributeB(const __int32 AttributeB) const; 
const AnObject AnObject::SetMyAttributes (const std::string AttributeA, const __int32 AttributeB) const; 

Wenn Sie eine völlig unveränderliche Klasse sprechen, wouldn Sie Ich habe Ihnen vorgeschlagen, hier alles zu lesen: const correctness.

+1

Das und die Rückgabe einer Konstante nach Wert. Es ist nicht so, als ob jemand acc versuchen Sie identisch, ein temporäres zu ändern ... – IInspectable

+0

@Intspectable ... THanks, hinzugefügt – WhiZTiM