For example,

MyClass *stable;
if(..){
    MyClass temp;
    stable=&temp;
}
stable.myVariable;

Code snippet above does not work, because destructor of temp object is called.
How Can I Increase Scope of "temp" object?

Thanks.

Recommended Answers

All 10 Replies

>How Can I Increase Scope of "temp" object?
Define it at a higher scope? :icon_rolleyes: Your example is especially nonsensical because when the if body isn't executed, you'll have an uninitialized pointer to contend with. Usually in cases like this one we use dynamic memory:

MyClass *stable = 0;

if ( ... ) {
  stable = std::auto_ptr<MyClass> ( new MyClass() );
}

if ( stable == 0 ) {
  // Handle the potential error
}

stable->myVariable = myValue;

Depends how much you want to increase it by. Simply declare it in a lerger scope.

instead of

MyClass *stable;
if(..){
    MyClass temp;
    stable=&temp;
}
stable.myVariable;

try

MyClass *stable;
MyClass temp;
if(..){
        stable=&temp;
}
stable.myVariable;

if you really don't want temp hanging around any longer that necessary then enclise it another scope. eg.

MyClass *stable;
{
   MyClass temp;
   if(..){
    
       stable=&temp;
   }
   stable.myVariable;
} // temp destroyed here

>How Can I Increase Scope of "temp" object?
Define it at a higher scope? :icon_rolleyes: Your example is especially nonsensical because when the if body isn't executed, you'll have an uninitialized pointer to contend with. Usually in cases like this one we use dynamic memory:

MyClass *stable = 0;

if ( ... ) {
  stable = std::auto_ptr<MyClass> ( new MyClass() );
}

if ( stable == 0 ) {
  // Handle the potential error
}

stable->myVariable = myValue;

What's wrong with an uninitializaed pointer?

>What's wrong with an uninitializaed pointer?
I'd guess the guaranteed undefined behavior is what's wrong. Unless your compiler is nice enough to initialize pointers for you, there's no way to test for an uninitialized pointer because it looks just like an initialized pointer. But accessing an uninitialized pointer is undefined behavior and extremely likely to blow up.

>What's wrong with an uninitializaed pointer?
I'd guess the guaranteed undefined behavior is what's wrong. Unless your compiler is nice enough to initialize pointers for you, there's no way to test for an uninitialized pointer because it looks just like an initialized pointer. But accessing an uninitialized pointer is undefined behavior and extremely likely to blow up.

oops, oh yes :). Just glanxced at your rewrite and assumed the = 0 was ops code.

>How Can I Increase Scope of "temp" object?
Define it at a higher scope? :icon_rolleyes: Your example is especially nonsensical because when the if body isn't executed, you'll have an uninitialized pointer to contend with. Usually in cases like this one we use dynamic memory:

MyClass *stable = 0;

if ( ... ) {
  stable = std::auto_ptr<MyClass> ( new MyClass() );
}

if ( stable == 0 ) {
  // Handle the potential error
}

stable->myVariable = myValue;
stable = std::auto_ptr<MyClass> ( new MyClass() );

Interested in this syntax too. Not seen auto_ptr used in this way and my compiler rejects it...

Can you explain please.

>my compiler rejects it...
auto_ptr was an after thought, and I didn't bother to fix the bugs. This compiles (barring the obvious and intentional ... error), if you didn't already figure it out on your own:

std::auto_ptr<MyClass> stable;

if ( ... ) {
    stable = std::auto_ptr<MyClass> ( new MyClass() );
}

if ( stable.get() == 0 ) {
    // Handle the potential error
}

stable->myVariable = myValue;
stable = std::auto_ptr<MyClass> ( new MyClass() );

Yes me too :-) !

I hadn't tried the compile but looking at it and assuming it would work (due to its source) I was asking myself the question, surely the auto_ptr would delete the object as soon it went out of scope, which since it is anonymous is the end of the statement.

EDIT: Never mind you edit and a quick refresher of the auto_ptr reference clears it all up.

>my compiler rejects it...
auto_ptr was an after thought, and I didn't bother to fix the bugs. This compiles (barring the obvious and intentional ... error), if you didn't already figure it out on your own:

std::auto_ptr<MyClass> stable;

if ( ... ) {
    stable = std::auto_ptr<MyClass> ( new MyClass() );
}

if ( stable.get() == 0 ) {
    // Handle the potential error
}

stable->myVariable = myValue;

No that's fine. Was wondering if I was missing something.

Yes me too :-) !

I hadn't tried the compile but looking at it and assuming it would work (due to its source) I was asking myself the question, surely the auto_ptr would delete the object as soon it went out of scope, which since it is anonymous is the end of the statement.

exactly, further more the compiler can't convert an auto_ptr to a MyClass*.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.