473,406 Members | 2,259 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,406 software developers and data experts.

About cout

Source code:
#include <iostream>
using namespace std;

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

return 0;
}

The output as follow:
0046B01C ----why it's an address?

My question:
1) Why the output was an address and why not "test string" as expected
?
2) What's the difference between
cout<<"test";
and
cout.operator<<("test");
?
(I had thought they were the same! )

Dec 4 '06 #1
4 1267
tu******@gmail.com wrote:
Source code:
#include <iostream>
using namespace std;

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

return 0;
}

The output as follow:
0046B01C ----why it's an address?

My question:
1) Why the output was an address and why not "test string" as expected
?
2) What's the difference between
cout<<"test";
The equivalent of the above should be:

std::operator<< (std::cout, "test");
and
cout.operator<<("test");
This only dumps an address.
?
(I had thought they were the same! )
Dec 4 '06 #2
On Dec 4, 1:03 pm, tuyon...@gmail.com wrote:
Source code:
#include <iostream>
using namespace std;

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

return 0;

}The output as follow:
0046B01C ----why it's an address?

My question:
1) Why the output was an address and why not "test string" as expected
?
2) What's the difference between
cout<<"test";
This calls this function:
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os , const
basic_string<charT,traits,Allocator>& str );
and
cout.operator<<("test");
This (I think) calls this function:
basic_ostream<charT,traits>&
operator<<(basic_streambuf<char_type,traits>* sb );

--
Erik Wikström

Dec 4 '06 #3
tu******@gmail.com wrote:
My question:
1) Why the output was an address and why not "test string" as expected
?
2) What's the difference between
cout<<"test";
and
cout.operator<<("test");
?
(I had thought they were the same! )
operator<< doesn't necessarily have to be implemented as a member
function.

By using the explicit call to the member function, you won't
match any of the nonmember function overloads.
Dec 4 '06 #4
tu******@gmail.com wrote:
Source code:
#include <iostream>
using namespace std;

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

return 0;
}

The output as follow:
0046B01C ----why it's an address?

My question:
1) Why the output was an address and why not "test string" as expected
?
There is no perfectly fitting operator in the class, so the type is using
the conversion that fits best.
2) What's the difference between
cout<<"test";
Here, the compiler can chosse between member an non-member operators.
and
cout.operator<<("test");
?
Here, the operator must be a member. Only few of the streaming operators for
std::cout are members.
(I had thought they were the same! )
Dec 4 '06 #5

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

Similar topics

11
by: kazack | last post by:
I am under the the impression that a variable is what is stored in a memory address and a pointer is the memory address of the variable? I was looking to use a function that does not return a...
4
by: Andrew | last post by:
Is it possible a constructor of a class to use a funtion defined in the same class ????
4
by: John | last post by:
Hi all: I write the following code about vector and list. Why the result is not correct? Thanks. John --------------------------------------------------------------
36
by: utab | last post by:
Dear, I have experince in C( numerical projects, like engineering problems, scientific applications) I have the basic notion of C++ also, I have read Accelerated C++ until Chapter 7, however it...
7
by: Yuanfei | last post by:
Hi There, I just found that there is a problem in vc2005 regarding to time_t and localtime. See code snippets belows. Using this code segment, I found that when ut is 86200, the corresponding...
2
by: TheRomance | last post by:
i have a problem about insert integer to link list. sorry it's too long but i try many times, many ways , it's still have an error function is fix . can't change anything about function. i...
3
by: b69316 | last post by:
Hi, I am a little confused on something. I am using DevC++. I am working on a project to make a "dinosaur walk across a map". I am not sure about the distance formula....i have no clue how to...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
8
by: Devon Null | last post by:
I was wondering if there is a container (i.e. a vector) that can hold data structures of differing sizes. I was thinking of something along the lines of a container of classes. Think of a backpack...
3
by: Jakiro | last post by:
I have some question about linked list. how to complete the delete and search function?? #include <iostream> #include <string> #include <cstdlib> using namespace std;
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.