how do u overload operator << so that cout<<A prints out the letter A as well as the value of A

Discussion in 'C++' started by bluekite2000@gmail.com, Sep 9, 2005.

  1. Guest

    Here A is an instantiation of class Matrix. This means whenever user
    writes
    Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2
    //and fills it up w/ random value
    cout<<A;

    the following will be printed
    A
    3
    2
    ..1 .9
    ..2 .4
    ..5 .6

    currently i only have
    3
    2
    ..1 .9
    ..2 .4
    ..5 .6

    so I d like to add the string A.
     
    , Sep 9, 2005
    #1
    1. Advertising

  2. Re: how do u overload operator << so that cout<<A prints out theletter A as well as the value of A

    wrote:
    > Here A is an instantiation of class Matrix. This means whenever user
    > writes
    > Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2
    > //and fills it up w/ random value
    > cout<<A;


    cout << "A\n" << A;

    > the following will be printed
    > A
    > 3
    > 2
    > .1 .9
    > .2 .4
    > .5 .6
    >
    > currently i only have
    > 3
    > 2
    > .1 .9
    > .2 .4
    > .5 .6
    >
    > so I d like to add the string A.
    >


    V
     
    Victor Bazarov, Sep 9, 2005
    #2
    1. Advertising

  3. Jordan Guest

    If you wrote the Matrix class yourself, you are going to have to give
    it a "name" member variable and name it in the constructor. Then you
    merely pull out this value when you use "<<". I'm not sure you can
    just reference the code name of a variable at runtime since it gets
    compiled into machine code and no longer your naming scheme.
     
    Jordan, Sep 9, 2005
    #3
  4. What you need is a meta-information "var-name", asking the var itself.
    That's possible in Java or .NET, but not standardized in C++.

    Workaround:
    A simple approach would be

    #define COUT_W_VARNAME(var) cout << #var << var

    <> schrieb im Newsbeitrag
    news:...
    > Here A is an instantiation of class Matrix. This means whenever user
    > writes
    > Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2
    > //and fills it up w/ random value
    > cout<<A;
    >
    > the following will be printed
    > A
    > 3
    > 2
    > .1 .9
    > .2 .4
    > .5 .6
    >
    > currently i only have
    > 3
    > 2
    > .1 .9
    > .2 .4
    > .5 .6
    >
    > so I d like to add the string A.
    >
     
    Oliver \(Nospam\), Sep 9, 2005
    #4
  5. <> wrote in message
    news:...
    > Here A is an instantiation of class Matrix. This means whenever user
    > writes
    > Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2
    > //and fills it up w/ random value
    > cout<<A;
    >
    > the following will be printed
    > A
    > 3
    > 2
    > .1 .9
    > .2 .4
    > .5 .6


    If all you need is any identification on the output, you can use the address
    of the object in operator<<:

    #include <iostream>

    using namespace std;

    class Foo
    {
    friend ostream & operator<< (ostream &, Foo const &);

    int i_;

    public:

    explicit Foo(int i)
    :
    i_(i)
    {}
    };

    ostream & operator<< (ostream & os, Foo const & object)
    {
    return os << "Foo at " << &object << ":\n"
    << object.i_;
    }

    int main()
    {
    Foo object(42);
    cout << object << '\n';
    }

    Ali
     
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Sep 9, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,652
  2. vertigo

    big letter -> small letter

    vertigo, Jul 6, 2004, in forum: Python
    Replies:
    4
    Views:
    759
    Reinhold Birkenfeld
    Jul 6, 2004
  3. Tony Meyer

    RE: big letter -> small letter

    Tony Meyer, Jul 6, 2004, in forum: Python
    Replies:
    0
    Views:
    507
    Tony Meyer
    Jul 6, 2004
  4. Andrew McNamara

    Re: big letter -> small letter

    Andrew McNamara, Jul 6, 2004, in forum: Python
    Replies:
    2
    Views:
    752
    Scott David Daniels
    Jul 6, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    255
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page