-
-
Save redpist/3740262 to your computer and use it in GitHub Desktop.
C++ML example 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _1_H_ | |
#define _1_H_ | |
#include <iostream> | |
template <class TChild> | |
class GrandParent | |
{ | |
protected: | |
GrandParent() { } | |
~GrandParent() { } | |
private: | |
void Print_() | |
{ | |
std::cout << "START Print (Parent)" << std::endl; | |
static_cast<TChild*>(this)->Print_(); | |
std::cout << "End Print (Parent)" << std::endl; | |
} | |
public: | |
void Print() | |
{ | |
Print_(); | |
} | |
}; | |
template <class TChild> | |
class Parent : public GrandParent<Parent<TChild> > | |
{ | |
private: | |
friend class GrandParent<Parent<TChild> >; | |
void Print_() | |
{ | |
std::cout << "START Print (" << childName_ << ")" << std::endl; | |
static_cast<TChild*>(this)->Print_(); | |
std::cout << "END Print (" << childName_ << ")" << std::endl; | |
} | |
protected: | |
Parent() { } | |
~Parent() { } | |
std::string childName_ = "<NONE>"; | |
public: | |
void Print() | |
{ | |
GrandParent<Parent<TChild> >::Print(); | |
} | |
}; | |
class Child : public Parent<Child> | |
{ | |
private: | |
friend class Parent<Child>; | |
void Print_() | |
{ | |
std::cout << "Child" << std::endl; | |
} | |
public: | |
Child() | |
{ | |
Parent<Child>::childName_ = "Child"; | |
} | |
~Child() { } | |
void Print() | |
{ | |
Parent<Child>::Print(); | |
} | |
}; | |
#endif /* _1_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "1.hh" | |
int main(int argc, char *argv[]) | |
{ | |
Child a; | |
a.Print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment