mercredi 24 février 2016

C++11: Instantiate object with arguments inside try [duplicate]

This question already has an answer here:

I'm learning about exceptions. I have discover the RAII pattern. In my case I did a chat than creates a socket object and allow to communicate through UDP.

But I'm trying to handle exceptions when instatiatin the object. So as this example:

class Foo {
   public:
      Foo(int a);
      int work();
};

int Foo::work(void){
    return 1;
}

int main(void){
    try {
        Foo foo1(1);
    } catch (std::system_error& e) {
        return 1;
    }
    foo1.work();
    return 0;
}

When I try to access foo1.work() I get a non-class type. That's probably related to the scope. I tried to use the uniform initialization with {} but it didn't work also.

The only possible solution is to use pointers? I though pointers should be avoided.

Thank you for your time.

Aucun commentaire:

Enregistrer un commentaire