I''ve recently begun to really take the plunge and start using more OOP in my code.
I have a function that takes, as an argument, a pointer to an object - in this case, of type Sphere. In this particular case, though, I have some information that is adequate to describe a sphere, but not an actual instance of the class Sphere. So, for convenience''s sake, I''ve added a constructor to my Sphere class that just takes the basic information...
Sphere(triple nCenter, float nRadius){position = nCenter; SetRadius(nRadius);}
Now, using this constructor, I can invoke my function that takes a Sphere * thusly:
void functionOne(triple a, float b)
{
functionTwo( &Sphere(a, b));
}
void functionTwo(Sphere * sp)
{
... (other code here that uses sp) ...
}
This seems to work just fine, but I feel a little weird passing a pointer to something that doesn''t actually exist, per se, so I just wanted to ask here:
Does what I''m doing create a memory leak? Are there any problems with doing it this way? It would seem to make sense that the object I''m creating in functionOne probably terminates when the function ends, and so I should be wary not to do anything with it in functionTwo that would be contingent on it existing once the function was done. Is this assumption accurate?
Thanks!