Can anyone give example about passing an object of a class as argument to 开发者_运维知识库the function of same class.
class Unicorn {
void Eat(Unicorn other_unicorn) {
// implementation omitted to keep this answer family-friendly
}
};
int main() {
Unicorn glitter;
Unicorn dazzle;
glitter.Eat(dazzle); // mmmm, yummy
}
Note that Dazzle is still alright because we made a copy of him and fed the copy to Glitter.
class X
{
public:
void func(X x) {}
};
int main()
{
X a,b;
a.func(b);
}
Just to be pedantic, I'd recommend passing objects by reference, and making them const by default, eg (with thanks to James McNellis)
class Unicorn {
void Eat(const Unicorn & other) {
// Nothing else changes
}
};
Not sure if this what you want but anyways
class MyClass { int a; void whatEver(MyClass myclass); };
void MyClass::whatEver(MyClass myclass) { ....do something }
and now somewhere in the code
MyClass obj1; obj2;
obj1.whatEver(obj2);
精彩评论