Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 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.
j0hnb is offline  
Old 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
welshbyte is offline  
Old 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.
Rekna is offline  
Old 12-07-2004, 04:33 PM   #4 (permalink)
Tilted
 
Location: Orlando
Why are you doing this? If you just need a dynamic string STL strings are pretty nice and I believe they are now part of ANSI C++
gariig is offline  
Old 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.
Yakk is offline  
Old 12-15-2004, 07:40 PM   #6 (permalink)
Tilted
 
Quote:
Originally Posted by Rekna
Code:
Class DynamicArray
{
   public: 
         DynamicArray(int size);
         ~DynamicArray();
   private:
         char *A;
         int N;
 };

DynamicArray::DynamicArray(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.
The only small change to this implementation is that technically, it should be:
Code:
delete[] A;
Also - assuming you are willing to get into STL and use templates, I would recommend it. They have variable length strings, vectors, lists, iterators, etc. It does require a bit of understanding of the theory of C++ and object oriented programming, though.

A good reference:
http://www.sgi.com/tech/stl
ergdork is offline  
Old 12-20-2004, 12:22 AM   #7 (permalink)
Crazy
 
I have a small question kind of off topic...

In c++ do you not create dynamic space using malloc and realloc? What are their substitutes in c++? I just recently learned the basics of C, and have never looked at C++.
FloydianOne is offline  
Old 12-20-2004, 01:06 AM   #8 (permalink)
Psycho
 
noodles's Avatar
 
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
noodles is offline  
Old 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.
Yakk is offline  
Old 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
Rangsk is offline  
Old 01-17-2005, 07:30 AM   #11 (permalink)
Wehret Den Anfängen!
 
Location: Ontario, Canada
Quote:
Originally Posted by Rangsk
C++ is FULLY backward compatible with C, but that doesn't mean it's recommended to actually use C in C++.
Pedantic Note: C++ isn't quite FULLY backward compatible with C.

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.
Yakk is offline  
 

Tags
arrays, class, dynamic


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 12:05 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76