Last active
June 6, 2017 00:20
-
-
Save psqq/2b7519c8d2b743f3b5bb72488bb3a28b to your computer and use it in GitHub Desktop.
Наследование и текучий интерфейс
This file contains hidden or 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 <iostream> | |
using namespace std; | |
class Base { | |
int propertyBase; | |
public: | |
template<class T> | |
T& setPropertyBase(int propertyBase) { | |
this->propertyBase = propertyBase; | |
return *(T*)this; | |
} | |
void print() { | |
cout << "propertyBase == " << propertyBase << endl; | |
} | |
}; | |
class Derived : public Base { | |
int propertyDerived; | |
public: | |
Derived& setPropertyDerived(int propertyDerived) { | |
this->propertyDerived = propertyDerived; | |
return *this; | |
} | |
void print() { | |
Base::print(); | |
cout << "propertyDerived == " << propertyDerived << endl; | |
} | |
}; | |
int main() { | |
Derived derived; | |
derived.setPropertyBase<Derived>(2).setPropertyDerived(1); | |
derived.print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment