hi I need something about this (inside a class). Is there a way??
thanksQt Code:
class prove { private: int size; char* save[size]; }To copy to clipboard, switch view to plain text mode
hi I need something about this (inside a class). Is there a way??
thanksQt Code:
class prove { private: int size; char* save[size]; }To copy to clipboard, switch view to plain text mode
Regards
What you want is a dynamic array. But both C++ and Qt have various data-structures you can use instead. std::vector, QVector, QList...
"The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry
hi, I donn't want a dynamic array. I'm looking for a way to put "save" as member of a class to permit to all members to use that space of memory....Are you understand? I don't like declare it as global (anyway: at begin of program I don't know how size is large....
Regards
Then you still want a dynamic array. Look it up.
I'm guessing you want all instances of the class to be able to use the memory. Make it static.
static char** save;
"The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry
Do you need something like this...
Qt Code:
class A { public: enum {size = 100}; A(){} private: char* save[size]; };To copy to clipboard, switch view to plain text mode
But a char** is not a string, and that's what he says he wants here. I guess he just wants to allocate a certain amount of memory. I still say: dynamic array.
However, maybe mickey should explain more clearly what it is he wants here.
(By the way, pet peeve of mine: don't use more than one question mark. It serves no purpose.)
"The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry
mickey-->
Are u looking something like this
Qt Code:
char *pChar; string sStr; cout<<"Please Enter The String "; cin>>sStr; //string input pChar = new char[sStr.length() + 1]; strcpy(pChar, sStr.c_str()); delete []pChar;To copy to clipboard, switch view to plain text mode
I'm not yet clear, actually what is that you are looking for. The above example only uses char*. Please explain what is that you want....
Probably you want something like this
Qt Code:
class A { public: A() { save = 0; } void allocate() { //assuming size is known by now save = new char*[size]; // allocate pointers for(int i=0; i < size; i++) save[i] = new char[whatEverSizeYouNeed]; } private: int size; char **save; };To copy to clipboard, switch view to plain text mode
The biggest difference between time and space is that you can't reuse time.
-- Merrick Furst
I did a mistake in the first post. I wanted something like this below:
Qt Code:
class A { public: int size; A(){} private: char* save; }; A::A (int sz) { size = sz; save = new char [size] }To copy to clipboard, switch view to plain text mode
Regards
So what's the problem? Looks like it should work. If your constructor has a parameter, at least. You have one in your implementation, but not in your class definition.
Also, why is A::size public? Do you ever want to change the size of the string after the object has been created?
If so, a public variable is not enough (and a public variable is always bad style). You need to create a resize(uint size) function. This function should create a new dynamic array with the new size, copy all old values into it, delete the old array and let A::save point to the new array. There is no other way.
Alternatively, if you just want a string with a dynamic size, there are loads of classes that already do this for you. std::string and QString, for example.
"The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry
Bookmarks