Custom Message Function

This is a discussion on Custom Message Function within the C++ Programming forums, part of the General Programming Boards category; Hi! Is it possible to create a function which behavior is exact as for function "cout"? Instead of using "myFunc("hello ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question Custom Message Function

    Hi!

    Is it possible to create a function which behavior is exact as for function "cout"?

    Instead of using "myFunc("hello %s!", personName);"
    I want to do it like this:
    "myFunc("hello " << personName << "!");"

    If it's possible, than please give me some direction how to create such function.

    Thanks!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    You would need a string class that overloads the << operator. The c++ string class provides this functionality via the + operator, so you could use that. The only drawback is that it only works for string types eg. string literals, char arrays, string objects.

    Code:
    #include <iostream>
    
    using std::string;
    
    // A null string object
    const string null_s = "";
    
    int main(void)
    {
        char buf[] = "Hello, World!";
    
        string combine = null_s + "TEST " + buf;
    
        std::cout << combine;
        std::cin.get();
    
        return 0;
    }
    Last edited by The Dog; 04-18-2004 at 09:50 AM.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You can just create your own class that overloads the << operator, for a number of different types. Then you can output it in whatever way you please.

    I made a similar class once for debugging purposes. As I was using Win32, cout did not do anything, so I created a "debug" object. To keep track of what my programs were doing, I simply streamed messages into the object, which were then appended to an edit control in the window.

    Is that what you want to do?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Yes, that's I want to do. But it seems difficult. I'm a newbie in C++.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Have you used classes very much? If not, then do that first.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 08:38 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21