// globally accessible variable. can be changed at runtime.
bool feature_flag = true
Class A
{
UtilityClass obj_util;
A(int x,int y,int z)
{
}
}
Class UtilityClass
{
UtilityClass()
{
}
}
Main()
{
A a(10,20,30);
}
When object to A is created, it instantiates UtilityClass object inside A and so calls UtilityClass() constructor.
My new scenario and problem:
when feature_flag is activated (true), some components will not be available in UtilityClass (don't ask me how), so UtilityClass() constructor call will fail.
To avoid this failure,
1) I want obj_util member to be removed when feature_flag = true
like
Class A
{
if(feature_flag == false)
UtilityClass obj_util;
A(int x,int y,int z)
{
}
}
(or)
2) I don't want UtilityClass() constructor to be invoked when creating object to A. I also don't want to convert UtilityClass in Class A to UtilityClass* to avoid calling constructor, as there is a huge code associated with this object in Class A and needs change in lot of components inside. But I am pretty sure that none of that code path will be hit when feature_flag = true.
I want both scenarios coexisting just with the deciding condition of one flag (feature_flag). Is it even possible in c++?
I heard something about std::conditional but did not understand whether I can apply it in the following scenario.
For example, the following did not work
// globally accessible variable. can be changed at runtime.
bool feature_flag = true
Class A
{
std::conditional <feature_flag, UtilityClass, DummyClass> obj_util;
A(int x,int y,int z)
{
}
}
Also, I tried to fit std::conditional in .h file because all class definitions are present in .h files in my project, and it also doesn't seem to work.
Please note that I cannot create any dummy constructor in UtilityClass or for that matter cannot change code for UtilityClass.
Any hints or suggestions are appreciated. Thanks