я, например, так и не услышал ответа на многочисленные вопросы касаемо делегатов в Д...
C++ (Qt)#include <stdio.h> class base{private: int i, j; void prn(int t) { printf("%s: %d \n", pref(), t); /* 1 */ } protected: void prn_i() { prn(i); } void prn_j() { prn(j); } public: base(int a, int b) : i(a), j(b) {} /* 2 */ base(int c) : i(c+1), j(c+2) {} /* 2 */ virtual const char* pref() { return "bs"; }}; class iface /* 4 */{public: virtual void foo() = 0; virtual void bar() = 0; void all() { foo(); bar(); }}; class impl1 : public base, public iface{public: // base impl1(int a, int b) : base(a, b) {} /* 3 */ impl1(int c) : base(c) {} /* 3 */ virtual const char* pref() { return "i1"; } public: // iface /* 4 */ virtual void foo() { prn_i(); } virtual void bar() { prn_j(); } }; class impl2 : public base, public iface{public: // base impl2(int a, int b) : base(a, b) {} /* 3 */ impl2(int c) : base(c) {} /* 3 */ virtual const char* pref() { return "i2"; } public: // iface /* 4 */ virtual void foo() { prn_j(); } virtual void bar() { prn_i(); } }; void poly(iface& i) /* 5 */{ i.all();} int main(){ impl1 i1(10); impl2 i2(20); poly(i1); poly(i2); return 0;}
Dimport std.stdio; class base{private: int i, j; void prn(int t) { printf("%*s: %d \n", pref(), t); /* 1 */ } protected: void prn_i() { prn(i); } void prn_j() { prn(j); } public: this(int a, int b) /* 2 */ { i = a; j = b; } this(int c) /* 2 */ { this(c+1, c+2); } char[] pref() { return "bs"; }} interface iface /* 4 */{ void foo(); void bar(); void all(); template iface_mix() { void all() { foo(); bar(); } }} class impl1 : base, iface{ // base: this(int a, int b) { super(a,b); } /* 3 */ this(int c) { super(c); } /* 3 */ char[] pref() { return "i1"; } // iface /* 4 */ mixin iface_mix; void foo() { prn_i(); } void bar() { prn_j(); } } class impl2 : base, iface{ // base: this(int a, int b) { super(a,b); } /* 3 */ this(int c) { super(c); } /* 3 */ char[] pref() { return "i2"; } // iface /* 4 */ mixin iface_mix; void foo() { prn_j(); } void bar() { prn_i(); } } void poly(iface i) /* 5 */{ i.all();} int main(){ impl1 i1 = new impl1(10); /* 6 */ impl2 i2 = new impl2(20); /* 6 */ poly(i1); poly(i2); return 0;}
C++ (Qt)foo::foo(int x, int y) : bar(x, y) {}
C++ (Qt)using bar::bar;
C++ (Qt)#include <iostream> class Piece{private: int x, y; protected: virtual bool chkmv(int dx, int dy) const = 0; public: Piece(int tx, int ty) : x(tx), y(ty) {} bool move(int dx, int dy) { if(!chkmv(dx, dy)) return false; x+=dx; y+=dy; return true; }}; class Rook : virtual public Piece{protected: virtual bool chkmv(int dx, int dy) const { return !(dx && dy); } public: // code dup :-( Rook(int x, int y) : Piece(x, y) {}}; class Bishop : virtual public Piece{protected: virtual bool chkmv(int dx, int dy) const { return dx==dy || (dx+dy)==0; } public: // code dup :-( Bishop(int x, int y) : Piece(x, y) {}}; class Queen : public Rook, public Bishop{protected: virtual bool chkmv(int dx, int dy) const { return Rook::chkmv(dx, dy) || Bishop::chkmv(dx, dy); } public: // code dup :-( Queen(int x, int y) : Piece(x, y), Rook(x, y), Bishop(x, y) // MAPA3M :-E {}}; int main(){ Queen q(0,4); bool t1 = q.move(1,1); bool t2 = q.move(0,4); bool t3 = q.move(2,3); std::cout<<t1<<" "<<t2<<" "<<t3<<"\n"; return 0;}
Perluse strict;package Piece; sub new{ my ($class, $tx, $ty) = @_; my $this = { x => $tx, y => $ty }; # constructor inheritage bless($this, $class); return $this;} sub move{ my ($this, $dx, $dy) = @_; # virtual static function call return 0 unless ref($this)->chkmv($dx, $dy); $this->{x} += $dx; $this->{y} += $dy; return 1;} 1;package Rook;our @ISA = qw(Piece); sub chkmv{ return !($_[1] && $_[2]);} 1;package Bishop;our @ISA = qw(Piece); sub chkmv{ return $_[1] == $_[2] || ($_[1] + $_[2]) == 0;} 1;package Queen;our @ISA = qw(Rook Bishop); sub chkmv{ return Rook::chkmv(@_) || Bishop::chkmv(@_);} 1;package main; my $q = new Queen(0,4);my $t1 = $q->move(1,1);my $t2 = $q->move(0,4);my $t3 = $q->move(2,3);print "$t1 $t2 $t3\n";
C++ (Qt)#include <iostream> class Piece{private: int x, y; protected: virtual bool chkmv(int dx, int dy) const = 0; public: Piece(int tx, int ty) : x(tx), y(ty) {} bool move(int dx, int dy) { if(!chkmv(dx, dy)) return false; x+=dx; y+=dy; return true; }}; template<class PieceStr> class PieceImpl : public Piece, protected PieceStr{protected: // "using PieceStr::chkmv" don't work :-( bool chkmv(int dx, int dy) const { return PieceStr::chkmv(dx, dy); } public: // code dup :-( PieceImpl(int x, int y) : Piece(x, y) {}}; class RookStr{protected: static bool chkmv(int dx, int dy) { return !(dx && dy); }}; class BishopStr{protected: static bool chkmv(int dx, int dy) { return dx==dy || (dx+dy)==0; }}; class QueenStr : public RookStr, public BishopStr{protected: static bool chkmv(int dx, int dy) { return RookStr::chkmv(dx, dy) || BishopStr::chkmv(dx, dy); }}; typedef PieceImpl <RookStr> Rook;typedef PieceImpl<BishopStr> Bishop;typedef PieceImpl <QueenStr> Queen; int main(){ Queen q(0,4); bool t1 = q.move(1,1); bool t2 = q.move(0,4); bool t3 = q.move(2,3); std::cout<<t1<<" "<<t2<<" "<<t3<<"\n"; return 0;}
Dimport std.stdio; class Piece{private: int x, y; protected: abstract bool chkmv(int dx, int dy); public: this(int tx, int ty) { x=tx; y=ty; } bool move(int dx, int dy) { if(!chkmv(dx, dy)) return false; x+=dx; y+=dy; return true; }} class PieceImpl(alias PieceStr) : Piece{protected: mixin PieceStr; // ok public: // code dup :-( this(int x, int y) { super(x,y); }} template RookStr(){ bool chkmv(int dx, int dy) { return !(dx && dy); }} template BishopStr(){ bool chkmv(int dx, int dy) { return dx==dy || (dx+dy)==0; }} template QueenStr(){ bool chkmv(int dx, int dy) { mixin RookStr Rk; mixin BishopStr Bs; return Rk.chkmv(dx, dy) || Bs.chkmv(dx, dy); }} alias PieceImpl!(RookStr) Rook;alias PieceImpl!(BishopStr) Bishop;alias PieceImpl!(QueenStr) Queen; int main(){ Queen q = new Queen(0,4); bool t1 = q.move(1,1); bool t2 = q.move(0,4); bool t3 = q.move(2,3); writefln("%d %d %d", t1, t2, t3); return 0;}
Dtemplate QueenStr(){ mixin RookStr Rk; mixin BishopStr Bs; bool chkmv(int dx, int dy) { return Rk.chkmv(dx, dy) || Bs.chkmv(dx, dy); }}