Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

class instance not visible...

Posted on 2004-08-19
4
Medium Priority
?
213 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

636 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