passing a class member function by reference?
Is it okay to do this?
Code:
#include<iostream>
int main()
{
class someclass
{
public:
void function(someclass&);
// private:
int a;
};
someclass someclassobject;
someclassobject.a=16;
cout<<someclassobject.a<<endl;
function(someclassobject.a);
cout<<someclassobject.a;
return 0;
}
void someclass::function(someclass& a)// error here
{
a=18;
}
Re: passing a class member function by reference?
Quote:
Originally posted by m712
Is it okay to do this?
Code:
#include<iostream>
int main()
{
class someclass
{
public:
void function(someclass&);
// private:
int a;
};
someclass someclassobject;
someclassobject.a=16;
cout<<someclassobject.a<<endl;
function(someclassobject.a);
cout<<someclassobject.a;
return 0;
}
void someclass::function(someclass& a)// error here
{
a=18;
}
function(someclassobject.a);
Is wrong because the function takes a someclassobject, not an integer so you should call like this:
function(someclassobject);
void someclass::function(someclass& a)// error here
{
a=18;
}
a = 18 should be a.a=18, because in a = 18, a is the object, you'll have to overload the assignment operator to get a=18 to work.