Question:
C++ newbie: advantage of using const in function declarations--need help understanding this example?
Antst
2011-01-24 03:45:51 UTC
Hi all:

I would be very grateful if someone could help me understand an example of using "const" in a function declaration.

In general I understand that const should be used in a function declaration to alert people that the function can deal with constant objects. But I don't understand this example in my text:

Using const in function declarations can reduce errors without compromising safety or efficiency. For example, recall the declaration of operator* for the Wood class:

const Wood operator*(const Wood& lhs, const Wood& rhs);

Why should the return type be const? Otherwise, you could write code like this:
Wood a, b, c; (a * b) = c;

This would not be allowed by a built-in type. Making operator* return const disallows this for the Wood type too. End of example...

This is probably a dumb question, but I don't understand what is wrong with writing code like: Wood a, b, c; (a * b) = c; What is the problem with this? Thanks for any help!
Five answers:
peteams
2011-01-24 10:54:46 UTC
(a * b) = c;



Without the const, that is quite possibly a legal statement. It creates the product of a and b as an object on the stack, it assigns this new object to the value of c and finally, as the pièce de résistance, it pops the calculated and assigned object off the stack throwing away all its hard work.



So the problem is being solved is avoiding expensive non-operations.



This situation occurs in several languages, often there are constructs where you can write "x.y = z", and what quietly happens is a copy is made of x, it's y is assigned z, and the result thrown away. The programmer is then left scratching their head wondering what went wrong.
2016-10-23 11:34:26 UTC
a million) i'm uncertain the place you obtain that line from, yet it quite is operator overloading. you prefer an operator after the operator key-word it fairly is what you're overloading. because of the fact that is not obtrusive from the function itself what you're attempting to do i p.c. to propose you replace the operator key-word with a significant function call. This function is returning a reference on your templated grid cellular. The variables x and y are being handed in from yet another function, although that is not significant what they have been reported as beforehand because of the fact x and y have been created to hold those values in the process this function. 2) double Row() const {return xsize_;} is a function that returns the xsize_ variable. It returns a double and the const statement states that this function can not substitute any information interior theGrid2D type. this could be a potential of becoming classes much less stressful to debug. 3) The delete function isn't probable mandatory, because of the fact the deconstructor aspects the 'delete' to stability the 'new'. although that's stable prepare to establish you're cleansing up your stuff manually quite than in line with a deconstuctor to sparkling up for you. 4) row and col are variables which will give up to exist while the function ends. subsequently they'd't be used later and their values would desire to be saved in some thing greater everlasting subsequently the xsize_ and ysize_ variables that are sure to persist for as long because of the fact the Grid2D occasion exists (besides the actuality that they'd substitute). 5)having declared the grid in considerable (uncertain if there grew to become into meant to be greater code), right this moment write: grid.make(5, 10); this could create a grid with 5 rows and 10 columns. On a facet word, you will probable locate you will desire to alter (a minimum of) the xsize_ and ysize_ double's to int's, because of the fact an array (subsequently grid_) won't manage to be listed utilising a double.
es
2011-01-24 04:03:14 UTC
In this example, you are overloading the * operator, for the class Wood.



You want to make sure that your operator function is not able to change anything it shouldn't, so that there are no unanticipated side effects.



To see why you want this, think about what a binary * operator usually does -- if you were to call this on integers a * b, you would not want either a or b to be changed by the operation (as lhs and rhs refer to the left-hand side and right-hand side of the original expression).



I suspect that making the result also be constant would add another layer of protection against side effects, preventing accidental "tricks" by which one of the operands can be changed by being used as the left hand side of the calling expression.



You would not want to be able to say (a * b) = c, because this is attempting to assign a new value (the value of "c") to an actual expression, "a * b." But you can't assign a value to "a * b" -- how would you implement it? Where would you store the result? You want to make sure that you can attempt to assign a value only to an appropriate variable.
koppe74
2011-01-24 04:54:01 UTC
Arguments can be passed to a function in several ways; by value, by pointer (address) or by reference.



When you pass by value; a local copy of the value of the argument (often the value/content of a variable) is created in the function. All subsequet changes by the function (like incrementing or swapping) is done by the local copy, and the variable the function was called with remains unchanged.



When you pass by pointer; the *address of* the argument (must be a variable) is passed the function. The function can thus access the original variable, and changes made by the function (like swapping the content of arguements or changing the content) will also be visible outside the function. The function is after all working with the original, not a copy.



This makes it possible to have arguments intended only for input (not to be changed), only for output (space reserved outside the function, but empthy until the function is called), or for input/output (content changed by the function, e.g. a string turned to all upper-case).



The const-modifier is used for the arguements that should be input-only, to prevent the content from accidently being changed.



Strings and other arrays, are implicitly pointers (their names return their address). For other types -- including the primitive types (int, char, float) and objects -- the &-operator is used before the variable-name to return their address. To prevent a string from being changed -- or being allowed to pass a string-constant -- you must prepend the type with the const-modifier. (e.g. const char *string).
2011-01-24 03:54:05 UTC
const means you can't change value of the variable, only use it



(a * b) = c;



a*b - is a number, not variable it's value stores in memory until " ; " occures. Due to this the statement isn't legal.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...