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 09:40 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360