Presumably not. Here is my use case:
I have two classes A and B.
class A { B *b_; }; class B { public: B(): b_string_("") {}; private: std::string b_string_; };
I want b_ to always point to object B. I want b_ to point to the "empty" object B instead of nullptr so that I can always dereference * b_ in a certain way to get an empty b_-> b_string. Therefore, I thought I would create a global "object of zero B": const B null_b . But I cannot (naturally) use this A ctor:
A(): b_(&null_b) {};
since b_ cannot point to a constant variable. If not "null object B", b_ must point to mutable objects B.
Creating a global non-const solves the problem, but I want const protection, so I can guarantee that the "null object" never changes.
FWIW, this includes a large project where b_ points to a vector of B objects in another class. I could add an empty object B to this vector, but it amazes me like kludgy.
Is there a way or template to solve my problem?
source share