Let's assume I have a class which manages some resource in RAII way:
class C
{
HANDLE hResource_;
// prevent sharing the ownership over the resource among multiple instances of C
C(const C&);
C& operator=(const C&);
public:
C() : hResource_(INVALID_HANDLE){}
C(int arg1, const std::string& arg2,...)
{
...
allocResource(arg1, arg2, ...);
...
}
~C
{
...
FreeResource(hResource_);
hResource_ = INVALID_HANDLE;
...
}
void allocResource(int arg1, const std::string& arg2, ...)
{
if(hResource_ == INVALID_HANDLE)
{
hResource_ = AllocateResource(arg1, arg2,...);
}
}
HANDLE handle() {return hResource_;}
};
Its constructor takes some parameters needed for resource allocation and I am able to create an instance of it, use it and let it live within some scope:
// some global function
void goo()
{
C c(123, "test");
UseResource(c.handle(),...);
...
}
Let's say I want now an instance of C
to be a member of some class, and want to delay allocation of the resource that happens in C
's c-tor. This requires C
's default c-tor and some C
's member function which performs resource allocation (e.g. allocResource()
which calls AllocateResource()
).
class A
{
C c_;
public:
void foo1()
{
...
c_.allocResource(123, "test");
UseResource(c_.handle(),...);
...
}
void foo2()
{
...
UseResource(c_.handle(),...);
...
}
};
By using dedicated function, we are exposing C
's internals in some way which I don't like.
My question is: Is this approach a common way to enable lazy initialization? Are there any alternatives?
EDIT: This is a possible class design regarding (MSalters') suggestions below:
class C
{
HANDLE hResource_;
// prevent sharing the ownership over the resource
// among multiple instances of C
C(const C&);
C& operator=(const C&);
public:
// prevent object creation if resource cannot be acquired
C(int arg1, const std::string& arg2,...)
{
hResource_ = AllocateResource(arg1, arg2,...);
// assumption: AllocateResource() returns
// INVALID_HANDLE in case of failure
if(hResource_ == INVALID_HANDLE)
throw resource_acquisition_exception();
}
~C
{
...
FreeResource(hResource_);
hResource_ = INVALID_HANDLE;
...
}
HANDLE handle() {return hResource_;}
};
class A
{
std::unique_ptr<C> c_;
public:
void foo1()
{
try
{
...
c_ = std::unique_ptr<C>(new C(123, "test"));
UseResource(c_->handle(),...);
...
}
catch(const resource_acquisition_exception& exc)
{
...
}
catch(...)
{
...
}
}
void foo2()
{
...
UseResource(c_->handle(),...);
...
}
};
No, this is not a common way to do RAII. In fact, it's not RAII at all. If you can't allocate the necessary resources for a C
, don't create a C
.