What's the difference between coutcout and cout&cout in c++?
cout << cout
is equivalent to cout << cout.operator void *()
. This is the idiom used before C++11 to determine if an iostream is in a failure state, and is implemented in std::ios_base
; it usually returns the address of static_cast<std::ios_base *>(&cout)
.
cout << &cout
prints out the address of cout
.
Since std::ios_base
is a virtual base class of cout
, it may not necessarily be contiguous with cout
. That is why it prints a different address.
Difference between `cout x` and `cout.operator(x)` and `operator(std::cout, x)`?
Operators can be implemented in different ways, in particular an operator<<
for which the left hand side is your type can be implemented as either a free function or as a member function of that left hand side type.
While you must implement ostream& operator<<(ostream&, MyType const&)
as a free function (since MyType
is not the left hand side), the library implementation can choose* to implement operator<<
for some fundamental types insde the std::ostream
type (which is really a particular instantiation of a template, I am trying to ignore the details).
Edit: After checking with the standard this is incorrect:
This is what you are noticing in the code, the overload that takes a const char*
is implemented as a member of ostream
(basic_ostream<char,char_traits<char>
).
The overloads taking manipulators are implemented as member functions (Q2), and there is an implicit conversion from const char*
to const void*
that will be picked if you use the syntax for explicitly calling a member operator (Q1). For Q3, the answer would be:
operator<<(std::cout, "Hello").operator<<(std::endl);
* The implementation is actually not free to choose, since the standard mandates the signatures.
Difference between 'coutcout' and 'cout&cout' in c++?
When you do this:
cout << cout;
You are relying on the stream's implicit conversion to void*
. This value is used (pre-c++11) for testing the state of the stream. It is unspecified what the value actually is, it just needs to be NULL if the stream is in a fail state, and a non NULL otherwise. Maybe it's returning the address of a member of the object, but it's not really important, and is implementation defined.
When you do this:
cout << &cout;
That is getting the actual address of cout
.
Note that in C++11 and beyond, the first one, cout << cout;
, will no longer compile, because the implicit conversion to void*
no longer exists. Instead there is an explicit conversion to bool
to serve the same purpose.
C++ difference between %p and cout &ptr
Here, you're printing the address of fPtr
:
cout << "Address fPtr: " << &fPtr << endl;
And here, you're printing the value of fPtr
:
printf("%p", fPtr);
Those are not the same thing.
If you want to print the address of fPtr
with printf
, then you need
printf("%p", &fPtr);
If you want to print the value of fPtr
with std::cout
, then you need
cout << fPtr << endl;
difference between cout and write in c++
ostream& write ( const char* s , streamsize n );
Is an Unformatted output function and what is written is not necessarily a c-string
, therefore any null-character found in the array s
is copied to the destination and does not end the writing process.
cout
is an object of class ostream that represents the standard output stream.
It can write characters either as formatted data using for example the insertion operator ostream::operator<<
or as Unformatted data using the write
member function.
What is the correct answer for cout a++ a;?
You can think of:
cout << a++ << a;
As:
std::operator<<(std::operator<<(std::cout, a++), a);
C++ guarantees that all side effects of previous evaluations will have been performed at sequence points. There are no sequence points in between function arguments evaluation which means that argument a
can be evaluated before argument std::operator<<(std::cout, a++)
or after. So the result of the above is undefined.
C++17 update
In C++17 the rules have been updated. In particular:
In a shift operator expression
E1<<E2
andE1>>E2
, every value computation and side-effect ofE1
is sequenced before every value computation and side effect ofE2
.
Which means that it requires the code to produce result b
, which outputs 01
.
See P0145R3 Refining Expression Evaluation Order for Idiomatic C++ for more details.
difference between printf() and std::cout with respect to pointers
The format specifier %p
prints a void *
, (untrue: so the char *
is implicitly converted to void *
) the char *
is converted to void *
before printing. (But this is actually undefined behavior, see comments. The correct way to do that would be printf("%p", (void *) str1);
) The corresponding C++ code would be std::cout << (void *) str1 << '\n';
.
The code std::cout << str1;
prints str1
as null terminated string. The corresponding C-code would be printf('%s', str1);
What is the output of cout (a, b) and why?
What is the output, is it the same on all systems?
The output is as you describe: the second of the two values. It's well defined.
Why is that statement possible?
Because the comma operator allows you to evaluate more than one thing in a single expression.
is there documentation on it?
It's documented in the C++ standard, [expr.comma]. In C++11, that's 5.18.
To summarise, the comma operator:
- evaluates the expression before the comma;
- discards that value;
- evaluates the expression after the comma;
- yields that value as the overall expression value.
You can see that from your output: in each case, the output is the value after the comma.
It's completely pointless in this case; but is useful if the first expression has side-effects which you want to sequence before the second, in a situation that only allows a single expression. For example:
for (int i = 0, j = n; i < j; ++i, --j)
The operator allows the final expression to do two things, even though you can only put one expression there.
Related Topics
Access Child Members Within Parent Class, C++
Mbcs Error Building Mfc C++ Project with Visual Studio
How to Find the Minimum Value in a Map
Why Are Std::Shuffle Methods Being Deprecated in C++14
Does Class/Function Order Matter in C++
Is It a Strict Aliasing Violation to Alias a Struct as Its First Member
Cmake: How to Change Properties on Subdirectory Project Targets
Does the Alignas Specifier Work with 'New'
Off-The-Shelf C++ Hex Dump Code
How to Call a C++ Method from C
Trouble with Dependent Types in Templates
Nat-Traversal Implementation for P2P Connection
How to Output the Actual Array in C++
Use Static_Assert to Check Types Passed to MACro