programm doubt

#include<iostream>
using namespace std;
class Buddy {

};

template<class T>

class My {

int i;
public:

void play(My<Buddy>& s) {
s.i = 3;
}

};

int main() {
My<int> h,m;
My<Buddy> me, bud;
h.play(bud);
me.play(bud);

} ///:~


Hi this programm is failing to compile saying that not able to access "i"

if i put "i" in public section it is working fine. but here my doubt is play(My<Buddy>& s) method and int i; are part of class My only?? why it is failing to access inside class method also??


can anybody clarify my doubt

thanks in advance

Regards
Siva
Because you are breaking encapsulation, even though the type of *this is the same
as My<Buddy>.

Would you, for example, expect the line to compile if *this were a My<Enemy> instead?
No, because My<Buddy> and My<Enemy> are different types in the same sense as
vector<char> and deque<char> are different types in the same sense as int and double
are different types.

The problem is that to allow the above line of code to compile allows containment to be
broken, by allowing class A to access the private parts of class B directly.
Topic archived. No new replies allowed.