Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a class. There is a function and a variable.
C++
class myClass
{
private:
   int num;
   int Calc(int x);
};

int myClass::Calc(int x)
{
   int num = 12536; //constant
   return num /*First One (Global)*/ + num /*2nd One (1 inside the function*/ + x;
}



Now, How Can I use both variables(num's) inside Calc Function ? I think above function is not corrent.
Posted
Comments
[no name] 13-Sep-12 11:06am    
Try this.num for the "gobal"
Philip Stuyck 13-Sep-12 11:57am    
this is very bad programming. Avoid reusing names like this.
There is coding conventions to be followed:
int _num; for member variables
int lnum; for local variables if you want to emphesize that they are local.
variable name hiding is what you do here, and is not a good idea.

1 solution

Use:
C++
this->num

To referance your class member.


[edit]
Actualy the best solution would be to change the names so as to remove any ambiguity.
C++
this->num

Will also work though.
 
Share this answer
 
v2
Comments
Captain Price 13-Sep-12 11:33am    
thanks, it helps

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900