PDA

View Full Version : new



mickey
11th July 2007, 17:38
hi I need something about this (inside a class). Is there a way??


class prove {
private:
int size;
char* save[size];
}

thanks

Michiel
11th July 2007, 17:52
What you want is a dynamic array (http://www.fredosaurus.com/notes-cpp/newdelete/50dynamalloc.html). But both C++ and Qt have various data-structures you can use instead. std::vector, QVector, QList...

mickey
11th July 2007, 20:25
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....

Michiel
12th July 2007, 08:28
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;

vermarajeev
12th July 2007, 09:53
Do you need something like this...


class A
{
public:
enum {size = 100};
A(){}
private:
char* save[size];
};

Michiel
12th July 2007, 10:55
I don't think so, because:


at begin of program I don't know how size is large

By the way, if you want a constant, why not just say: const int size = 100;

vermarajeev
12th July 2007, 11:13
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....

Why not use string in that case, why you dont want a builtin type (like string, vectore, list ). IS there any particular reason????

Rather than using a char*, it is always better to you std::string.

Michiel
12th July 2007, 12:18
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.)

vermarajeev
12th July 2007, 14:19
mickey-->

Are u looking something like this

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;

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....

Gopala Krishna
12th July 2007, 14:29
Probably you want something like this

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;
};

mickey
12th July 2007, 15:50
I did a mistake in the first post. I wanted something like this below:


class A
{
public:
int size;
A(){}
private:
char* save;
};
A::A (int sz) {
size = sz;
save = new char [size]
}

Michiel
12th July 2007, 16:17
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.