   Overloading a function
  Hi, Im writing a program for my final project and need some help.Is it possible to overload a function by the return type or something similar, i find it dumb to write 4 functions for what I have to do to accomplish this.A* CreateNew(int &size){    size++;    A *tempA = new A[size];    return tempA;}I have, classes A B C and D which are all the same just different names. Would I have to really create 4 functions like the above and just change letters A to B, C etc.. And also name them differently? Seems kind of dumb to me..   
  The return type is not part of the signature for overload resolution. However, you could use templates for what you're doing:template< typename T >T* Create( int& size ) {  ++size;  return new T[size];}Can you explain what you're doing more concretely? As it stands it looks like you're engineering an extremely backwards and nonidiomatic hack that would be better served by application of a std::vector.   ;  If you are highly careful, you could simulate return type overloading:class Foo {    Frob const lhs, rhs;    explicit Foo (Frob lhs, Frob rhs) : lhs(lhs), rhs(rhs) {}    Foo (); Foo (Foo const&); Foo operator = (Foo const &); // forbid    friend Foo fun(Frob lhs, Frob rhs);public:    operator Bar () const {   ...    }    operator Foop () const {   ...    }};Foo fun(Frob lhs, Frob rhs) {    return Foo(lhs,rhs);}Or, in more detail (whole thread at http://ompf.org/forum/viewtopic.php?p=15548):There is also the possibility of having operator* for both dot-product and cross-product.Assume a basic vector-type (just for demonstration):struct Vector {   float x,y,z;   Vector() {}   Vector (float x, float y, float z) : x(x), y(y), z(z) {}};We observe that the dot-product is a scalar, the cross-product is a vector. In C++, we may overload conversion operators:struct VecMulRet {public:   operator Vector ()  const { return Vector (    lhs.y*rhs.z - lhs.z*rhs.y,    lhs.z*rhs.x - lhs.x*rhs.z,    lhs.x*rhs.y - lhs.y*rhs.x );   }   operator float () const { return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;   }private:   Vector const &lhs, &rhs;   VecMulRet ();   VecMulRet (VecMulRet const &);   VecMulRet & operator = (VecMulRet const &);   VecMulRet (Vector const &lhs, Vector const &rhs)   : lhs(lhs), rhs(rhs)   {}   friend VecMulRet operator * (Vector const &lhs, Vector const &rhs);};Only operator* is allowed to use struct VecMulRet, copying of VecMulRet is forbidden (paranoia first).Operator* is now defined as follows:VecMulRet operator * (Vector const &lhs, Vector const &rhs) {   return VecMulRet (lhs, rhs);}Et voila, we can write:int main () {   Vector a,b;   float dot = a*b;   Vector cross = a*b;}Btw, this is blessed by the Holy Standard as established in 1999.If you are curious about performance: It comes at no penalty if you are using a real C++ compiler: Performance comparison.   ;Quote:Original post by RogerThat123Hi, Im writing a program for my final project and need some help.Is it possible to overload a function by the return type or something similar, i find it dumb to write 4 functions for what I have to do to accomplish this.A* CreateNew(int &size){    size++;    A *tempA = new A[size];    return tempA;}I have, classes A B C and D which are all the same just different names. Would I have to really create 4 functions like the above and just change letters A to B, C etc.. And also name them differently? Seems kind of dumb to me..You could do it by "faking" it.void CreateNew(int &size, A *return_value){    size++;    A *tempA = new A[size];    return_value = tempA;}This works because the parameters would change with the B, C, D you add.theTroll   ;Quote:Original post by TheTrollQuote:Original post by RogerThat123Hi, Im writing a program for my final project and need some help.Is it possible to overload a function by the return type or something similar, i find it dumb to write 4 functions for what I have to do to accomplish this.A* CreateNew(int &size){    size++;    A *tempA = new A[size];    return tempA;}I have, classes A B C and D which are all the same just different names. Would I have to really create 4 functions like the above and just change letters A to B, C etc.. And also name them differently? Seems kind of dumb to me..You could do it by "faking" it.void CreateNew(int &size, A *return_value){    size++;    A *tempA = new A[size];    return_value = tempA;}This works because the parameters would change with the B, C, D you add.theTrollThere is a bug in your code. Either you must pass a reference to pointer, or a pointer to pointer, but a pointer only will not change the variable at the caller's site.And your way has some disadvantages: 0) introduces pointers 1) forbids for initialization 2) because of 1), const correct code not possibleI would still prefer (sorted by preferability) 0) a templated CreateNew<T> (...) 1) a non-overload 2) my other solution;  so I could create all of theseA* CreateNewA(int &size){    size++;    A *tempA = new A[size];    return tempA;}B* CreateNewB(int &size){    size++;    B *tempB = new B[size];    return tempB;}C* CreateNewC(int &size){    size++;    C *tempC = new C[size];    return tempC;}D* CreateNewD(int &size){    size++;    D *tempD = new D[size];    return tempD;}Using a template or whatever you say??;  Yes. Exactly as I demonstrated. But also like I said, it doesn't look like you need to do this at all. There are better ways to achieve what you appear to be after.   ;  yeah but there are specific requirements for the assignment so i cant use vector.template< typename T >T* Create( int& elements ) {  ++elements;  T* temp = new T[elements];  if (elements > 0)std::copy(orig, orig+ elements, tempA); //orig is pointer to original array  temp[elements].x = 0;  return temp;}How can i pass the ref the the orig array in for each of the different classes, another template or?Is it possible to access the members of the class like above temp[elements].x if all of the classes have the member x in it?   ;  You can't use vector but you can use copy? Seems silly. You'd better make sure you can use templates, then, because if you have one arbitrary restriction you're likely to have others.Quote:How can i pass the ref the the orig array in for each of the different classes, another template or?'orig' will have to come from a parameter, no?Is it possible to access the members of the class like above temp[elements].x if all of the classes have the member x in it?Yes.Since this is homework, you're going to have to figure out the details on your own. If you don't know anything about templates it's probably a bad idea to try to use them here, for a variety of reasons.   ;  ok this is how im using now, with passing the parameter through a template;template< typename T >T* forest::Create( int& elements, T* original ) {  T* temp = new T[elements+1];   if (elements > 0)std::copy(original, original + elements, temp);   ++elements;  return temp;}//main function creatingswitch (type){case 1:pA = Create(total[A], pA); break;case 2:pB = Create(total, pB);break;case 3:pC = Create(total[C], pC);break;case 4:pD = Create(total[D], pD);break;}There is a runtime error sayign invalid allocation size: 4294967295 bytes   