🔒 Locked
A simple array declaration problem
Advertisement
Original Post
Without getting into too much detail about what i'm doing, since it will only complicate things, i'll show you how i tried to solve the problem and explain from there: Class CBoard { CBoard(int iSize); const int iBoardSize; COtherClass* pOtherClass[iBoardSize]; } CBoard::CBoard(int iSize) { iBoardSize = iSize; } This is just pseudo code but you can see i'm trying to initialize the size of the array based on the class constructor i'm not even sure this is possible i'd like to know if it is and if so how it's done. Any help is greatly appreciated. Mike!
If you want to declare an array in a class you have to use a literal, not a variable:
However if you want the constructor to determine the size of the array then dynamically allocate it:
Hope that helps,
Dave
COtherClass* pOtherClass[10];notCOtherClass* pOtherClass[iBoardSize];However if you want the constructor to determine the size of the array then dynamically allocate it:
COtherClass* pOtherClass;In C'tor:pOtherClass= new COtherClass[ someSizeVariable or literal ];Hope that helps,
Dave
header file
CBoardPiece *pBoardPiece;
cpp File
CBoard::CBoard(LPDIRECT3DDEVICE8 pD3DDevice, int iSize)
{
CBoardPiece* pBoardStoreage[iSize];
for (int h = 0; h < iSize; h++)
{
pBoardStoreage[h] = new CBoardPiece(pD3DDevice);
}
pBoardPiece = pBoardStoreage;
}
I tried to implement it the way you said, as above, according to the syntax i know but still have the problem with iSize being a variable, any more ideas?
Mike!
class Board{ public: Board(unsigned int size); private: std::vector< OtherClass * > other_class;}Board::Board(unsigned int size) : other_class(size){}- Manual memory management is error-prone and rarely more efficient than the standard container classes.
- Systems Hungarian notation has no redeeming qualities in a type-safe language like C++, as argued in one of my favourite articles. Also see the parameters of the WndProc to see the negatives in action (hint: compare the wart and the type of WPARAM).
- Since the array can never have a negative size it's safer to use an unsigned type.
Σnigma
I've read that dynamic allocation like this can be very costly, so do you think it would be better to store and the maximum size array of the class but then only initialise the ones i need i.e.
CBoardPiece* pBoardPiece[MAXAMMOUNT]
Construtor::Constructor(unsigned int iSize)
{
for (int h = 0; h < iSize; h++)
{
pBoardPiece[h] = new CBoardPiece();
}
}
Do you think this would this be more or less efficient?
Considering the CBoardPiece class is a single triangle fan with its relevant methods.
CBoardPiece* pBoardPiece[MAXAMMOUNT]
Construtor::Constructor(unsigned int iSize)
{
for (int h = 0; h < iSize; h++)
{
pBoardPiece[h] = new CBoardPiece();
}
}
Do you think this would this be more or less efficient?
Considering the CBoardPiece class is a single triangle fan with its relevant methods.
Don't optimize prematurely. Unless you're doing this thousands of times a second you don't need to worry about it yet. Implement your program with the simplest solution possible. If when finished it doesn't run fast enough then find out what is slowing you down using a profiler and optimize those parts.
Make it run, then make it fast.
Σnigma
Make it run, then make it fast.
Σnigma
Quote:
Original post by Marx2052
I've read that dynamic allocation like this can be very costly,
Did you try it to see? Did you question what "very costly" meant in context?
Did it occur to you that in C++, the exact sizeof() types *must* be known *at compile time*?
Quote:
so do you think it would be better to store and the maximum size array of the class but then only initialise the ones i need i.e.
CBoardPiece* pBoardPiece[MAXAMMOUNT]
If you can *prove* that MAXAMOUNT is always enough AND there is a reason *imposed by the problem domain* that MAXAMOUNT makes sense as a logical upper bound AND the *minimum* space you will use is not significantly less than MAXAMOUNT, then yes. But even then, it's a question of space and of code complexity, rather than speed.
Quote:
Considering the CBoardPiece class is a single triangle fan with its relevant methods.
The amount of time you spend dynamically allocating a chunk of memory for the board will be orders of magnitude less than the time spent to draw a triangle fan even once. Hell, it will probably be far less than the time spent drawing a single triangle. You are likely to create the board once at the beginning of the game, and draw every piece every frame. Where do you think you should be looking for optimizations?
That said, you may consider whether you really need to store a container of pointers, and dynamically allocate each piece. That does add extra code complexity, and could slow things down slightly (due to extra indirection and cache coherency issues). But the devil is in the details.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement