Const vs Non const method

Namespace via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Mar 7 08:30:48 PST 2016


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++?


More information about the Digitalmars-d-learn mailing list