Generic var c++ -


I want to make a general hostile, which can be from a class or another class.

In this code sample I want var aa normal, in my code I can use code from class A or class B.

But this is essential to Global.

Can you help me? MyClass {Public: MyClass () {} // Controller Padro, Non Dave Serial Useful Identity Static Mayclass * getInstance () {Fixed MyClass * instance = 0; If (! Example) example = new MyClass (); Return example; }}; Class A: Public MyClass {Public: int a () {Return 1; }}; Class B: Public MyClass {Public: int b () {Return 1; }}; Template & lt; Square tiat & gt; Class AIT {Public: Fixed Tiat & amp; Example () {Fixed TAIT example; Return example; }}; AIT AA; Zero main () (A. Instance (). A (); // or aa.Instance () .b (); // A may be class 1a or class B. Therefore i // function a function or b (Not the same / time, of course);}

Short answer:

C ++ template lets you compile periodicity from time to time, do not do runtime generics.

There is no existence in the program (this A solid binary is executable). They will end in the form of generated codes only when their arguments will be clearly or inherently specified.

Therefore, it does not declare a variable :

  AIT aa;  

This declares a variable:

  AIT  A & gt; Come on;  

If you need runtime genericity, you must use a runtime dispatch mechanism:

  • Either switch case Than manually.
  • Or better use the common base class + the underlying succession of some virtual methods.

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -