Skip to content

Instantly share code, notes, and snippets.

@redpist
Created September 17, 2012 22:48
Show Gist options
  • Save redpist/3740262 to your computer and use it in GitHub Desktop.
Save redpist/3740262 to your computer and use it in GitHub Desktop.
C++ML example 1
#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_ */
#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