12-05-2004, 11:03 AM | #1 (permalink) |
Crazy
Location: Arnold, MD
|
[c++]Dynamic arrays within class's?
I'm trying to figure this out with no luck so far. How do I dynamically allocate a character array within a class? Do I have to set a pointer in the private secion and then link to that pointer from the constructors? Any advice would be great, a quick sample code would be even better. Thanks for the help in advance.
|
12-05-2004, 11:20 AM | #2 (permalink) |
Insane
Location: Wales, UK, Europe, Earth, Milky Way, Universe
|
In the private bit you can say
char* foo; Then in the constructor, you'd initialise it to whatever foo = "bar"; And to get at it from another class you'll need to write an accessor method which returns a char*
__________________
There are only two industries that refer to their customers as "users". - Edward Tufte |
12-07-2004, 10:11 AM | #3 (permalink) |
Junkie
|
Class DynamicArray
{ public: DynamicArray(int size); ~DynamicArray(); private: char *A; int N; }; DynamicArray:ynamicArray(int size) { N=size; A=new int[N]; } DynamicArray::~DynamicArray() { delete A; } I havn't compiled this or anything so there may be a few typos but you should get the basic idea. Then just add what ever functionality you want. |
12-09-2004, 03:41 PM | #5 (permalink) |
Wehret Den Anfängen!
Location: Ontario, Canada
|
std::vector is your friend.
typedef std::vector<int> int_vec; int_vec foo; foo.resize(40); for (int_vec::iterator it = foo.begin(); it != foo.end(); ++it) { *it = 7; } foo[6] = 2; for (int i = 0; i < foo.size(); ++i) { printf("value %d is %d\n", i, foo[i]); } int* data_ptr = foo.empty()?0:&foo.front(); size_t data_size = foo.size(); foo.push_back(15);
__________________
Last edited by JHVH : 10-29-4004 BC at 09:00 PM. Reason: Time for a rest. |
12-15-2004, 07:40 PM | #6 (permalink) | |
Tilted
|
Quote:
Code:
delete[] A; A good reference: http://www.sgi.com/tech/stl |
|
12-20-2004, 01:06 AM | #8 (permalink) |
Psycho
Location: sc
|
i'm also not an expert in c++ but know c pretty well.
i do believe that malloc and alloc are useable. but the point of object orientation is to abstract that stuff out, i'm sure there's something in some library somewhere to cover this. /ducks back into the shadows for not contributing much at all |
12-21-2004, 12:20 PM | #9 (permalink) |
Wehret Den Anfängen!
Location: Ontario, Canada
|
malloc and 'free' exists in C++. You can use 'new' and 'delete' instead, however.
int* x = new int[10] makes an array of 10 ints. delete[] x; deletes the array pointed to by x. new and delete call constructors, while malloc and free do not. This is more important when you are writing real C++ code. std::vector's are what you want for dynamic arrays in C++. Really.
__________________
Last edited by JHVH : 10-29-4004 BC at 09:00 PM. Reason: Time for a rest. |
01-16-2005, 01:18 AM | #10 (permalink) |
Crazy
Location: San Diego, CA
|
C++ is FULLY backward compatible with C, but that doesn't mean it's recommended to actually use C in C++. As Yakk said, you CAN use malloc and free, but if you are writing C++ code, you should as a general rule of thumb use new and delete. new is nicer to use, anyway, since it automagically deals with type issues, the size of the type, and as Yakk said, it calls the constructor if you are making a new object, and the destructor when you delete the object array.
Also I want to re-re-re-re-emphasize the fact that you should not be re-making any objects that already exist in the standard libraries. This is because, in all likelyhood, the standard libraries are implemented better than you could do it, and they can be specifically geared towards the OS and machine that you are compiling for without any changes to your code. Also, you don't have to document them or spend the time making them, and people will know how to read the code
__________________
"Don't believe everything you read on the internet. Except this. Well, including this, I suppose." -- Douglas Adams |
01-17-2005, 07:30 AM | #11 (permalink) | |
Wehret Den Anfängen!
Location: Ontario, Canada
|
Quote:
Valid old-school C, but not C++: void* foo; int* bar; foo = 0; bar = foo; (in C, you can cast a void* into any type, if I remember correctly) Then there are other differences: unprototyped functions, implicit declaration of functions at first use, implicitly typing things as ints, sizeof('a') == sizeof(int), the ',' operator always generates rvalues, const values have external linkage by default, enum differences, etc. Most of these differences are pretty obtuse, and aren't all that worrysome, unless you are porting large chunks of code -- in which case, it can get very annoying! C99 opens another kettle of incompatabilities (including native variable-length arrays), and fixes others incompatabilities.
__________________
Last edited by JHVH : 10-29-4004 BC at 09:00 PM. Reason: Time for a rest. |
|
Tags |
arrays, class, dynamic |
|
|