Reference one very important difference is that as

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: want a function to return an Stype value. Stype value. There are four ways. There Stype* func2() { Stype v; // automatic variable // code setting members of v return &v; } Stype& func2() { Stype v; // automatic variable // code setting members of v return v; } with a call such as s = *func2(); Only the address is returned. Less copying, except for that to s. Less with a call such as s = func2(); Same effect, without the dereference. Object Programming Object Programming There are differences between returning a reference value as opposed to a pointer. reference One very important difference is that, as the One reference is an alias, it must refer to an actual memory location (a variable) and hence 0 (NULL) cannot be returned in this case. cannot If you want to return 0, you must return a pointer If value. value. Object Programming W ouldn't it be nice to be able to get new memory locations for variables when you need them? Especially for arrays when you know how much you need? you W ell, C++ provides a way of asking for memory Well, from an area called the heap. heap To request memory for a particular datatype, say To datatype say an int, we can specify int var = new int; new int Object Programming The new operator gets the number of bytes new operator required for that type and returns the address of that memory. that The C++ standard says that an exception occurs, much the same as if we divide by zero or try to take the square root of a negative number. take This is then stored in the pointer variable on the This left hand side of the assignment. left This causes a termination of the program– unless unless an exception handler has been registered by the programmer – not covered in this subject. What happens if the memory is not available? OR Well, there are two possibilities. the address returned is 0 Object Programming As exceptions are beyond this subject (and the latter is the more traditional method) we'll discuss this approach. this Thus, whenever we ask for memory with the new Thus, new operator, we should check that the address we operator, got back is non-zero. got Some compilers have an option to select the form Some of handling new exceptions. new Object Programming Object Programming Instead of just saying Instead x = new int; new int We use x = new(nothrow) int; new(nothrow int This ensures no exception is thrown and a zero This value is returned. value But we must include the line #include <new> Object Programming For example, For For example, For float* f; float* f; f = new float; if (f == 0) // don't go on using f // don't f = new(nothrow) float; new(nothrow if (f == 0) // don't go on using f // don't Once allocated this way, the pointer variable can Once be used just like any other pointer. This new memory location can be used to store values. memory *f = 3.56; g = *f/2.; Object Programming Object Programming Because it is a pointer, it must be dereferenced – dereferenced a bit unfortunate. We can then say We But the more d...
View Full Document

This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online