Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / C++ / C# - I need help on Templates, Classes and Dynamic Arrays

Author
Message
Clonkex
Forum Vice President
16
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 25th Jan 2012 05:55
Hi all,

I am in the process of rewriting Xarshi's Havok wrapper and I am using my own array code. I just want to know if this will do what I want it to do or if not, what I need to change.



I only started with classes and templates this morning, so I don't know that much yet. The code doesn't get any compiler errors, but that's nothing to go on.

I am defining the array that I will be passing in like this:



Shape is a class, as is RigidBody. This is one of the functions that uses the Shape class:



I'm pretty sure that's the function. I forgot to copy it before coming to this computer so I had to write it out from memory. The problem is that when I try to compile it generates an error, saying it can't convert Shape to Shape* (for the return value). I don't understand why it's doing this as the shapelist array is defined as Shape*. I can make it compile by putting a & in front of the shapelist array when it returns it like so...



...and by changing the -> in both the for-loop and the if-statement to a dot (full stop, period, etc.).

So will it work if I just leave it with these changes or are they just cheating the compiler into compiling?

Thanks,
Clonkex

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 25th Jan 2012 14:23 Edited at: 25th Jan 2012 14:33
That array code will not work. There is no way to get the size of a dynamically allocated array, and static arrays cannot be resized. You can get the size of static arrays like so:



The only way to implement it would be to store both the array and its size together in a class. There would be quite a lot of code involved to write this, but luckily it has already been done for you. C++ has its own standard libraries, one of which is the Standard Template Library (STL). In the STL there is a "std::vector<T>" class which is the closest thing to a DBPro array there is.

That contains methods to get the size of the vector, access elements at a particular index, add/remove elements, etc. If you look in the top right corner of that page, you can see it has the text "<vector>". This tells you the header file you need to include to use the class template, so in this case you would need to write:

Before you can use it in your code. Notice it uses angled brackets <> instead of double quotes "". This is not strictly necessary but makes the code more readable. Angled brackets are used for include files which are part of the standard library, as opposed to ones you've written yourself. Your code will compile faster too as the compiler can find the file more quickly.

edit:
Also, an explanation for why you were getting the shape error. The problem was that you were making an array of "Shape" when you wanted an array of "Shape*". In C++, dynamic arrays are just pointers, so when you wrote this:

It is equivalent to this:


What you wanted is this:


That way when you access an element in "shapes" it will be of type "Shape*"

The [] operator when used on pointers/arrays such as "shapes[n]" is equivalent to "*(shapes+n)":

[b]
Clonkex
Forum Vice President
16
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 27th Jan 2012 04:00
Thanks! That really helps. I should just get stuck in and learn how to use C++ properly. At least I've got you guys (esp. you and IanM!) to help me!

Thanks again,
Clonkex

Login to post a reply

Server time is: 2026-06-11 09:55:44
Your offset time is: 2026-06-11 09:55:44