-4

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++?

Community
  • 1
  • 1
dani
  • 341
  • 1
  • 7
  • 18

4 Answers4

1

It means -- pass by reference.

alinsoar
  • 15,386
  • 4
  • 57
  • 74
1

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.

PermanentGuest
  • 5,213
  • 2
  • 27
  • 36
1

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
-1

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

Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
  • Seriously, did you even read the question? This is far off, completely misleading and peppered with a link to a horrible tutorial. – pmr Aug 06 '12 at 11:46
  • The question was changed – Eamonn McEvoy Aug 06 '12 at 13:37
  • Even without the change in the question this is still horribly wrong. – pmr Aug 06 '12 at 13:54
  • @EamonnMcEvoy: Nothing in the question has anything to do with pointers. The use of `&` in a declaration (as in the question) to declare a reference is completely different to its use in an expression (as in your link) to take the address of an object. – Mike Seymour Aug 06 '12 at 14:59
  • @MikeSeymour thanks for pointing that out, I didn't realise there was a difference. http://stackoverflow.com/questions/114180/pointer-vs-reference – Eamonn McEvoy Aug 06 '12 at 15:12