Index

C & C++ Programming - Function Return Types

#include "stdafx.h"

class CReturningFromFunction{
public:
 CReturningFromFunction();
 ~CReturningFromFunction();

 int ReturnByVal();
 int& ReturnByRef();
 int* ReturnByPtr();
 int* ReturnByPtrMemAlloc();
};

/////////////////////////////////////////////////////////////////////////
// Implementation File for class CReturningFromFunction
CReturningFromFunction::CReturningFromFunction()
{
}

CReturningFromFunction::~CReturningFromFunction()
{
}

int CReturningFromFunction::ReturnByVal()
{
 int X;

 X = 1000;

 return(X);
}

int& CReturningFromFunction::ReturnByRef()
{
 int Y;

 Y = 2000;

 return(Y);
}

int* CReturningFromFunction::ReturnByPtr()
{
 int Z;

 Z = 3000;

 return(&Z); // Not Advisible
}

int* CReturningFromFunction::ReturnByPtrMemAlloc()
{
 int *Z;

  Z = new int;

 *Z = 4000;
 delete Z; // Adsurd Value

 return(Z);
}

void TESTFUNCTIONRETURN()
{
 CReturningFromFunction objReturnFromFunction;

 cout << "Return by Value = " << objReturnFromFunction.ReturnByVal() << endl;

 cout << "Return by Reference = " << objReturnFromFunction.ReturnByRef() << endl;

 cout << "Return by Pointer = " << *objReturnFromFunction.ReturnByPtr() << endl;

 cout << "Return by Pointer = " << *objReturnFromFunction.ReturnByPtrMemAlloc() << endl;
}

Index