Jump to content
  • Advertisement
Sign in to follow this  
absolute

Noob array question

This topic is 3742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I have a class that has an array member, is there any way to declare the size of the array through the class constructor? Or am I stuck using vector? Thanks. header
#ifndef TEXT_H
#define TEXT_H

class Text
{
public:
  Text (int size);
private:
  int blah[]; // size of the array passed through constructor
};
#endif


cpp

#include "text.h"

Text::Text(int size)
{
}

Share this post


Link to post
Share on other sites
Advertisement
yes

but btw, your array needs to be "int* blah", then in the constructor, put "blah = new int[size_here]"

Edit:

So your source would look like this:

Header

#ifndef TEXT_H
#define TEXT_H

class Text
{
public:
Text (int size);
private:
int* blah; // size of the array passed through constructor
};
#endif




Source

#include "text.h"

Text::Text(int size)
{
blah = new int[size];
}

Share this post


Link to post
Share on other sites
You have to use a vector.

If you could define the size of the array in the constructor that would go against the requirement that all instances of a class have the same size.

Oh, and please don't tell me you're writing a string class. Because there already is one, its called std::string, std::wstring

Share this post


Link to post
Share on other sites

class Text
{
private:
int* blah;
public:
Text(int size)
{
blah = new int[size];
}

~Text()
{
delete [] blah;
}
};




I haven't compiled this to try it, but it looks right. Also, if the name of the class is anything to go by, you want to store an array of characters... and I'd use an array of chars instead of an array of integers, mostly because it just makes more sense.

@fpsgamer: I think it works here because you're storing a pointer to an array? I'm doing pretty much the same thing in my own code and it works fine.

Share this post


Link to post
Share on other sites
Quote:
Original post by Twisol
@fpsgamer: I think it works here because you're storing a pointer to an array? I'm doing pretty much the same thing in my own code and it works fine.


Ya, storing a pointer and using a dynamic allocation works too. But std::vector is "nicer". As I said before if the user intends to represent text then std::string would be best.

Share this post


Link to post
Share on other sites
use vector...

arrays are messy, and are not able to be dynamically resized without full recreation of the array.

Vectors are in the standard libraries, work better and are cleaner to use in code.

Share this post


Link to post
Share on other sites
Quote:
Original post by lordcorm
yes

but btw, your array needs to be "int* blah", then in the constructor, put "blah = new int[size_here]"

Edit:

So your source would look like this:

Header
*** Source Snippet Removed ***

Source
*** Source Snippet Removed ***



ah ok, thanks :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Kalten
arrays are messy, and are not able to be dynamically resized without full recreation of the array.


False. You can malloc more memory on to the end of an array.

Edit: Sorry, its realloc().

Share this post


Link to post
Share on other sites
Quote:
Original post by lordcorm
Quote:
Original post by Kalten
arrays are messy, and are not able to be dynamically resized without full recreation of the array.


False. You can malloc more memory on to the end of an array.

Edit: Sorry, its realloc().


If realloc cannot acquire n contiguous bytes past the end of your current m byte allocation, it simply creates a new buffer of m + n bytes, copies the data, and frees the original allocation.

In C++ we we prefer not to use C memory allocators since they are incompatible with non-pod types. So this allocator should be avoided.

You should note however that the C++ does not have an equivalent to realloc. Bjarne Stroustrup explains what to do instead here (hint: it has something to do with std::vector).

Share this post


Link to post
Share on other sites
Are vectors generally better? The reason I am considering array now is because I'm doing A* pathfinding and I have it implemented now as vectors holding the information. The problem is that it is quite slow and I thought if I switched over to arrays instead of vectors it would improve the calculation time.

Or is the difference between vector and array for computation speed not different? Since this question is probably heavily affected by the circumstance I am using them under a quick description. I have about 5 vectors as of right now holding, x/y positions, costs, etc ( all ints). I have to use push_back each time I add a new step to the path I'm finding ( can get quite large, in the hundreds at least). So if I went with arrays I would avoid the push_backs in exchange for some memory space. Would this change make a big difference speed wise or should I look elsewhere for possible bottle necks? Thanks.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!