class Texture
{
public:
Texture(int width, int height);
Texture();
Colour* texture;
};
Texture::Texture(int width, int height)
{
Colour colourArray[width][height];
texture = colourArray;
}
class Texture
{
public:
Texture(int width, int height);
Texture();
Colour* texture;
};
Texture::Texture(int width, int height)
{
Colour colourArray[width][height];
texture = colourArray;
}
Texture::Texture(int width, int height){ Colour colourArray[width][height]; texture = colourArray;}
You didn't post the errors, but I'm guessing they refer to illegal array declarations and/or incompatible types. This would be closer:Colour colourArray[width * height];texture = colourArray; // Array decays to pointer gracefully
Except that you can't declare variable-length built-in arrays in C++, and in any case it wouldn't help you because the pointer would become invalid as soon as the constructor completed execution.Texture::Texture(int width, int height) : texture(new Colour[width * height]){}
You will then need a destructor with a matching invocation of delete [].Quote:Original post by leet bixIn theory, you'll have undefined behaviour ;)
I'm assuming that if I just say
delete texture;
I will have a memory leak?
Texture::Texture(int width, int height) : texture(new Colour[width * height]){}Texture::Texture(int width, int height){ texture = new Colour[width * height];}
class Texture{public: Texture() : texture(NULL) {} Texture(int width, int height) : : texture(new Colour[width * height]) {} ~Texture() { delete [] texture; }private: Texture( const Texture& );//not implemented Texture& operator=( const Texture& );//not implemented Colour* texture;};
class Texture { typedef boost::multi_array<Colour, 2> Data; typedef boost::shared_ptr<Data> Handle; Handle texture; Texture(Handle handle): texture(handle) {} public: Texture(int width, int height): texture(new Data(boost::extents[width][height])) {} Colour& get(int x, int y) const { return (*Handle)[x][y]; } void set(int x, int y, const Colour& c) { (*Handle)[x][y] = c; } Texture copy() { return Texture(new Data(*Handle)); }}
Quote:Thank you for the clarification. I'm going to go with Hodgman's method because I don't like using boost.Note that in practice you'd probably be better off using std::vector, e.g. (not compiled or tested):
class Texture{ std::vector<Colour> data;public: Texture(int width, int height) : data(width * height) {}};
This will obviate the need for a user-defined destructor and for worrying about the 'Rule of Three'. (Depending on what sort of semantics you want for your class, you could still disable copying as Hodgman demonstrated, or make the data shared as in Zahlman's example.)Quote:Original post by leet bixFair enough :) Just keep in mind that a lot of Boost is 'practically' standard, and that some parts of it (e.g. shared_ptr) actually are part of the standard library now (although they may or may not be available as part of your implementation, depending on what compiler you're using).
I have nothing against boost, it seems to be used very widley, heck, if I had a dollar for everytime i saw shared_ptr on this forum I'd be rich! I'm just trying to use only the standard libraries to complete my entire project.
Quote:Original post by leet bix
I have nothing against boost, it seems to be used very widley, heck, if I had a dollar for everytime i saw shared_ptr on this forum I'd be rich! I'm just trying to use only the standard libraries to complete my entire project.