Inherit from class const

I would like to inherit from the class using the const specifier as follows:

 class Property { int get() const; void set(int a); }; class ConstChild : public const Property { // Can never call A::set() with this class, even if // the instantiation of this class is not const }; class NonConstChild : public Property { // Can call both A::set() and A::get() depending on // the constness of instantiation of this class }; 

My compiler obviously gives me an error for the const keyword in the second class declaration. Ideally, I would like to avoid creating a new ReadOnlyProperty class from which ConstChild would inherit.

Can I somehow use the const keyword for inheritance?

  • If not, do you have any other ideas to solve this problem?
+8
source share
8 answers

Add variability later in your inheritance tree and get the corresponding value:

 class Property { int get() const; }; class MutableProperty : public Property { { void set(int a); }; 

And then:

 class ConstChild : public Property { ... }; class MutableChild : public MutableProperty { ... }; 
+6
source

I had a need for a related problem, which is: to really control / allocate mutable and const access for some class. I did this with this simple reusable template wrapper:

 template <typename T> class TAccessor : private T { public: const T& Const() const { return *this; } T& Mutable() { return *this; } }; // Example of use: template <typename T> using MyVector = TAccessor<std::vector<T>>; void main() { MyVector<int> vector; vector.Mutable().push_back(10); int x = vector.Const()[1]; ... } 
+3
source

I would like to inherit from the class using the const specifier

Whatever you want, it does not matter. You can not. This is not valid C ++.

Is there any way to use the const keyword for inheritance? "

Not.

+1
source

If you create a const set member function, you will get what you need.

 class Property { int get() const; void set(int a); }; class ConstChild : public Property { void set(int a) const {} }; 

The only caveat is that a cunning user can get around your intent using:

 ConstChild child; child.set(10); // Not OK by the compiler Property& base = child; base.set(10); // OK by the compiler 
0
source

Use a data element or private base class instead of a public base class.

Then you control access to this item.

You can make the Property property an abstract interface if you need polymorphic behavior.

0
source

You can use the template class and specialization for the constant type:

  template<typename T> class base_property { protected: T value; }; template<typename T> class property : public base_property<T> { public: const T& get()const { return value; } void set(const T& v){ value = v; } }; template<typename T> class property<const T> : public base_property<T> { public: const T& get()const { return value; } }; class ConstChild : public property<const int>{ }; 
0
source

I had the same need, and I ended up with this rather manual approach:

 class A { public: void fooMutable(void) {} void fooConst(void) const {} }; class B : private A { public: using A::fooConst; const A& getParent(void) const { return *this; } }; void fooParent(const A&) {} int main(void) { auto b = B{}; b.fooConst(); // Ok b.fooMutable(); // Fail fooParent(b); // Fail fooParent(b.getParent()); // Ok return 0; } 

Note that the using keyword will not work with const / mutable overloads:

 class A { public: void foo(void) {} void foo(void) const {} }; class B : private A { public: using A::foo; // Expose the const and the mutable version }; 

To solve this problem, you can independently override the function and call the parent:

 class B : private A { public: void foo(void) const { A::foo(); } }; 

This can take quite a while if you inherit a large hierarchy, but if it is for a small class, it should be very reasonable and quite natural for the user.

0
source

I have a trick, not a clean solution.

 class ConstChild : private Property { operator const Property () { return *this; } }; 

then

 ConstChild cc; cc.set(10); // ERROR cc().get(); 
-one
source

All Articles