J
John
Hi,
In the code below, if I define member functions that take an A object,
then I am able to change protected data members of the passed in A
object. Does this mean that all members functions of a class or friend
functions of the same class? This surprised me. I can understand the
need for copy/assignment operators, but this seems to be true for all
member functions.
Thanks,
John
==================
#include <iostream>
class A
{
protected:
int y;
public:
virtual int abc() { return y+1; }
A() : y(1) {}
A(const A& a) { y = a.y; }
A& operator= ( A& a )
{
y = a.y;
return *this;
}
int xxx ( A& a ) { a.y =8; }
};
int main()
{
A a;
A b(a);
A c;
std::cout << a.abc() << "\n";
c = a;
std::cout << a.abc() << "\n";
std::cout << b.abc() << "\n";
std::cout << c.abc() << "\n";
c.xxx(a);
std::cout << a.abc() << "\n";
return 0;
}
=====
In the code below, if I define member functions that take an A object,
then I am able to change protected data members of the passed in A
object. Does this mean that all members functions of a class or friend
functions of the same class? This surprised me. I can understand the
need for copy/assignment operators, but this seems to be true for all
member functions.
Thanks,
John
==================
#include <iostream>
class A
{
protected:
int y;
public:
virtual int abc() { return y+1; }
A() : y(1) {}
A(const A& a) { y = a.y; }
A& operator= ( A& a )
{
y = a.y;
return *this;
}
int xxx ( A& a ) { a.y =8; }
};
int main()
{
A a;
A b(a);
A c;
std::cout << a.abc() << "\n";
c = a;
std::cout << a.abc() << "\n";
std::cout << b.abc() << "\n";
std::cout << c.abc() << "\n";
c.xxx(a);
std::cout << a.abc() << "\n";
return 0;
}
=====