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.
Sie rufen den Konstruktor rekursiv auf: - P ... –