Skip to main content
GameDev.net gamedev.net
🔒 Locked

pointer fun!

Started by Nullio Oct 17, 2003 at 3:28 AM 6 replies 1.1k views
Original Post
Nullio
Nullio
I have a funtion that requires an array of pointers as an argument. if I knew the size of the array at compile time I could use the following code:
MyClass *pointerArray[100];
      
but becuase I do note know the size of the pointer array until run time I'm forced to use the new operator to create the array at runtime. This is what I've tryed:
MyClass **pointerArray;
int val;

val = someValue();
pointerArray = new MyClass[val];
  
and I've been getting nothing but errors, I'm sure there is a simple way of doing this and I'll feel like a moron when I hear it but could someone please help? edit: code tag fixup [edited by - Nullio on October 17, 2003 4:32:59 AM]
CProgrammer
CProgrammer
I''d say this line has to be:
pointerArray = new *MyClass[val];

Notice the star because your allocating an array of pointers not MyClass objects.

-CProgrammer

My Website
Palidine
Palidine
er... that''d be:

new MyClass*[num];

asterix on the right side not the left. asterix on the left is a dereference call.

-me
Nullio
Nullio
ok I did that now I get a really wierd error:


error C2440: ''initializing'' : cannot convert from ''int (__cdecl *)(const MyClass*)'' to ''size_t''


I''m accually using a const if it maters, so the code looks like:


const MyClass **pointerArray;
int val;
val = someValue();
pointerArray = new const MyClass*[val];


don''t think it makes a difference. I have no idea what ''size_t'' is it not part of MyClass possibly something that ''new'' uses?
Nullio
Nullio
oh whoops, I found it. nevermind
CProgrammer
CProgrammer
quote:
Original post by Palidine
er... that''d be:

new MyClass*[num];

asterix on the right side not the left. asterix on the left is a dereference call.

-me


Oh wups you''re right ofcourse.
Sorry bout that.
-CProgrammer

Sirp
Sirp
I suggest you use the C++ vector container.


#include <vector>
.
.
.
std::vector pointerArray(val);

//to add a new pointer
pointerArray.push_back(new MyClass());


Much much easier than that pointer-to-pointer magic.

-Sirp.
Battle For Wesnoth: A Free fantasy turn-based strategy game - http://www.wesnoth.org
dmikesell
dmikesell

#include <boost/shared_ptr.hpp>
#include <vector>
.
.
using namespace boost;
using namespace std;
.
.
vector<shared_ptr<MyClass> > MyClassVector;
.
.
shared_ptr<MyClass> foo(new MyClass());
MyClassVector.push_back(foo);


--
Dave Mikesell Software & Consulting

[edited by - dmikesell on October 17, 2003 5:37:31 AM]

[edited by - dmikesell on October 17, 2003 5:38:09 AM]

[edited by - dmikesell on October 17, 2003 5:38:25 AM]

[edited by - dmikesell on October 17, 2003 7:37:45 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.