Overloading "

Hi,
Can anyone tell me how can I overload the operator "<<"??
Thanks,

Comments

  • : Hi,
    : Can anyone tell me how can I overload the operator "<<"??
    : Thanks,
    :

    simply just write your own << operator function. Here's a very simple example.

    [code]
    #include <iostream>
    using namespace std;

    class CMyClass
    {
    public:
    CMyClass() {x = 0;}
    void operator<<(int k)
    {
    x = k;
    }
    int getx() {return x;}
    private:
    int x;
    };


    int main()
    {
    CMyClass obj;
    obj << 4;
    cout << obj.getx() << endl;
    cin.ignore();

    return 0;
    }
    [/code]




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion