I have a class named Complex with many member methods returning objects of that Complex class, and many member methods taking objects of class Complex as parameter.
class Complex
{
public:
Complex sampleFunc(Complex c) { ... }
Complex operator+(Complex c) { ... }
};
This would be a library, so other users would be using my class. Since, most users use:
Complex *ptrObj = new Complex(); // declaration 1
rather than using:
Complex obj; // declaration 2
The code I have written above would work with declaration 2 but not with declaration 1. I can change the prototype for sampleFunc() as follows:
Complex* sampleFunc(Complex *c) { ... }
But then I cannot meaningfully change the operator+() function to get the effect of adding two objects (rather than erroneously adding two pointers). So, what API should I be exposing - that works with declaration 1 or that works with declaration 2 or should I provide two versions for each function? I believe that passing pointers to functions would work faster, but then operator overloading would be a problem.
sizeof(Complex)?sampleFunc()does not modifycthen useconst Complex &c(orComplex const &c) instead ofComplex &c. The return value should not be aComplex&reference if a newComplexis created, returning by value is the correct way.