Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 2.17 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class myBaseClass
  2. {
  3. public:
  4.         virtual void myMethod() { /* do fancy things */ }
  5. }
  6.  
  7. class myDerivedClass : myBaseClass
  8. {
  9. public:
  10.         virtual void myMethod() { /* do fancier things */ }
  11. }
  12.  
  13. class myNonDerivedClass
  14. {
  15. public:
  16.         void myMethod() { /* do unfancy things :( */ }
  17. }
  18.  
  19.  
  20. template <typename T>
  21. void myFunction(T parameter)
  22. {
  23.         parameter.myMethod();
  24. }
  25.  
  26.  
  27. // Below are different main functions, each one which produces a different result.
  28. int main()
  29. {
  30.         myFunction<myBaseClass>(someVariable);
  31.         // This main function, *at compile time*, produces a new function like:
  32.         // void myFunction_myBaseClass(myBaseClass parameter) { parameter.myMethod(); }
  33.         // And then tries to compile it.  Notice if you typed this code, there would be no compiler errors.
  34. }
  35.  
  36.  
  37. int main()
  38. {
  39.         myFunction<myBaseClass>(someVariable);
  40.         myFunction<myDerivedClass>(someOtherVariable);
  41.         // This would make the compiler produce 2 functions:
  42.         // void myFunction_myBaseClass(myBaseClass parameter) { parameter.myMethod(); }
  43.         // void myFunction_myDerivedClass(myDerivedClass parameter) { parameter.myMethod(); }
  44.         // Both of which would compile.
  45. }
  46.  
  47. int main()
  48. {
  49.         myFunction<myBaseClass>(someVariable);
  50.         myFunction<myNonDerivedClass>(someOtherVariable);
  51.         // You might expect this one to fail, because now myNonDerivedClass is not a derived class,
  52.         // But, look at the function it produces:
  53.         // void myFunction_myNonDerivedClass(myNonDerivedClass parameter) { parameter.myMethod(); }
  54.         // It still has a myMethod method, even though this method has no connection to the base class one.
  55.         // This is important for things like operators and such,
  56.         // and means you can write your own classes that work with the STL templated functions,
  57.         // without having to inherit from their classes, as long as you implement the methods it needs
  58.         // to operate.
  59. }
  60.  
  61. int main()
  62. {
  63.         myFunction<int>(10);
  64.         myFunction<std::vector<std::string>>(someVector);
  65.         // These produce the following functions:
  66.         // void myFunction_int(int parameter) { parameter.myMethod(); }
  67.         // void myFunction_stdstring(std::string parameter) { parameter.myMethod(); }
  68.         // Neither of these compile, because neither has a method named "myMethod".
  69. }