Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Cout Problem (http://www.programmingforums.org/showthread.php?t=14771)

lucifer Dec 19th, 2007 9:36 AM

Cout Problem
 
Is
cout.operator<<("Factorial of :");
same as
cout << "Factorial of:" ;
I have searched google and checked books and all of them say it is but when i run the same code the former code gives me the address of the string.

Narue Dec 19th, 2007 2:08 PM

Re: Cout Problem
 
>I have searched google and checked books and all of them say it is
They're wrong. cout<<"blah" is a non-member function that's defined to work with C-style strings:
:

  1. template <typename T, typename Traits>
  2. basic_ostream<T, Traits>& operator<< (
  3.   basic_ostream<T, Traits>& out, const char *s )
  4. {
  5.   // Magic
  6. }

There's no equivalent member function in basic_ostream. The closest you'll get is an overloaded operator that takes a pointer to void:
:

  1. T& operator<< ( const void *p )
  2. {
  3.   // Magic
  4. }

That's why the member function gives you a number (it treats the string as a pointer) and the non-member function works like you'd expect.


All times are GMT -5. The time now is 3:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC