www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - casting & templates

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
I have:

class OperatorV(T) : Value {
  T impl;

  this(T impl) {
    this.impl = impl;
  }
...

and use it like this: makeOperator((IntV a, IntV b) => new IntV(a.num + 
b.num));

Now I want to do: opWord.get() returns a Value

OperatorV *op = cast(OperatorV*)(opWord.get());

and get: Error: class app.OperatorV(T) is used as a type

Hmm... any idea? Don't know how to carry the template stuff around.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
Jan 31
parent Chris Wright <dhasenan gmail.com> writes:
On Sun, 31 Jan 2016 19:59:01 +0100, Robert M. Münch wrote:

 I have:
 
 class OperatorV(T) : Value {
   T impl;
 
   this(T impl) {
     this.impl = impl;
   }
 ...
This expands to: template OperatorV(T) { class OperatorV { ... } } If you're just typing `OperatorV` with no template arguments, you're referring to the template itself. There's not necessarily any relationship between two instantiations of a template with different arguments. You could write it so that OperatorV! int and OperatorV!string have entirely different sets of methods, constructors, and fields. Because of that, the compiler doesn't create a base class for you automatically. But you can do it yourself: class BaseOperator {} // or interface, or abstract class class OperatorV(T) : BaseOperator {}
Jan 31