If I understand you correctly, you want to call name.method() inside foo() and the compiler doesn't let you. Is ClassName::method() a non-const method by any chance? Since name is declared as a const parameter to foo(), you can only call const functions on it.
Update: if ClassName::method() is non-const, but does not actually change state, the best would be of course to make it const. In case you can't for some reason, I see the following ways:
- (the obvious way, as @Naveen pointed out - thanks :-) declare
name as a non-const method parameter.
- make a non const copy of
name, and call method on it - as you actually did. However, this works only if the assignment operator and/or copy constructor is properly implemented for ClassName. However, you write "after foo was executed, the original name screwed up", which is a very vague description, but it can be interpreted so that copying did have some unwanted side effects on name, which suggests that those functions are not implemented correctly or at all.
- (the nasty way) cast away constness with
const_cast - this should really be a last resort, and only if you are sure that ClassName::method() does not actually change any state.
Update2, to @AKN's comment - example of casting away constness:
void foo(const ClassName &name)
{
ClassName& temp = const_cast<ClassName&>(name);
... ....
}