Char array's in class's

  • 18 years ago

    hello children yep, i got anouther problem with C++


    ok, before any1 points this out to me, i am aware that there are header files that do what im trying to do, however im coding them myself in an attempt to learn the basics.


    I am trying to make a string class which replaces all those great VB string functions ,, eg Len() Mid() UCase() LCase() Right() Left().


    the problem is, that i have to declair a maximum length fro my string. to waste less memory, i have made a base class with all the functions that has the largest string [1024] and then i make loads of derived classes with only 1 difference,,,, the size of the string, so the user can use whatever class depending on wether they need to store a small name, or huge string.


    here's how it works..


    class CString1024
    {
    protected:
    char m_string[1024];


    public:
    //loads of functions and over loaded operators
    };


    class CString512: public String1024
    {
    protected:
    m_String[512]
    };




    as you can see, CString512 has a smaller string, but inherits all the fnctions from CString1024...
    and the idea was that it only used a string array of half the size,,


    BUT, testing has shown that all derived classes use the base class's size String.


    How do i fix this ?


    thanX






  • 18 years ago

    I think is better for u to declare as char pointer and allocate memory in the constructor. But if u don't wanna, try to use the <this> pointer. something like:


    char *String1024::Left(int n = 1)
    {
       char *resp = new char[n +1]
       memset(resp, 0, n+1);
       strcpy(resp, this->m_string, n);
       return resp;
    }


    I hope it helps.


    Best Regards,
    Dirso.

  • 18 years ago

    eh ?
    you can actually increace the size of an array during runtime !! ??


    i was told you had to specify the max length of an array in the code, and it was fixed.

  • 18 years ago

    U can allocate memory how many times u want to.


    Just Try this:
    // Base Class
    class CStrClass
    {
    protected:
     char *mcharString;
     int  m
    iMaxIndex;
     void m_vChangeCase(bool Upper);        


    public:
     CStrClass(int iMaxLength = 1024)                  
    {
         miMaxIndex = iMaxLength ;
         m
    charString = new char[miMaxIndex + 1];
         memset(m
    charString, miMaxIndex + 1, 0)
     
     }
     ~CStrClass(){ delete []m
    charString; }


    /// ...
    };
     


    // and try this 1 to ur derived classes
    // Derived Class'
    class CStrClass64: CStrClass
    {
    public:
     CStrClass64():CStrClass(64 /* change to value to the max length of ur class /)
     {
       /
    nothing to do here, the base class do it for u */
     }
    };


    Best Regards,
    Dirso

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.” - Cory Doctorow