-
class help
OK, I made a class called CMyString, it has a member at the end called data, it's a char*, what I want to do is that all functions that can use a char* can support my class whitout having to call the meber each time, I just want that whenever a CMyString is used, it directly uses my char*, is this possible?
Oskilian
-
yes. you just need to write functions to translate your calls into string library calls passing them the pointer to your private string data.
-
operator overloading will come in useful too. you can overload + to do a strcat for example.
-
Code:
class CMyString {
public:
int size();
operator char*() { return str; } // will promote a CMyString to char* anytime a function takes a char* but is passed a CMyString
CMyString operator + (const CMyString& other);
// etc
private:
char* str;
int strLen;
// etc
};
-
does it really work the way I want it to?
Oskilian
-
Yeah. It will actually probably work too well. I don't remember the specifics, but I believe Dorian had some problem where there was ambiguity caused by the automatic conversion... which is why there is the the explicit c_str() member function of std::string.
-
Cool, thanks I'll try it!
Oskilian
-
I have another question, it's an extension to my last one.
if I want to use it, for example, in the funtion strcmp, do I have to make the explicit casting?
like strcmp((char)(MyStringABC),"Hello");
or can I just have
strcmp(MyStringABC,"Hello");
?
Oskilian
-
How about if you create a class function to replace strcmp?
MyStringABC str1;
MyStringABC str2;
if(str1.compare(str2)){
//Rock & Roll
}
you have to recieve str2 as a reference and use its accessor functions to compare it to the string data already in str1......
-
no can do, I'm going to use that class in a lot of functions, and I can't implement them all simply because I don't know them all.
the idea is that you can use that class in any WIN32 API function, any DirectX function, any string.h function, and my own functions. (I can't use the char* because my own functions will need to use my own class).
Oskilian