Parameter to a function in C++ [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
const CFoo &bar() const
Which is the meaning of this line?
virtual void Encode (KDataStream & stream) const;
What´s the meaning of const at the end in C++?

Which is the meaning of this line?
virtual void Encode (KDataStream & stream) const;
It's a statement that declares a function.
virtual means it's a member function that can be overridden by a function of the same name and compatible parameter and return types declared in a class derived from this one. The correct version will be chosen (at run-time, if necessary) according to the type of the object it's invoked on.
void means it doesn't return anything.
Encode is the name of the function.
( marks the start of the parameter list.
KDataStream is the type of the first parameter.
& means the parameter is passed by reference.
stream is the name given to the parameter; it serves as documentation, but can be left out of the declaration without changing the meaning.
) marks the end of the parameter list.
const means that it's a member function that can't modify non-static, non-mutable data members of the object it's invoked on. It also allows it to be invoked on objects that are declared const.
; marks the end of the statement.

It means -- pass by reference.

The 'const' at the end of the method says that the method implementation will not change the values of any member variables. So, by seeing this in the class interface itself (without having to know the implementation), the clients of the object can know about this behaviour.

Read up on pointers, if you want to code in c++ you will need to know how these work:
http://www.cplusplus.com/doc/tutorial/pointers/
& means you are passing in the memory address of stream rather the value of stream

Related

When to use const in the beginning and in the end of a method declaration in C++ [duplicate]

This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 6 years ago.
When coding a header file in C++ with method declarations, what's the difference between:
int getFoo() const;
const int getFoo();
const int getFoo() const;
First one, is for preventing this method changing any member variables of the object.
Second one, is for the return type (ie: constant integer)
Third one, is mix of both
Your first function operates on a const this pointer (that is; a const object that it can't change (or at least shouldn't)).
Your second function returns a constant integer - which is somewhat nonsensical since you can just assign it to a non-const variable and change it anyway. Besides, why does the function care if you change a POD type or not?
Your third function is just a combination of the first two. A function operating on a const object returning a const value.

c++ - difference between const and static keyword in function header

Consider the following code:
class Test
{
public:
//1
int kon1() const;
//2
const int kon2();
//3
static int kon3();
};
As far as I know, the difference between function 1 and 2 is that :
Function 1 says that the function will not be able to change any data member's value
Function 2 says that it will return a const int
(If I have wrong understanding, please correct me)
My question is :
As we can see there, if we want to make a function to be const function, the const keyword is placed behind. But why in function 3, the static function, the static keyword is placed in front?
For const member functions must have the const keyword afterwards to avoid ambiguity with the return type.
For static, virtual and other keywords having a dramatic effect on how the function works, it's desirable to list it first so it's easier to see in the class definition. For example, we can quickly scan through a list of member functions and spot all the static functions, or all the virtual ones - aiding our understanding of the overall use of the function.
Marking a member function const (or e.g. an override) is a less crucial distinction - if you have a non-const object you can invoke functions whether they're const or not, the appropriate const-ness is often obvious to the reading developer as they absorb the function return type and identifier, and in some corporate/project coding standards mutating functions are grouped above const-accessors, or const and non-const versions of the same member function are side by side to emphasise their similarities - then the differet const-ness stands out more.
All these factors combine to make the actual choices in C++ optimal for development, but you're right in observing that they're a bit inconsistent.
You are mixing two concepts i.e. Storage Class with Storage Type.
C++ have following kind of storage classes
auto, register, static, extern & mutable
And following kind of storage type (based on what u can do with on storage)
read only (can be initialized ) --> this is const
read and write --> this is non const.
So when u define a variable/function u have tell in advance what kind of storage type u want to associate. Thats why u put static as first keyword in ur code.
Hope this helps.
The keyword static does not modify the variable's type. It modifies the memory address in which it will be located. It is used identically for function-type variables, and for data-type variables:
static int n; // data
static int n (); // function
The keyword const does modify the variable's type. For function-type variables, this keyword has two possible meanings:
modify the function's return value as type const:
const int n (); // function can be invoked from non-const objects only, and returns a const value
modify how this function may be invoked
int n () const; // function can be invoked const and non-const objects alike, and returns a non-const value`
int kon1() const;
This function is readonly function intended to work on const data only.
const int kon2();
This function can work on modifiable object but it returns type is readonly and caller can not modify this.

