Thread: erased pointer

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    Exclamation erased pointer

    I have a problem in some code I've been writing lately.
    Suppose I have the following code:
    Code:
    class A{
    
        public:
        
        Vector3D* point;
    
    };
    
    class B{
    
        public:
    
            A* obj;
    
            float computeSomething( Vector3D* point );
    
    };
    
    float B::computeSomething( Vector3D* point ){
        Vector3D* v = someObject->getVector();
        float result = *point * *v;
        return result;
    }
    Then at some point inside a function of class B I do:
    Code:
    float m = computeSomething( obj->point );
    My problem is, pointer obj is correctly initialized before the call to function computeSomething, meaning that it points to an instance of class A, but after the function call the pointer is NULL!
    It seems that somhow the instance of class A gets erased.
    Does someone know why?

    Ed.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    can you show us the overloaded multiplication operator for Vector3D?

    generally you should pass the vector3d per const reference or const pointer at least. then the compiler will lead you to the problem.

    Code:
    float B::computeSomething( const Vector3D & point )
    {...}
    or


    Code:
    float B::computeSomething( const Vector3D * const point )
    {...}

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Thank you, I resolved the problem. It was in fact related to passing a non const pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM