Let's use an example:

----
import std.stdio;

class Visitor {
public:
    void visit(inout A) {
        writeln("visit A");
    }

    void visit(inout B) {
        writeln("visit B");
    }
}

class A {
public:
    void accept(Visitor v) inout {
        v.visit(this);
    }
}

class B : A {
public:
    override void accept(Visitor v) inout {
        v.visit(this);
    }
}
----

This piece of code works for both versions below:

#1:
----
void main() {
    A a = new A();
    A b = new B();

    Visitor v = new Visitor();

    a.accept(v);
    b.accept(v);
}
----

#2:
----
void main() {
    const A a = new A();
    const A b = new B();

    Visitor v = new Visitor();

    a.accept(v);
    b.accept(v);
}
----

Thanks to the wildcard modifier inout. Is there any possible way to do the same in C++?

Reply via email to