Skip to main content
gamedev.net
🔒 Locked

String Wrappers

Started by Prozak 8 replies 550+ views
Original Post
Prozak
Prozak
I''m doing my own string class to get myself familiarised with operator overloading. I''ve seen other string classes that allow you to do: String MyName="John Doe"; Messagebox(MyName); My class has a char* array which I resize as needed, and to use my class i have to do Messagebox(MyName2.charArray); ...where ".charArray" is my char*.... So how do i remove the whole ".charArray" part, so that it auto-returns the string by name alone, like so: Messagebox(MyName2); Also, what is the correct string format to use under win32, zero-terminated char strings are good enough? Thanks for any tips on this... Jester Says: Visit Positronic Dreams!
[Hugo Ferreira][Positronic Dreams][Colibri 3D Engine][Entropy HL2 MOD]
[Yann L.][Enginuity] [Penny Arcade] [MSDN][VS RoadMap][Humus][BSPs][UGP][NeHe]
"I say we take off and nuke the entire site from orbit. It''s the only way to be sure."
ChaosEngine
ChaosEngine
overload the char* operator.. although you should really be overloading the const char* operator

"That''s not a bug, it''s a feature!"
--me
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Nervo
Nervo
One simple method within a string class is to use a vector of chars under the private label. Honestly, I don't understand why you need to call a member function to get at the data. You can define insertion << or extraction >> overloaded operators to use data from the array or vector of chars. When you pass an object of the class, through assignment/initialization as a function parameter your constructors should be doing the dirty work. Have one copy constructor take a const char* parameter, and another a size_type, and char. The compiler should use implicit conversions where necessary. I could post a string class I have, but you might not want that much help. It might be best though, if you want more specific help that you paste your class and associated functions so folks can throw in their wisdom.

[edited by - nervo on February 3, 2004 1:21:39 AM]
Well, R2D22U2..
Toni Petrina
Toni Petrina
class String {
operator const char*()
{
return Len() ? Data : _T("");
}
DWORD Len() const
{
return Data ? _tsclen(Data) : 0;
}
private:
char *Data;
}
So... Muira Yoshimoto sliced off his head, walked 8 miles, and defeated a Mongolian horde... by beating them with his head?

Documentation? "We are writing games, we don't have to document anything".
antareus
antareus
You think you want to do this, but you don''t. It triggers an implicit conversion to const char* which is very difficult to track down.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Oluseyi
Oluseyi
quote:
Original post by antareus
You think you want to do this, but you don''t. It triggers an implicit conversion to const char* which is very difficult to track down.
Listen to this man.
TangentZ
TangentZ
There is a good reason why std::string provides the explicit
c_str() and no implicit conversion to const char *.

Google it.



Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
Prozak
Prozak
well, my string class has a char* which starts out as null, and gets asigned/resized as operations are performed onto it...

i also provide some stuff like:
int = mystring;
mystring = int;
mystring = char*;

...is that bad design?

btw, why cant i do this:
mystring = "1"+"2"+"3";

I allways get a "cannot add two pointers" on that one...

Jester Says: Visit Positronic Dreams!
[Hugo Ferreira][Positronic Dreams][Colibri 3D Engine][Entropy HL2 MOD]
[Yann L.][Enginuity] [Penny Arcade] [MSDN][VS RoadMap][Humus][BSPs][UGP][NeHe]
"I say we take off and nuke the entire site from orbit. It''s the only way to be sure."
Oluseyi
Oluseyi
quote:
Original post by pentium3id
well, my string class has a char* which starts out as null, and gets asigned/resized as operations are performed onto it...
That''s not the problem. The problem is with implicit conversions, because of ambiguous situations that cause very hard-to-track-down bugs. Like tangentz said, Google the rationalizations for std::string::c_str.

quote:
i also provide some stuff like:
int = mystring;
mystring = int;
mystring = char*;

...is that bad design?
Well, not really. It does raise the question what happens when you assign a non-numeric string to an integer variable, or when the numeric characters in a string are in the middle.

quote:
btw, why cant i do this:
mystring = "1"+"2"+"3";

I allways get a "cannot add two pointers" on that one...
Because a string is not a first class type in C++; a char * is. Also check the operator precedence: the addition is completed first.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.