It's probably that your class has no default constructor, and when you call call_function(), the compiler is not doing a good job of guessing at how to construct your class. Try passing a reference like this:
void call_function MyClass& C )
{
c.do_something();
}
right ... I already figured that possiblity out ... :-)
2 further questions:
1) why the & (I know it helps and roughly what it does but why can the Segmentation fault be avoided)?
2) where in my code is the default constructor relevant (I don't exactly know what you mean) ... get_my_class(some_val) - is working out all right as:
MyClass c = get_my_class(some_val);
c.do_something...
You'd only get a segmentation fault if your copy ctor or assignment operator are no good.
If you don't provide your own, the compiler will fill in its own implementations, which work fine for most classes, but have issues when you have dynamically allocated memory in the class.
void foo()
{
A a; // this allocates an int for 'a.p'
{
A b(a); // this copies 'a' to another A object, 'b'
// note that b did not allocate it's own 'p'
// b.p was just assigned to a.p. as if you did this:
// b.p = a.p;
//
// because of this, both objects point to the same memory
} // here, b's destructor deletes 'p'
// which is bad because a was still using it!
a.Set(5); // HEAP CORRUPTION!!!
// a.p has already been deleted by b's destructor, so calling set here is bad
} // EXPLODE
// a's dtor is being called, which deletes p AGAIN (after b's dtor deleted it)
// this may cause a segfault (if you're lucky)
When you pass objects to functions by value (without the & operator), you are creating copies, which causes the above problem.
The solution here is to write your own copy constructor and assignment operator which correctly copies the object: