473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

question on cout and operator<<

Consider the code:

#include <iostream>

using namespace std;

int main( )
{
cout << "test string ";
cout.operator<<(10).operator<<(endl);

return 0;
}

This prints
test string 10
followed by a newline as expected.

However, in main( ), if I have

cout.operator<<("test string
").operator<<(endl).operator<<(10).operator<<(endl );

it prints
0x804897c
10
followed by a newline. It does not print test string but instead
prints its address.

I do not understand.
Does it mean that operator<<(const char *) is not a member function of
ostream ? If so why ?

Kindly explain.

Thanks
V.Subramanian

Sep 19 '07 #1
3 1854
On 2007-09-19 08:37, su**************@yahoo.com, India wrote:
Consider the code:

#include <iostream>

using namespace std;

int main( )
{
cout << "test string ";
cout.operator<<(10).operator<<(endl);

return 0;
}

This prints
test string 10
followed by a newline as expected.

However, in main( ), if I have

cout.operator<<("test string
").operator<<(endl).operator<<(10).operator<<(endl );

it prints
0x804897c
10
followed by a newline. It does not print test string but instead
prints its address.

I do not understand.
Does it mean that operator<<(const char *) is not a member function of
ostream ? If so why ?
You are correct, but as to why it is not a member I do not know. Last
time I saw this discussed none seemed to be able to come up with a good
reason, you could try asking in comp.std.c++ though.

--
Erik Wikström
Sep 19 '07 #2
On Sep 19, 2:40 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-09-19 08:37, subramanian10...@yahoo.com, India wrote:
Consider the code:
#include <iostream>
using namespace std;
int main( )
{
cout << "test string ";
cout.operator<<(10).operator<<(endl);
return 0;
}
This prints
test string 10
followed by a newline as expected.
However, in main( ), if I have
cout.operator<<("test string
").operator<<(endl).operator<<(10).operator<<(endl );
it prints
0x804897c
10
followed by a newline. It does not print test string but instead
prints its address.
I do not understand.
Does it mean that operator<<(const char *) is not a member function of
ostream ? If so why ?

You are correct, but as to why it is not a member I do not know. Last
time I saw this discussed none seemed to be able to come up with a good
reason, you could try asking in comp.std.c++ though.

--
Erik Wikström
I just now found a member function
operator<<(const void * val)

Could the reason be the following :
If we wanted to print some address, we have to call
cout.operator<<(ptr) EXPLICITLY for any pointer ptr. So, by calling
cout.operator<<("test string"), the compiler assumes that we want to
print the address and so it prints 0x804897c
When we give cout << "test string", the overloaded non-member function
is called to print the string literal itself.

Is this reasoning correct ? Excuse me if I am wrong.

Kindly explain.

Thanks
V.Subramanian

Sep 20 '07 #3
On 2007-09-20 05:13, su**************@yahoo.com, India wrote:
On Sep 19, 2:40 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2007-09-19 08:37, subramanian10...@yahoo.com, India wrote:
Consider the code:
#include <iostream>
using namespace std;
int main( )
{
cout << "test string ";
cout.operator<<(10).operator<<(endl);
return 0;
}
This prints
test string 10
followed by a newline as expected.
However, in main( ), if I have
cout.operator<<("test string
").operator<<(endl).operator<<(10).operator<<(endl );
it prints
0x804897c
10
followed by a newline. It does not print test string but instead
prints its address.
I do not understand.
Does it mean that operator<<(const char *) is not a member function of
ostream ? If so why ?

You are correct, but as to why it is not a member I do not know. Last
time I saw this discussed none seemed to be able to come up with a good
reason, you could try asking in comp.std.c++ though.

--
Erik Wikström

I just now found a member function
operator<<(const void * val)

Could the reason be the following :
If we wanted to print some address, we have to call
cout.operator<<(ptr) EXPLICITLY for any pointer ptr. So, by calling
cout.operator<<("test string"), the compiler assumes that we want to
print the address and so it prints 0x804897c
When we give cout << "test string", the overloaded non-member function
is called to print the string literal itself.

Is this reasoning correct ? Excuse me if I am wrong.
Not quite, the only overload of the << operator which takes a pointer is
the one for the char* (or perhaps is was const char*), if you want to
print any other pointer you can use the operator as normal:

#include <iostream>

int main()
{
int* i = new int(1);
std::cout << i;
}

I would assume that the reason for overloading for char* is to allow the
common usage of printing a string literal:

std::cout << "Hello World";

which would not work otherwise.

--
Erik Wikström
Sep 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: JustSomeGuy | last post by:
I have a double that I want to output as: (depending on its value) 1000 Bytes 1.6 Kilobyte 2.5 Megabytes ..5 Terabytes can I do this with... ostream & operator<<(ostream & o, double n)
4
by: bluekite2000 | last post by:
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...
3
by: Yudan Yi \(OSU\) | last post by:
I have a question to define a friend operator<< for a class. for example, I can define friend ostream& operator<<(ostream& os, const TTest& x) { ...; return (os); }; While I want to add more...
12
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
46
by: suresh | last post by:
Hi, For this code snippet, I get the following output, which I am unable to understand. (2^31 = 2147483648) cout<< -2147483648 << endl; cout << numeric_limits<int>::min() <<',' <<...
5
by: Alex Vinokur | last post by:
void foo (int n) { std::ostringstream oss; oss << "ABCD: " << n << std::endl; std::cout << oss.str() << std::flush; } That function has been invoked in multiprocessing mode.
8
by: Goran | last post by:
Hi all, I have a question regarding operator <<. A lib of mine contains a class with an overloaded operator << as NON- class member. This would look like: #include <iostream> #include...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.