opCall
Part of OperatorOverloadingCategory
Description
Shows how an opCall can be assigned to allow using C++-style "new"-ing on a class.
Example
import std.stdio : writefln; import std.string; class A { this(uint x) { num = x; } char[] toString() { return std.string.toString(num); } private uint num; static A opCall(uint x) { /* This overload allow the shorter C++ syntax for "new"-ing the class. */ return new A(x); } } void main() { /* and you can create class objects the in a c++ syntax */ A a = A(500); A a2 = new A(567); writefln("Number: %s", a); writefln("Number: %s", a2); }
Sample Batch File
@echo off set pgm=OpCallExample dmd %pgm%.d %pgm%.exe pause erase %pgm%.obj erase %pgm%.map
Expected Output
Number: 500 Number: 567
Tested Compiler and Operating System
- Tested with DMD 0.173.
- Compiled and tested on Windows 2000.
Source
Based on digitalmars.D:1539.