Jump to content

Instantiating a class and using it

- - - - -

  • Please log in to reply
4 replies to this topic

#1
alirezan

alirezan

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Hello,

I have an issue trying to use a class I defined. Here's the issue. I have a class definition:

class test {

public test ();       //default constructor

public test (int var1, int var2);    //constructor #2

public ~test();

public func1 (void);

...

};

and in my main () I am doing:

main (){

int testvar = 3;


if ( testvar > 0 ) {

    test myvar ( 2, 1 );

}

myvar.func1();

}


But the trouble is, by the time the "if" is executed, the destructor of myvar has already destroyed the object myvar. How can I get around this issue? I want to define an object in an if statement and be able to still use it after the if is finished.

Any ideas?
Thanks

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
I believe you can simply define a pointer to a class beforehand, and use C++'s new keyword to instantiate it.
int main(...) {
    test* myvar = NULL;
    ...    
    if( testvar > 0 ) {
        myvar = new test(2, 1);
    }

    if(myvar != NULL) {
      //ensure myvar was allocated beyond this point
      myvar->func1(); //. = local member, -> = pointed member
      ...
      delete myvar; //call deconstructor
    }
    return 0;
}
I would read informed documentation on how C++ handles scopes like this.

Edited by Alexander, 19 May 2011 - 02:36 PM.
Corrected assuming code (in case of failure of allocation)

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
If that if fails, doesn't create new object, wouldn't this crash the program, deleting an unallocated object?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
@Flying Dutchman: As Alexander's code is currently shaped, yes, but you could simply assign myvar to NULL before the if statement, then delete is just a no-op. The real problem is attempting to dereference a potentially NULL or non-allocated pointer using myvar->func1().

My advice on this issue, especially if you want to keep static allocation, is to simply extend the size of the if block. This is because you shouldn't be attempting to use a particular method on an object if that object does not exist, and if you only construct/declare the object in the if statement, then there's no guarantee that if statement will be executed, which means there's no guarantee the object will exist outside the if statement when you attempt to run it, even if you do dynamically allocate it.
if (testvar > 0)

{

    test myvar(2, 1);

    myvar.func1();

}
However, that may not help you in your particular situation, assuming the code you gave us was simply an example.
Wow I changed my sig!

#5
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
I am amazed.

Shouldn't it be

class test

{

public:

    test(){}

    void func1(){}

};


Since when c/c++ started allowing this c# like prefix? Am i missing something here?

Also i would argue that above is an approach flaw. Using all of these rightful workarounds would only clutter the code and should be shaved off in any decent code review.

Tell me of a good programming situation in which you only create an object in a condition but still use it outside of it. If it is used outside, it should have been created outside if too. If not it should write fully be destroyed as soon as the code block goes out of scope.

Moreover, when would the destructor be called then if not immediately when it is going out of scope? Next level scope? function termination? program termination? None of these is the right place.

My two cents




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users