operator overloading

This is a discussion on operator overloading within the C++ Programming forums, part of the General Programming Boards category; I overloaded << operator to format output in this fashion "(" variable1 ", " variable2 ", " variable3 ")"; Lets ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    operator overloading

    I overloaded << operator to format output in this fashion
    "(" variable1 ", " variable2 ", " variable3 ")";
    Lets say that my Vector class has another methods and I want to output those methods in normal way. How to undone overloading operator <<? What to write and where in the code?
    Code:
    #include <d3dx10.h>
    #include <iostream>
    using namespace std;
    
    // Overload the  "<<" operators so that we can use cout to 
    // output D3DXVECTOR3 objects.
    
    ostream& operator<<(ostream& os, D3DXVECTOR3& v)
    {
    	os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
    	return os;
    }
    
    int main()
    {
    	// Using constructor, D3DXVECTOR3(FLOAT x, FLOAT y, FLOAT z);
    	D3DXVECTOR3 u(1.0f, 2.0f, 3.0f);
    
    	// Using constructor, D3DXVECTOR3(CONST FLOAT *);
    	float x[3] = {-2.0f, 1.0f, -3.0f};
    	D3DXVECTOR3 v(x);
    
    	// Using constructor, D3DXVECTOR3() {};
    	D3DXVECTOR3 a, b, c, d, e;  
    
    	// Vector addition: D3DXVECTOR3 operator + 
    	a = u + v;
    
    	// Vector subtraction: D3DXVECTOR3 operator - 
    	b = u - v;
    
    	// Scalar multiplication: D3DXVECTOR3 operator * 
    	c = u * 10;
    
    	// ||u||
    	float L = D3DXVec3Length(&u);
    
    	// d = u / ||u||
    	D3DXVec3Normalize(&d, &u);
    
    	// s = u dot v
    	float s = D3DXVec3Dot(&u, &v);
    
    	// e = u x v
    	D3DXVec3Cross(&e, &u, &v);
    
    	cout << "u             = " << u << endl;
    	cout << "v             = " << v << endl;
    	cout << "a = u + v     = " << a << endl;
    	cout << "b = u - v     = " << b << endl;
    	cout << "c = u * 10    = " << c << endl;
    	cout << "d = u / ||u|| = " << d << endl;
    	cout << "e = u x v     = " << e << endl;
    	cout << "L = ||u||     = " << L << endl;
    	cout << "s = u.v       = " << s << endl;
    
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    19,366
    I do not really understand your question, but it sounds like you should just write another function to print in the exact way that you want.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    Quote Originally Posted by dontoo View Post
    I overloaded << operator to format output in this fashion
    "(" variable1 ", " variable2 ", " variable3 ")";
    Lets say that my Vector class has another methods and I want to output those methods in normal way. How to undone overloading operator <<? What to write and where in the code?
    How do you output a method? Do you mean produce output from within a method? What's the normal way? Either you handle the output in a different way within those methods on an individual basis or you can do something that I've done using a static data member and member function that sets and stores the current output type so that the print routine can do something different depending on what the value is:
    Code:
    #include <iostream>
    
    class foo
    {
    public:
        enum out_type {TYPE1,TYPE2,TYPE3};
        int data;
        foo(int _data = 0) : data(_data)
        {
        }
        std::ostream& print(std::ostream& os) const
        {
            if(output == TYPE1)
                return os << "This is format 1: " << data;
            else if(output==TYPE2)
                return os << "This is format 2: \"" << data << '\"';
            else if(output==TYPE3)
                return os << "This is format 3: ***" << data << "***";
            else return os;
        }
        static void SetOutputType(out_type typ)
        {
            output = typ;
        }
    private:
        static out_type output;
    };
    
    foo::out_type foo::output;
    
    std::ostream& operator<<(std::ostream& os, const foo& rhs)
    {
        return rhs.print(os);
    }
    
    int main()
    {
        foo bar1, bar2(10);
    
        foo::SetOutputType(foo::TYPE1);
        std::cout << bar1 << '\n' << bar2 << '\n' << std::endl;
    
        foo::SetOutputType(foo::TYPE2);
        std::cout << bar1 << '\n' << bar2 << '\n' << std::endl;
    
        foo::SetOutputType(foo::TYPE3);
        std::cout << bar1 << '\n' << bar2 << '\n' << std::endl;
    
        return 0;
    }
    Output is as follows:
    Code:
    This is format 1: 0
    This is format 1: 10
    
    This is format 2: "0"
    This is format 2: "10"
    
    This is format 3: ***0***
    This is format 3: ***10***
    I used to be an adventurer like you... then I took an arrow to the knee.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    7
    That will work, thx

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21