class help

This is a discussion on class help within the C++ Programming forums, part of the General Programming Boards category; OK, I made a class called CMyString, it has a member at the end called data, it's a char*, what ...

  1. #1
    Former Member
    Join Date
    Oct 2001
    Posts
    955

    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

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    yes. you just need to write functions to translate your calls into string library calls passing them the pointer to your private string data.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    operator overloading will come in useful too. you can overload + to do a strcat for example.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    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
    };
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    does it really work the way I want it to?

    Oskilian

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    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.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    Cool, thanks I'll try it!

    Oskilian

  8. #8
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    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

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    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......

  10. #10
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21