Thread: Consolidating two functions

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Consolidating two functions

    Hello, all.

    Let's say there are two class member functions that do the same thing but have different argument types.

    Code:
    bool some_class::isX(string S) const
    {
    return stl_set_object.end() != stl_set_object.find(S)
    }
    
    bool some_class::isX(const char* S) const
    {
    return stl_set_object.end() != stl_set_object.find(S)
    }
    So I consolidated this with a template to:
    Code:
    template <class T>
    bool isX(T S) const
    {
    return stl_set_object.end() != stl_set_object.find(S)
    }
    But what if I wanted to pass a string object as a reference and leave character pointers alone.

    So the first function would be modified to:
    Code:
    bool some_class::isX(string& S) const  //note the addressof operator here
    {
    return stl_set_object.end() != stl_set_object.find(S)
    }
    Would there still be a way to consolidate the two functions (one with string reference argument and another with const char*) into one using a template?

    It strikes me as counter-intuitive, but I was just curious to know the flexibility of the STL and c++ with regard to this.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In theory, yes. In practice, it's more work than just having the two functions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Would you mind telling me how it might be done? Or at least throwing me a link?

    Thanks.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Two ways.
    Either you can write metafunctions that decide the right argument type (or use those available in Boost).

    Or you can define a string_arg class that hides the difference by calling c_str() on a string and storing it or storing a char* directly. (Of course that only works for immutable arguments.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thanks.

    And you're right. It does seem to be more trouble than it's worth.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM