Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

shurcool

std::string wrapper class?

This topic is 5334 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, For the longest time I thought this was possible, but never tried to do it. Now that I did, I ran into some problems. Up until now, my String class was nothing more than
typedef std::string String; 
- but it''s not enough anymore. I wanted to make a small wrapper class, and add some of my own functions that I''d need. How would I do it? Sure, I could make a whole class with a private std::string member, and provide functionality to it by copying all the calls. So for each function/constructor that std::string has, I''d need an identical copy that calls the function in std::string. But that''s just plain stupid - rewriting what''s already done, causing an additional and unnecessary overhead. Simply a no-no. So I thought something like this would do the trick...
class String : public std::string {}; 
Apparently not. All I wanna do is copy all the functions/constructors that std::string has, and add a couple of my own. Isn''t this possible? ---------------------------------------------------------------- On another topic... I know I could convert a std::string into all uppercase or all lowercase by using std::transform and std::toupper/std::tolower. But is there a way to compare two strings, ignoring the case? An equivalent to Java''s String.equalsIgnoreCase(String) function. I was thinking something along std::equals(String1.begin(), String1.end(), String2.begin(), ???); But what goes in the question marks? I don''t want to write a function of my own, as I''m sure std covers that somewhere... But where? Thanks in advance, shurcooL` ---
shurcooL`

Share this post


Link to post
Share on other sites
Advertisement
"Inheriting from std::string is not useful, you cannot override any of
its functions (because none of them are virtual,) and you cannot use
std::string's protected interface (because it doesn't have one.) The
only thing you could do by inheriting std::string is to add functions to
it, which can be done just as easily with global functions that accept a
std::string pointer/reference"



Human beings, by changing the inner attitudes of their minds, can change the outer aspects of their lives.

William James (1842 - 1910)

[edited by - rohde on December 13, 2003 10:26:25 PM]

Share this post


Link to post
Share on other sites
I do not know of a single common function for a case-insensitive compare, but I do know that the standard C++ libraries for the GNU C++ Compiler include a function called strcasecmp(char *, char *) and strncasecmp(char *, char *, size_t).

If it works, wonderful. If it doesn''t, then hopefully someone else answers the question properly either while I''m typing this or before you check back.



Remember: Silly is a state of Mind, Stupid is a way of Life.
-- Dave Butler

Share this post


Link to post
Share on other sites
On the string compare issue:

No there's is no such function in the Standard. I dug this up:

bool CharsEqualIgnoreCase(char x, char y)
{
static locale loc;
return toupper(x, loc) == toupper(y, loc);
}

bool StringsEqualIgnoreCase(const string& S1, const string& S2)
{
if (S1.length() == S2.length())
{
return equal(S1.begin(), S1.end(), S2.begin, CharsEqualIgnoreCase);
}
else
return false;
}


It's from one of my old files, but I don't know who wrote it originally actually, but it's good.
You should of course call the StringsEqualIgnoreCase from your code like this: StringsEqualIgnoreCase(string1, string2);.



Human beings, by changing the inner attitudes of their minds, can change the outer aspects of their lives.

William James (1842 - 1910)

[edited by - rohde on December 13, 2003 10:45:37 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by rohde
"Inheriting from std::string is not useful, you cannot override any of
its functions (because none of them are virtual,) and you cannot use
std::string's protected interface (because it doesn't have one.) The
only thing you could do by inheriting std::string is to add functions to
it
, which can be done just as easily with global functions that accept a
std::string pointer/reference"


So you're saying it's possible, but not recommended?

Beer Hunter and Indigo Darkwolf: Thanks for the suggestions.

Looks like I'll end up using one of them. Still, I'd rather do it without converting them to c_str(), but whatever works.

Edit: rohde, that's exactly what I was talking about. Only thing is, I was hoping that the CharsEqualIgnoreCase function would be built into std. :| Oh, well.

BTW, what's the purpose of 'static locale loc;'? Because toupper only needs one parameter.

---
shurcooL`

[edited by - shurcooL on December 13, 2003 10:45:57 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by shurcool
quote:
Original post by rohde
"Inheriting from std::string is not useful, you cannot override any of
its functions (because none of them are virtual,) and you cannot use
std::string's protected interface (because it doesn't have one.) The
only thing you could do by inheriting std::string is to add functions to
it
, which can be done just as easily with global functions that accept a
std::string pointer/reference"


So you're saying it's possible, but not recommended?

Beer Hunter and Indigo Darkwolf: Thanks for the suggestions.

Looks like I'll end up using one of them. Still, I'd rather do it without converting them to c_str(), but whatever works.

And so there's nothing on std::equals? It looked just like what I needed.

---
shurcooL`

[edited by - shurcooL on December 13, 2003 10:40:54 PM]



Yeah it is possible. Sure. But it won't give you anything extra, at all! If you need some extra functions to work on strings you might as well define them outside a class and put them in a namespace for packaging. That would be a very common C++ thing to do actually.




Human beings, by changing the inner attitudes of their minds, can change the outer aspects of their lives.

William James (1842 - 1910)

[edited by - rohde on December 13, 2003 10:46:28 PM]

Share this post


Link to post
Share on other sites
On toupper and locale:

The toupper from my code is the toupper from The C++ Standard Library, NOT the toupper from CRT (C Run-Time).

The C++ toupper takes two arguments:


template
CharType toupper(
CharType _Ch,
const locale& _Loc
) const;

Parameters
_Ch
The character to be converted to upper case.
_Loc
The locale containing the character to be converted.




Human beings, by changing the inner attitudes of their minds, can change the outer aspects of their lives.

William James (1842 - 1910)

[edited by - rohde on December 13, 2003 10:52:24 PM]

Share this post


Link to post
Share on other sites
I don''t know why, but...

String test = "AbCdE zzz-ZZZAP!";
if (test.substr(test.begin() + test.find(''z''), test.end() - test.begin()).toupper()
== "ZZZ-ZZZAP!") return true;
Makes more sense to me than...

String test = "AbCdE zzz-ZZZAP!";
if (toupper(test.substr(test.begin() + test.find(''z''), test.end() - test.begin()))
== "ZZZ-ZZZAP!") return true;
Also, take a look at the edit in my previous post.

---
shurcooL`

Share this post


Link to post
Share on other sites

template<class _Ty>
struct equal_to_insensitive : binary_function<_Ty, _Ty, bool>
{
bool operator()(const _Ty& _X, const _Ty& _Y) const
{
return (std::tolower(_X) == std::tolower(_Y));
}
};

std::equals(String1.begin(), String1.end(), String2.begin(), equal_to_insensitive());

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!