Skip to content

Instantly share code, notes, and snippets.

@psqq
Last active June 6, 2017 00:20
Show Gist options
  • Save psqq/2b7519c8d2b743f3b5bb72488bb3a28b to your computer and use it in GitHub Desktop.
Save psqq/2b7519c8d2b743f3b5bb72488bb3a28b to your computer and use it in GitHub Desktop.
Наследование и текучий интерфейс
#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