Constructing and destructing on different functions.

This is a discussion on Constructing and destructing on different functions. within the C++ Programming forums, part of the General Programming Boards category; I have this in my main: Code: int main(){ MyClass* myObject = NULL; function1(myObject); function2(myObject); return 0; } void function1(void* ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    55

    Constructing and destructing on different functions.

    I have this in my main:

    Code:
    int main(){
         MyClass* myObject = NULL;
    
         function1(myObject);
         function2(myObject);
    
         return 0;
    }
    
    void function1(void* param){
         MyClass* myObject = (MyClass*) param;
    
         myObject = new MyClass();
    
         //Do some stuff that requires that MyClass*
    }
    
    void function2(void* param){
         MyClass* myObject = (MyClass*) param;
    
         //Do some stuff that require that MyClass*
    
         delete myObject;
    }

    It hangs when I do that "stuff" in the second function. Of course this isn't my actual code, but it is what I want to achieve. I need to have a pointer to a certain object, that has to be created on one function and destroyed on another.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You are passing the pointer by value. When the pointer gets modified (to point to new memory), only the local copy is updated. The original pointer in main is unchanged and still pointing to NULL.

    If you wanted this to work, you would use pass-by-reference, either with a reference to pointer or a pointer to pointer.

    I'm not sure this is the best design, though, so there could be a better solution that avoids this.

  3. #3
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    You need to pass the pointer to the first function by reference.

    Code:
    void function1(void*& param)
    The value of your pointer in main() isn't being changed by function1.

    I may have the *& backward (I can never remember the order for those), but that's your problem.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    88
    Code:
    int main()
    {
         MyClass* myObject;
    
         function1(myObject);
         function2(myObject);
    
         return 0;
    }
    
    //Any reason in particular why you were using void pointers?
    void function1(MyClass*& param)
    {
         param = new MyClass();
    
         //Do some stuff that requires that MyClass*
    }
    
    void function2(MyClass* param)
    {
         
         //Do some stuff that require that MyClass*
         param->whatever();
    
         delete param;
    }

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    55
    Yes, I was using void pointers because those were function pointers I couldn't touch, but I did it like this:

    Code:
    	MyClass** myObject = (MyClass**) param;
    
    	*myObject = new MyClass();
    And then used *myObject too for the delete, and it worked well. (I called the functions with &myObject)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21