0

I want to new a StringArray as follow:

class Test
{
public: 
    QString* str[];
    Test() {
        str = new QString[];
    }
};

is there some wrong: about str = new QString[] ?

3
  • 3
    Chances are that you really want something more like std::vector<QString> (or use one of Qt's collections). Commented Mar 26, 2012 at 5:03
  • Qt already has QStringList as a built-in typedef; I suggest looking into that. Commented Mar 26, 2012 at 5:07
  • yeah! i know QStringList, but i just want to the mechanism about c++ how to do that,, In my memory, c++ can't do that, but i forget..i am not sure..Aha.. Commented Mar 26, 2012 at 5:12

5 Answers 5

3

You didn't specify a size for the array.

Note that there is special syntax for the delete operator when deleting an array as well:

QString* strings;
strings = new QString[10];
delete[] strings;

If you want a dynamic array, consider using STL vector (std::vector<QString> in this case) instead.

Sign up to request clarification or add additional context in comments.

3 Comments

by the way, Can it be initialized like Assignment , string[] = {"USA", "CHINA"} , ? thanks for your help..
@Tu_JianWu In C++11 it can be: QString *arr = new QString[10] {"abc","def"}; Whether you can use that depends on your compiler.
@ravuya: That actually wouldn't be correct. The [] on the declaration would make strings a pointer to a pointer. The correct declaration would be QString* strings;.
2
QString* str[]; 

Declares a array of the type QString*. If you need a single string, what you need is, simply:

QString* str;
str = new QString;

However, Usually You don't need to do allocate dynamic memory explicitly. QString would provide its own memory management.

If you need a collection of strings, You are much better off using an container class like vector.

QVector<QString> str;

1 Comment

if i change it to <code>QString* str;</code> , there is a wrong : expected primary-expression before ']' token, what i want is that i hava a point that point to a string list , like "USA", "CHINA", "ENGLAND", then , how can i do ? thanks for help ...
1

The size must be specified, or it won't work.

str = new QString[10];

Comments

1

QString* str[] declares an array of QString pointers. A pointer is more or less synonymous with an array. If you just want an array of QString's, you should do this:

QString* str;
Test() {
    str = new QString[10];
}

This will make str point to an array of 10 QStrings. The [] in your declaration is incorrect unless you would want to make a 2D array. Of course, if you do this, make sure to remember to delete [] str; at some point to release your memory.

If the size of your array is known at compile time, you're better off not using any dynamic allocation at all:

QString str[10];
Test() { }

This code will work the exact same as it does above, but you can avoid dealing with the new and delete.

Or, as others have suggested, you can just use std::vector<QString> and let the STL deal with it.

2 Comments

thanks for your help..Can it be initialized like Assignment , string[] = {"USA", "CHINA"} , ?
@Tu_JianWu: I'm not familiar with Qt, but I don't see why you wouldn't be able to. However, you'd only be able to do it within a function. This: class Test { QString str[] = {"USA", "CHINA"}; Test() { } }; isn't allowed by the C++ standard unless you're using C++11.
0

You probably want QStringList

1 Comment

oh, no,, i know QStringList, but i just want to know c++ whether can implement it directly 、 thanks for your help..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.