Suppose we write a function which returns the name of

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: the value 'the address of the d' in the string. has Object Programming means ans points at the 'd' and the output means ans points 'd' and would be would def Thus the return value is a location within the Thus memory we informed the function about. memory (It could also be 0.) Object Programming But suppose we want to return something new? Suppose we write a function which returns the name of a month given the month number. name But suppose we want to return something new? Suppose we write a function which returns the name of a month given the month number. name char* MonthName(int m) char* MonthName(int { static char name[12][4] ={"Jan","Feb","Mar","Apr","May", "Jun","Jul","Aug","Sep", "Oct","Nov","Dec"}; return name[m]; return name[m } char* MonthName(int m) char* MonthName(int { static char name[12][4] ={"Jan","Feb","Mar","Apr","May", "Jun","Jul","Aug","Sep", "Oct","Nov","Dec"}; return name[m]; return name[m } Object Programming The static keyword is needed so that the static keyword pointer value we return points at memory that exists. exists. W ithout it, the array becomes automatic and the Without memory it uses will be given up when we return from the function. from That memory would be available for other uses. W e would then be returning a pointer to memory We not currently in use. not Object Programming Object Programming W e must ensure that the pointer value is for valid memory. memory. (Here's an alternative... void MonthName(int m, char** n) void MonthName(int { static char name.... *n = name[m]; *n name[m } still with the static keyword.) still static Object Programming To ensure that the memory referenced by a returned pointer is to fixed memory we must refer to either global variables or static variables. to These are allocated space at compilation time. The other way is to get some memory when we The need it – see in a few moments. W e mentioned earlier that pointers and references were similar. references Both are implemented as addresses – but Both but pointers need dereferencing. pointers We can also return references. Consider the following example using the Consider struct Stype as defined earlier. struct Object Programming Object Programming W e want a function to return an Stype value. Stype value. There are four ways. There W e want a function to return an Stype value. Stype value. There are four ways. There void func1(Stype& v) { // code setting members of v } Stype func2() { Stype v; // automatic variable // code setting members of v return v; } with a call such as with a call such as func1(s); s = func2(); The function directly refers to the variable s The aliased by v. aliased Object Programming This involves copying v to some location and This to then that value is copied to s. Object Programming W e want a function to return an Stype value. Stype value. There are four ways. There W e...
View Full Document

This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online