tags:

views:

79

answers:

2
void foo(const ClassName &name)
{
    ...
}

How can I access the method of class instance name?

name.method() didn't work. then I tried:

void foo(const ClassName &name)
{
    ClassName temp = name;
    ... ....
}

I can use temp.method, but after foo was executed, the original name screwed up, any idea? BTW, the member variable of name didn't screwed up, but it was the member variable of subclass of class screwed up.

+3  A: 

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);
    ... ....
}
Péter Török
ClassName::method() is a non-const method. I am thinking if there is a way one can get around this if ClassName::method() is non-const. cuz all I need is to know some information of name, I don't want to change it
small_potato
@small_potato: Why don't you make method() as a const-method?
Naveen
@small: The way you get around it is to make `ClassName::method()` a const method: `ClassName::method() const`. http://www.parashift.com/c++-faq-lite/const-correctness.html provides lots of good information.
Dennis Zickefoose
Instead of going to `const_cast`, the parameter can be pased as non const reference.
Naveen
@small_potato, see my update. @Naveen, good point, I missed that - apparently I need another cup of tea yet :-(
Péter Török
@Péter Török: I have a doubt in your last point. How you remove the constness using <const_cast> for object 'name' inside the method. ?... Please quote that with example.
AKN
@AKN, see my update.
Péter Török
+1 for update2. Thanks Peter.
AKN
+6  A: 

Going by the description you provided, it looks like method() in ClassName is a non-const method. If you try to call a non-const method on a const object compiler will throw an error. When you did ClassName temp = name; you created a copy of the variable into a temporary non-const object temp. You can call any method on this object but since it is a copy, the modification done on this object will not be reflected in name object.

EDIT

The best way to solve this, is to make the ClassName::method const if it doesn't modify any member variables. If it does, then you should not take your parameter in function foo as a const-reference. You should take parameter as a non-const reference.

Naveen