CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2000
    Location
    Germany
    Posts
    369

    overriding manipulator endl

    How can i override the manipulator endl. I tried the following thing:
    Code:
    #include<iostream>
    
    inline ostream& std::endl( ostream& os )
    {
      os << "\n\n\n" << flush;
      return os;
    }
    
    void main(void)
    {
      cout << "hallo" << std::endl;
    }
    But it doesen´t work. Why? It should. I thing i have some problem with the namespace, maby.

    Thanks in advance

  2. #2
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    endl is defined as
    template class<_Elem, _Tr>
    basic_ostream<_Elem, _Tr>& endl(
    basic_ostream<_Elem, _Tr>& _Ostr
    );

    so try defining your function the same. Also, I don't think its a good idea to try and use the std namespace too.

  3. #3
    Join Date
    May 2000
    Location
    Germany
    Posts
    369
    I know that endl is definied this way. I already tried to redefine endl in this way, but it doesn´t work too. Now i used the following code without any success:
    Code:
    template <class _Elem, class _Tr>
    inline basic_ostream<_Elem, _Tr>& endl( basic_ostream<_Elem, _Tr>& OutStream )
    {
    	OutStream << "\n\n\n" << endl;	
    	return OutStream;
    }
    It is no problem to define my own manipulator but when i try to redefine one it doesn´t work...

    Thanks

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Why do you expect to be able to redefine a function that's global
    to a namespace? Is that possible to do?

    --Paul

  5. #5
    Join Date
    May 2000
    Location
    Germany
    Posts
    369
    Hello!
    I found something in my programming book. The author write that you can add!!! manipulators to the already existing ones. I thing it is not possible to change the behavior of the ex. manipulators. But if someone knows an way i would be very interessed in.

    Thanks

  6. #6
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Well, overriding functions only works when you create a class
    that is derived from another class. Each function will have its own
    signature, however. In this manner, the compiler knows to insert
    code to call that one particular function. In the event of virtual
    functions, that decision is made at run-time. With "global"
    functions [like manipulators] ... even if they're in a namespace
    [like std] ... you can't just redefine the function because then
    you will have two functions with the same signature. If you
    supply endl, which version should the compiler call? Usually,
    endl is going to be implemented entirely in a header file
    somewhere in your std headers. If you REALLY need to change
    the behavior of endl, you'd have to edit this header file. However,
    this is clearly non-portable behavior.

    Since you can add your own manipulators, there's no real need
    to change endl's behavior. If you SOMETIMES want to output a
    newline and flush the buffer and SOMETIMES you want other
    behavior, you could have another manipulator that checks the
    existance of a global boolean; if this boolean is true, it does the
    customized behavior; otherwise, it could call endl.

    If you let us know what you're really trying to do, we can help
    you out more effectively

    --Paul

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    I thought the standard says that you shouldn't do this sort of thing. You're not allowed to extend the std namespace (so, by implication, you shouldn't alter or overload anything defined in it).

    To modify one of Paul's questions slightly: Why on earth would you want to override/overload std::endl? As Paul says, write your own manipulator - it's easy enough, Stroustrup has a whole section on it in TC++PL.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured