Const functions and "using" classes

I have two unrelated questions:

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.
Why don't you try these things and see if any will compile?
closed account (S6k9GNh0)
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?

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/using_directive.htm

Might be a bit different with different compilers. I'm not sure, I've never tried. :/
Last edited on
"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?
Google "const correctness" and read the first few links to get an idea of just how extensively the const keyword is used.
closed account (S6k9GNh0)
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...
Last edited on
closed account (S6k9GNh0)
I answered both. And other people answered it. If you declare a function const, you cannot edit the copied parameters.

See my old post for more about USING keyword.
Last edited on
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 :)
Last edited on
Topic archived. No new replies allowed.