Dynamic array VS class

Started by
9 comments, last by RiBa 20 years, 10 months ago
Hi, Currently i''m crawling my way trough c++ and i''ve come to my first problem, i can''t seem to solve by myself. Problem: I have a class with this layout: Myclass.init(...) Myclass.dostuff(...) Myclass.destroy(...) Now what i want is to be able to create and fill a dynamic array in .init(), use it in .dostuff() and destroy it in .destroy(), without calling .dostuff() or .destroy() from .init() (i call them from my mainloop). So can someone help me with this? I didn''t found a good solution on the inet by myself. Thanks.
Advertisement
What is the problem? Call init, dostuff, and then destroy in the main loop... what isn''t working?
An ASCII flowchart may help us understand what your talking about ...

From what I understand, you have your mainloop and you want to create a dynamic array of that class; then call the init, dostuff, and destroy function in each of those classes. Is that right?


Rob Loach
Current Project: Go Through Object-Oriented Programming in C++ by Robert Lafore

"Do or do not. There is no try."
- Yoda
Rob Loach [Website] [Projects] [Contact]
One of the things that needs clearing up here is do you want to create a dynamic array of Myclass or do you actually want Myclass to have as one of it''s members a dynmanic array of something and have Myclass methods work on that array?

--{You fight like a dairy farmer!}

class Myclass{

public:

...

private:
int *array;
};


Myclass::init(){
array = new int[10];
...
}

Myclass::delete(){
...
delete[] array;
}

[edited by - kdogg on June 3, 2003 9:57:14 PM]
The only problem I see here is that you should have a constructor instead of your init method and destructor instead of your delete method.

You didn''t state your problem and there are no syntax errors. So what is wrong?


First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Ops, sorry guys, my fault.

This is turning out to be more like a directx related problem.

I need a dynamic array of vertexbuffers actually, so when i try kdogg''s example, it works, but when i replace the int with IDirect3DVertexBuffer8, i get this error:

error C2259: ''IDirect3DVertexBuffer8'' : cannot instantiate abstract class due to following members:
C:\DXSDK\include\d3d8.h(933) : see declaration of ''IDirect3DVertexBuffer8''

THIS is my problem :-)
Read a tutorial about DX first, then come back
But a quick solution is:

MyClass {   // ...   typedef std::vector<IDirect3DVertexBuffer8*> buffer_array;   buffer_array vertexBuffers;   size_t bufferCount;};bool MyClass::init(){   for (int n = 0; n < MAX_VERTEX_BUFFERS; ++n) {        IDirect3DVertexBuffer8* buffer;        if (SUCCEEDED(Device->CreateVertexBuffer( &buffer/* lots of params here */ )) {        vertexBuffers.push_back(buffer);        ++bufferCount;        } else {           break;        }   }   return (bufferCount == MAX_VERTEX_BUFFERS);}void MyClass::destroy(){   buffer_array::iterator cur = vertexBuffers.begin();   buffer_array::iterator end = vertexBuffers.end();   for(; cur != end; ++cur) (*cur)->Release();   vertexBuffers.clear();}
The reason it doesn''t work with IDirect3DVertexBuffer8 is because of the way COM is implemented in C++.

IDirect3DVertexBuffer8 is actually a C++ class with all the methods that the COM interface exposes. However as the class doesn''t actually implement the methods (the COMponent that supports the interface does) all these C++ methods are Pure virtual (ie have =0; after each declaration).

So you can''t just create instances of this interface, eg:

IDirect3DVertexBuffer8 buff;

will not compile because the methods are pure virtual - ie it''s an abstract class - hense the compiler error "cannot instantiate abstract class.."

When you get a IDirect3DVertexBuffer8 back from CreateVertexBuffer(..) - you''re actually getting a pointer to the interface. You can have pointers to abstract classes - so this works.

So,
IDirect3DVertexBuffer8* buff;

will complile - but obviously buff is uninitialised - ie points to rubbish. You need to call CreateVertexBuffer(..) to get one which actually points to the interface on a valid component.

Hope this helps..



IDirect3DVertexBuffer8 is an interface , not a class, so You can't create an instance of it directly. You have to use another D3D interface (IDirect3DDevice8?) to create a vertex buffer object. I don't really know D3D, so I couldn't tell you exactly how.

What you need to do is to create a dynamic array of pointers to IDirect3DVertexBuffer8, and create each vertex buffer individually. Something like:

class Myclass{    ...private:    IDirect3DVertexBuffer8 **buffers;};void Myclass::init(){    // Get your IDirect3DDevice8 pointer from somewhere    IDirect3DDevice8 *pDevice = ...;    ...    buffers = new IDirect3DVertexBuffer8*[42];    ...    pDevice->CreateVertexBuffer(..., &buffers[0], ...);    pDevice->CreateVertexBuffer(..., &buffers[1], ...);    ...    pDevice->CreateVertexBuffer(..., &buffers[41], ...);}void Myclass::destroy(){    for(int i=0; i < number_of_vertex_buffers; ++i)    {        buffers[i]->Release();    }}


[edit] beaten twice, by people who know more about DX than me...oh well...

[edited by - sbennett on June 4, 2003 11:23:36 AM]

This topic is closed to new replies.

Advertisement