What does const after function header do? [duplicate]

This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 8 years ago.
I saw following code on c++ reference:
bool operator() (const int& lhs, const int&rhs) const
What does the last const do?
From the Const Correctness tutorial:
If you have a const object, you don't want to call methods that can
change the object, so you need a way of letting the compiler know
which methods can be safely called. These methods are called "const
functions", and are the only functions that can be called on a const
object. Note, by the way, that only member methods make sense as const
methods. Remember that in C++, every method of an object receives an
implicit this pointer to the object; const methods effectively receive
a const this pointer.
It might be worthwhile (spoiler: it is) to read through the whole article if you're new to the concept of constness.
It means the function (operator) does not change the object.
Effectively makes the "this" pointer a pointer to a const object. It means that members of the object cannot be modified in that method, nor can that method be invoked on a non-const object.

Difference between the * and & operator in function calls [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ functions: ampersand vs asterisk
What are the distinctions between the various symbols (*,&, etc) combined with parameters?
I am wondering the difference between the address operator & and the deference operator * in a C++ function call. For example take the following function
void foo (std::string& param)
{
param = "Bar.";
std::cout << param.size();
}
and let's call it in our main() function like so...
int main()
{
std::string test;
foo(test); //Why not foo(&test)?
std::cout << test; //Prints out the value "Bar."
}
First off, why does the & operator allow me to assign a value as if it were a pointer (assigning it a value that survives the RAII and scope of the function foo() when it's not a pointer) as it is able to be printed out in my main() function even though it's not static? I am assuming it is not a pointer because I am able to access the size() method by using the . operator instead of the -> which is used for pointers.
Secondly, what would be the difference between using the & operator in a function parameter vs. using the * operator? Is it even different than just a plain variable like std::string param? It appears to be called like that (foo(test) instead of foo(&test)).
& function parameter specifically signifies that this parameter is being passed-in by reference (traditionally compilers implement this as a pointer) which is why you see the effect of this assignment in your main().
static would have nothing to do with that.
The difference in declaring a parameter to a function using & and * is that the second one allows a nullptr (or a non-existent or just a plain invalid address) to be passed-in while the & guarantees that there's a real object being referenced by this function's argument. Other than that both provide similar functionality of allowing an original object to be changed via it's reference.
test on it's own (without &) would pass a copy to the string to the function, whilst the & means it will pass a reference.
FYI: When you don't need a reference, best-practise dictates passing all objects as const references.

Clarification on pointer to non-static class member

When I need a pointer to member of class, I do as following
struct MyStruct
{
int foo();
};
int (MyStruct::*p)() = &MyStruct::foo;
My question is why do I need to use & operator to take the address which may be ignored if it were a static function. Also, I've heard pointer to members aren't really a pointer, Can someone clarify that?
If it's a static function, it works just as a regular non-member function pointer: the function name itself can be implicitly converted to a function pointer.
If it's a non-static member function, it's no longer the same thing as a non-member function:
It has a hidden this parameter;
In a multiple inheritance scenario, converting a pointer to one of the base classes may produce a pointer to a different address. This means that if the member function is inherited, the this pointer may need to be adjusted before the call. This already makes it impossible to use a pointer to store a pointer-to-member-function.
Raymond Chen wrote an interesting article about this with more details and examples.
why do I need to use & operator to take the address which may be
ignored if it were a static function
You are right, in the case of pointer to member function syntax ideally & can be omitted. I think, & syntax is there may be due to historical convention.
I've heard pointer to members aren't really a pointer, Can someone
clarify that?
That's not correct. The only difference is that they are pointer to member function. Since, class non-static member contain an implicit this pointer as their argument, they have a special signature. Also, they are not inter-convertible with normal function pointer with same signature.
In your code example, theoritically p is pointing to:
int MyStruct::foo (MyStruct* const);