Solved

class instance not visible...

Posted on 2004-08-19
4
187 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now