2013-06-04 13 views
5

Ich habe versucht, Boost optional für eine Funktion zu verwenden, die entweder ein Objekt oder eine Null zurückgeben kann und ich es nicht herausfinden kann. Hier ist was ich bisher habe. Vorschläge zur Lösung dieses Problems sind willkommen.fehlgeschlagener Versuch der Verwendung von boost :: optional

class Myclass 
{ 
public: 
    int a; 
}; 

boost::optional<Myclass> func(int a) //This could either return MyClass or a null 
{ 
    boost::optional<Myclass> value; 
    if(a==0) 
    { 
     //return an object 
      boost::optional<Myclass> value; 
     value->a = 200; 

    } 
    else 
    { 
     return NULL; 
    } 

    return value; 
} 

int main(int argc, char **argv) 
{ 
    boost::optional<Myclass> v = func(0); 
    //How do I check if its a NULL or an object 

    return 0; 
} 

Update:

Das ist mein neuer Code ist, und ich bin einen Compiler-Fehler bei value = {200};

class Myclass 
{ 
public: 
    int a; 
}; 

boost::optional<Myclass> func(int a) 
{ 
    boost::optional<Myclass> value; 
    if(a == 0) 
     value = {200}; 

    return value; 
} 

int main(int argc, char **argv) 
{ 
    boost::optional<Myclass> v = func(0); 


    if(v) 
     std::cout << v -> a << std::endl; 
    else 
     std::cout << "Uninitilized" << std::endl; 
    std::cin.get(); 

    return 0; 
} 

Antwort

8

Ihre Funktion sollte wie folgt aussehen bekommen:

boost::optional<Myclass> func(int a) 
{ 
    boost::optional<Myclass> value; 
    if(a == 0) 
     value = {200}; 

    return value; 
} 

Und Sie könnten es überprüfen, indem Sie auf 01 werfen:

boost::optional<Myclass> v = func(42); 
if(v) 
    std::cout << v -> a << std::endl; 
else 
    std::cout << "Uninitilized" << std::endl; 

Ist es nicht wert- sein würde> a = 200

Nein, es ist nicht. Von Boost.Optional.Docs:

T const* optional<T (not a ref)>::operator ->() const ; 

T* optional<T (not a ref)>::operator ->() ; 
  • Anforderungen: * dies initialisiert.
  • Rückgabe: Ein Zeiger auf den enthaltenen Wert.
  • Wirft: Nichts.
  • Hinweise: Die Anforderung wird über BOOST_ASSERT() bestätigt.

Und in der operator-> Definition:

pointer_const_type operator->() const 
{ 
    BOOST_ASSERT(this->is_initialized()); 
    return this->get_ptr_impl(); 
} 

Wenn das Objekt nicht initialisiert wird, Assertionsfehler wird. Wenn wir schreiben

value = {200}; 

Wir initialisieren Wert mit Myclass{200}.


Hinweis, dass value = {200} erfordert Unterstützung für initializer Listen (C++ 11-Funktion). Wenn Ihr Compiler nicht unterstützt, können Sie es wie folgt verwenden:

Myclass c; 
c.a = 200; 
value = c; 

Oder Konstruktor sorgen für Myclass mit int als Argument:

Myclass(int a_): a(a_) 
{ 

} 

Dann könnte man nur schreiben

value = 200; 
+0

Ich bin verwirrt mit 'value = {200}' Wird es nicht 'value-> a = 200' sein? – MistyD

+0

@MistyD, schauen Sie sich die Bearbeitung an. – soon

+0

Danke für die Bearbeitung.Aber mit 'Wert = {200};' bekomme ich den Kompilierfehler während der Erstellung 'Fehler C2143: Syntaxfehler: fehlt ';' vor '{' ' – MistyD