Solved

class instance not visible...

Posted on 2004-08-19
4
198 Views
Last Modified: 2012-05-05
Hi all,

I've got:

void myFunction(){
   try{
      CSpec CS("param1");
   }catch(exception e){
      //Do recovery things...
   }

   if(something){
      CS.method();
   }
}

...problem is, is that CS.method(); isn't recognised since CS is in a different scope (???)....I can't do this:

void myFunction(){
   try{
      CSpec CS("param1");
      if(something){
         CS.method();
      }
   }catch(exception e){
      //Do recovery things...
   }
}

because I've got plenty of those if(something)'s...so I don't really want hundreds of nested if's like that...
0
Comment
Question by:Unimatrix_001
4 Comments
 
LVL 3

Expert Comment

by:Indrawati
ID: 11848462
I think it's better if you code it this way:

try
{
     MyFunction();
}
catch(exception &e)
{
}

You don't need to explicitly specify try/catch inside MyFunction() anymore.
0
 
LVL 30

Accepted Solution

by:
Axter earned 130 total points
ID: 11848554
You can use a pointer instead.
void myFunction(){
 CSpec *CS_ptr = NULL;
  try{
     CS_ptr  = new CSpec("param1");
  }catch(exception e){
     //Do recovery things...
  }
   CSpec &CS = *CS_ptr;
  if(something){
     CS.method();
  }
}

By using a pointer, you can delay creating the object until it's in the try{} scope.
By using a reference after the try/catch, you can reference the pointer, and then use the reference variable, which has the same syntax as a static variable.
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 120 total points
ID: 11850570
If the exception is thrown in the ctor, Axter's approach wouldn't work, because the instance of CSpec will not have been constructed and the CS_ptr will continue to point to NULL.

Careful with this:

   CSpec &CS = *CS_ptr;

You need to test for a NULL pointer before dereferencing CS_ptr.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11853361
Thanks :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question