Thread: insertion operator question

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    19

    insertion operator question

    How does the << operator works??

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How does the << operator works??
    This gets my vote for the "innocent question with a REALLY long and complicated answer" award.
    My best code is written with the delete key.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >How does the << operator works??
    Like Prelude said the answer is long and complicated. Dave's link will show you how to make an inserter for your own objects.

    Making the answer not so complicated... the stream inserter is an operator that takes a reference to the stream itself and a const reference of the data you want to insert, and writes it into the stream. Exactly how this is done depends on what kind of stream it is, and what the data is, and if any stream manipulators were used on that data.

    Since cout and friends are created before you ever use it, and destructed automagically by the compiler, you don't need to worry about it. cout has an inserter for all built in types.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    19
    thanks a lot to the 3 of you.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Basically it calls a member function for the stream called operator<< to do the actual ouput. Thus:

    Code:
    double foo = 18.9;
    cout << foo;
    Is equivalent to:
    Code:
    double foo = 18.9;
    cout.operator<<(foo);


    This member function is overloaded to handle all the basic primary data types (ints, floats, chars, doubles, char *, etc...). Additional user defined types (classes,structs) can also be created and the operator<< function overloaded to support the writing of those particular object to the stream in question.

    The function's return type is a reference to the modified stream which allows them to be chained together thus you can do:
    Code:
    double foo = 18.9;
    char * bar = "Hello World";
    cout << foo << ' ' << bar;
    which is equivalent to three seperate calls:
    Code:
    double foo = 18.9;
    char * bar = "Hello World";
    cout.operator<<(foo);
    cout.operator<<(' ');
    cout.operator<<(bar);
    Yeah... this can get a bit involved but that's a quick and dirty primer.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick, Partiotion, and Insertion Sort
    By silicon in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2005, 08:47 PM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 12:30 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM