By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,495 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,495 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
bl**********@gmail.com 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
Sep 9 '05 #2

P: n/a
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.

Sep 9 '05 #3

P: n/a
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

<bl**********@gmail.com> schrieb im Newsbeitrag
news:11*********************@o13g2000cwo.googlegro ups.com...
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 '05 #4

P: n/a
<bl**********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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

Sep 9 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.