Results 1 to 2 of 2

Thread: Char Arrays as Class Members

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Unhappy Char Arrays as Class Members

    Been reading about chars, and arrays and all that for a while now, and googling my questions but still can't figure this one out:

    1.) How to go about creating a char member variable in a class. I've got a member variable:

    Qt Code:
    1. char chString[];
    To copy to clipboard, switch view to plain text mode 

    and a member function:

    Qt Code:
    1. void AddString(char chText)
    2. {
    3.  
    4. }
    To copy to clipboard, switch view to plain text mode 

    What do I need to do in the add text function to be able to add text to the member variable?

    2.) And is there some way of creating a constructor that would allow me to populate the char string member from the get go? Simply doing this isn't working

    Qt Code:
    1. Field(char chFieldText) : m_chText(chFieldText)
    2. {}
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2013
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Char Arrays as Class Members

    Members of C++ classes need to stay of a static size, and must be programmed to use the heap if they want to have variable length members.

    e.g.
    class ExClass_A
    {
    private:
    char chFieldText[256]; //Works fine for what you want to do, assuming you're okay with always taking up 256 bytes, and never going over 256 bytes.
    }

    class ExClass_B
    {
    private:
    char chFieldText[]; //Is equivalent to "char *chFieldText", which has scope-issues*.
    }

    class ExClass_C
    {
    private:
    QString chFieldText; //This isn't a c-primitive, and doesn't come with the limitations of scope, copying. It relies on the heap to efficiently store variable-length text, but hides most of that management from the programmer.
    }

    class ExClass_D
    {
    private:
    vector<char> chFieldText;// This would be the pure C++ form. Or you could use "string" instead of "vector<char>", which is also better and pure C++. Both vector and string (which is effectively vector) use the heap for space-efficient storage, but a lot of that magic is hidden away from the programmer.
    }



    If you're using QT, I recommend QString.
    Last edited by Tros; 27th January 2013 at 19:04.

Similar Threads

  1. Abstract base class and inherited class members
    By JovianGhost in forum General Programming
    Replies: 3
    Last Post: 19th March 2010, 12:32
  2. for i = 0 to nr members in a class ...
    By qt_gotcha in forum Qt Programming
    Replies: 4
    Last Post: 6th March 2010, 19:38
  3. how to limit char * arrays?
    By mhoover in forum General Programming
    Replies: 2
    Last Post: 24th June 2009, 19:38
  4. Question about class members
    By serenti in forum Qt Programming
    Replies: 5
    Last Post: 11th June 2009, 14:20
  5. char arrays to "multiline editor"
    By baray98 in forum Qt Programming
    Replies: 2
    Last Post: 29th January 2008, 06:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.