?
Solved

class instance not visible...

Posted on 2004-08-19
4
Medium Priority
?
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 390 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 360 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

Technology Partners: 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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

762 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