Is there a way to forbid casting to subslass that is non-const in C++? 
Here is a complete example. 
I want to forbid using A::set from objects casted from B to A by allowing only casting 
B to const A.
How to do it?
(I can't use virtual functions)


    #include <iostream>
    #include <cassert>
    
    using namespace std;
    
    class A {
    public:
      int  get() const { return i_; }
      void set(int i) { i_ = i; }
    protected:
      int i_;
    };
    
    class B : public A {
    public:
      int  ok() const { return A::get() == copy_i_; }
      void set(int i) { A::set(i); copy_i_ = i; }
    protected:
      int copy_i_;
    };
    
    void test2() {
      B b;
      b.set(5);
      A& a = b;
      assert(b.ok());
      a.set(3);
      assert(b.ok()); // fail-here
    }
    
    int main() {
      test2();
      return 0;
    }

Is there a way to forbid casting to subslass that is non-const in C++? 
===

Here is a complete example. 
I want to forbid using A::set from objects casted from B to A by allowing only casting 
B to const A.
How to do it?
(I can't use virtual functions)


    #include <iostream>
    #include <cassert>
    
    using namespace std;
    
    class A {
    public:
      int  get() const { return i_; }
      void set(int i) { i_ = i; }
    protected:
      int i_;
    };
    
    class B : public A {
    public:
      int  ok() const { return A::get() == copy_i_; }
      void set(int i) { A::set(i); copy_i_ = i; }
    protected:
      int copy_i_;
    };
    
    void test2() {
      B b;
      b.set(5);
      A& a = b;
      assert(b.ok());
      a.set(3);
      assert(b.ok()); // fail-here
    }
    
    int main() {
      test2();
      return 0;
    }
