Solved

class instance not visible...

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the real difference between C++ and Visual C++.NET? 7 108
Best book to learn C++ 4 70
Problem with SqlConnection 4 160
designing in object programming 12 52
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

861 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

24 Experts available now in Live!

Get 1:1 Help Now