Generic Programming

zhaozj2021-02-16  47

Hello all. It's nice to meet you all here. I Got A Lot of Knowledge from 9cbs. Now, i try to feed back some. The Topic is about generic programming.

I Paste a snippet to show how it it works.

Everyone Who Has Studied The Book Think In C 2nd Edition Knows The Sample, Trash Collection, Which Is At The end of chapter 10, design pattern.

OK, Let's Begin with this.

The Sample Mentioned in Think In C Using Double Dispatch TO IMPLEMENT The Design. And i use generic programming.

Enjoy.

#include

#include

Using namespace std;

Class trash

{

}

Class Paperbin;

Class Glassbin;

Class Paper: Public Trash

{

PUBLIC:

Typedef Paperbin Abin;

}

Class Glass: Public Trash: PUBLIC TRASH

{

PUBLIC:

Typedef glasbin abin;

}

Class Paperbin: Public Vector

{

}

Class Glassbin: Public Vector

{

}

Template

Struct Int2Type

{

Enum {value = v};

}

Template

Class Conversion

{

Typedef char small;

Struct Big {char Dummy [2];

}

Static Small Test (U);

Static Big Test (...);

Static t maket ();

PUBLIC:

ENUM {EXISTS = (SIZEOF (TEST ()) == SIZEOF (SMALL))}

}

Template

Add (Trashs Trash, Bin Bin)

{

Cout << Conversion :: exists << endl;

_Add (trash, bin, int2type :: exists> ());

}

Template

_Add (trash trash, bin bin, int2type )

{

bin.push_back (trash);

COUT << "Success Adding" << ENDL;

}

Template

_Add (trash trash, bin bin, int2type )

{

Cout << "failed adding" << Endl;};

int main ()

{

Paper apaper;

Glass aglass;

Paperbin apaperbin;

Glassbin Aglassbin;

Add (apaper, apaperbin);

Add (Aglass, Aglassbin);

Add (APAPER, AGLASSBIN);

Add (Aglass, APAPERBIN);

Return 0;

}

You can reach me by send me a email at bu 3bu4@263.net.

转载请注明原文地址:https://www.9cbs.com/read-27404.html

New Post(0)