1. Where do I put the const, in the declaration int MemberFunc() const;)
or in the definition
1 2 3 4
int MyClass::MemberFunc() const
{
/* ... */
}
Or in both?
2. Can the using keyword be used with classes and class members? For example, if a global function needed to access a static member of a class, using MyClass::staticMember; would allow me to use staticMember in the function. Without it I'd have to type MyClass::staticMember every time.
1. First one is a declaration of a function and the second one is both the declaration and the implementation. And they both work depending on what you want to do.
2. using does work with classes. But, seriously, how often do you see people use it?
"They both work depending on what you want to do" - I know only one use of const when it appears after a member function like in the examples - it defines the function as a const function, which means that it doesn't change the object and only const functions can be used with const members. Are there other uses? And does it matter where I put the const? Where should I put it?
const stands for constant. It can be applied to just about everything where something can be changed. If you declare a variable constant, it cannot be change. If you declare a reference const, then you may not edit a part of the reference. It's not changeable.
Thanks for the explanations, but I know exactly what const does. I think you don't understand my question. I'm asking about const functions. Anyway, I found an answer at learncpp.com :
"Const member functions declared outside the class definition must specify the const keyword on both the function prototype in the class definition and on the function prototype in the code file."
That's all I needed to know about const..There's the other question (about using), but I'll just try it with a compiler...
You didn't answer the question about const (the answer is: both, declaration and definition), but it doesn't matter now. I already got the answers I needed. Thanks for helping me :)