Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 19th, 2007, 8:36 AM   #1
lucifer
Programmer
 
lucifer's Avatar
 
Join Date: Oct 2005
Posts: 84
Rep Power: 3 lucifer is on a distinguished road
Question 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.
__________________
"You're good... but me, I'm magic"
lucifer is offline   Reply With Quote
Old Dec 19th, 2007, 1:08 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
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:
cpp Syntax (Toggle Plain Text)
  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:
cpp Syntax (Toggle Plain Text)
  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.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
having problem with data not saving to a file codylee270 C++ 5 Apr 13th, 2006 7:39 PM
Vector problem - find max, min and positions codylee270 C++ 12 Mar 20th, 2006 12:57 AM
cgi/perl script + IE problem joyceshee Perl 2 Jan 24th, 2006 11:10 AM
problem with user defined class mixed with functions willj729 C++ 4 Oct 9th, 2005 3:26 PM
help with correcting a problem hopeolicious C++ 1 Mar 1st, 2005 2:49 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:23 PM.

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