S
Stefano Sabatini
Hi to all C++ posters,
this is my first post here so please be clement.
I want to define inside an object an array with a size which is
determined by the constructor. Here it is the code:
#include <iostream>
using namespace std;
class BufferContainer {
public:
/**
* this buffer is initialized at creation
*/
int buffer[];
/**
* the size of the buffer the object will contain
*/
int bufferSize;
/**
* constructor
*
* @param _bufferSize the size of the buffer to be contained in the
* object
*/
BufferContainer(int _bufferSize);
/**
* destructor
*/
~BufferContainer();
};
BufferContainer::BufferContainer (int _bufferSize) {
bufferSize = _bufferSize;
buffer = new int[bufferSize];
}
BufferContainer::~BufferContainer() {
delete buffer;
}
#if 1
int main(void) {
BufferContainer *b1 = new BufferContainer(128);
exit(0);
}
#endif
I know that the following syntax:
buffer = new int[bufferSize];
is wrong and so I'm not surprised it raises a compilation error. So
which is the correct way to express it?
And which should be the right approach to the problem (maybe should I
use some standard class instead of a raw array)?
Greeter.sayThank(1000);
Greeter.greets();
this is my first post here so please be clement.
I want to define inside an object an array with a size which is
determined by the constructor. Here it is the code:
#include <iostream>
using namespace std;
class BufferContainer {
public:
/**
* this buffer is initialized at creation
*/
int buffer[];
/**
* the size of the buffer the object will contain
*/
int bufferSize;
/**
* constructor
*
* @param _bufferSize the size of the buffer to be contained in the
* object
*/
BufferContainer(int _bufferSize);
/**
* destructor
*/
~BufferContainer();
};
BufferContainer::BufferContainer (int _bufferSize) {
bufferSize = _bufferSize;
buffer = new int[bufferSize];
}
BufferContainer::~BufferContainer() {
delete buffer;
}
#if 1
int main(void) {
BufferContainer *b1 = new BufferContainer(128);
exit(0);
}
#endif
I know that the following syntax:
buffer = new int[bufferSize];
is wrong and so I'm not surprised it raises a compilation error. So
which is the correct way to express it?
And which should be the right approach to the problem (maybe should I
use some standard class instead of a raw array)?
Greeter.sayThank(1000);
Greeter.greets();