By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,123 Members | 1,256 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,123 IT Pros & Developers. It's quick & easy.

class member and local variable

P: n/a
Hi,
If I have a class member variable
int myMember;

Is it legal to have a class member function like this?
void MyClass::MyFunc( int myMember )
{
// some operations on myMember
}

Thanks

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I think we cant provided myMember and MyFunc belong to same class. But
it can be if we have both in different classes, because in that case we
will have an object to be passed on as type.

Jul 23 '05 #2

P: n/a
b8*******@yahoo.com wrote:
Hi,
If I have a class member variable
int myMember;

Is it legal to have a class member function like this?
void MyClass::MyFunc( int myMember )
{
// some operations on myMember
}

Thanks
You can do operations on myMember but it the operations will act on the
function arugment you are passing.

for ex:
void MyClass::MyFunc( int myMember )
{ myMember++; }
This will increment the myMember that is being passed as the argument.

However, if you want to use myMember and do opertions with it on your
class member variable myMember. You should use the this-> access
specifier.

for ex: your code should be.
void MyClass::MyFunc( int myMember )
{
// some operations on myMember this->myMember = myMember; }


Jul 23 '05 #3

P: n/a
Yes you can have that type of member function, but then the class
member myMember is made hidden by the parameter myMember. What ever you
do in that function to that myMember is not reflected to class member
myMember.

Either you can do that using

this->myMember = myMember.

or name the class member mas myMember_ or m_myMember, which ever most
suites you.

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.