As far as I understand the RAII idiom when applied to resources needed by a class (and please correct me if I'm wrong), a class that requires a resource should define a member of the appropriate type, and its destructor will be called automatically when the using class instance is destroyed, like this:
class Skybox
{
public:
Skybox() : tex_(...) {}
private:
Texture tex_;
};
Apart from using a smart pointer to allocate the resource on the heap, how can this pattern be applied if the resource member requires some code to be executed in the Skybox
constructor, before the initialisation of the resource? For example:
class Skybox
{
public:
Skybox(const std::string& fileName);
private:
Texture tex_;
}
Skybox::Skybox(const std::string& fileName)
{
// read stuff from skybox initialization file
// including various texture parameters such as texture file
...
// initialize tex_ based on information read above
}
Update: the Texture
class requires all initialization to be performed in its constructor (i.e. no Texture::Init()
method is available)
Wrap the initialization code into a function, and use that function (member or non-member, static or non-static, as appropriate) to initialize the member variable:
Texture Skybox::init_tex(std::string const& fileName) {
// read stuff from file, including textureFile
// initialize result
return Texture(...);
}
Skybox::Skybox(std::string const& fileName):
tex_(init_tex(fileName))
{ }
The initialization function should probably be a static function. If it isn't, be careful not to use any members that haven't been initialized yet — you're calling init_tex
on a not-yet-fully initialized Skybox
instance.