Thread: adding method to STL class

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    adding method to STL class

    Can I do that? I just wrote a "tokenize string" function:
    Code:
    vector<string> tokenize(string &str, string tokens);
    and then thot, well, why not do this:
    Code:
    vector<string> string::tokenize(string tokens);
    However, I get "error: too few template-parameter-lists" for that line...
    Last edited by MK27; 03-27-2010 at 10:04 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    To add members to a class, well, you need source for the class (because that name has to appear in the definition). And then, of course, almost by definition, you are no longer in std::string, but MK27::string.

    I've never tried to inherit from a STL class (which seems like the next most obvious choice), but I remember hearing that it usually fails horribly since the STL wasn't really designed for that sort of thing.

    (ETA: And of course, since string is a template class with (at least) three templated arguments (i.e., string<charT, traits, allocator>) you would have to have that in your definition as well.)
    Last edited by tabstop; 03-27-2010 at 10:12 AM.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Free functions are they way to go if you want to extend the interface of existing classes.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Probably not worth the bother then.

    Quote Originally Posted by tabstop View Post
    I've never tried to inherit from a STL class (which seems like the next most obvious choice), but I remember hearing that it usually fails horribly since the STL wasn't really designed for that sort of thing.
    I believe the caveat has to do STL classes not having virtual functions, so:
    Code:
    string *ptr = new myDerivedClass(...
    will foul up.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    How about writing a wrapper class around it?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is no point in writing a wrapper class; writing a free function like what you did will do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Callback function as class method
    By schifers in forum Windows Programming
    Replies: 39
    Last Post: 05-19-2008, 03:02 PM
  2. best STL method to implement a binary tree
    By MatthewDoucette in forum C++ Programming
    Replies: 8
    Last Post: 06-16-2006, 07:08 AM
  3. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM
  4. cannot use vector in class method?
    By Shadow12345 in forum C++ Programming
    Replies: 0
    Last Post: 05-29-2002, 10:45 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM