Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing objects as arguments

Tags:

c++

Can anyone give example about passing an object of a class as argument to the function of same class.

like image 841
Shweta Avatar asked Apr 10 '26 02:04

Shweta


2 Answers

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.

like image 79
James McNellis Avatar answered Apr 12 '26 15:04

James McNellis


class X
{
public:
    void func(X x) {}
};

int main()
{
    X a,b;
    a.func(b);
}
like image 26
Benjamin Lindley Avatar answered Apr 12 '26 14:04

Benjamin Lindley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!