Skip to content

Instantly share code, notes, and snippets.

@a-x-
Created August 11, 2016 10:34
Show Gist options
  • Select an option

  • Save a-x-/1ac3e48f508b3fe68e44dcb919f2dbf7 to your computer and use it in GitHub Desktop.

Select an option

Save a-x-/1ac3e48f508b3fe68e44dcb919f2dbf7 to your computer and use it in GitHub Desktop.
Вспомнить наследование в c++ / Компиляция и запуск на маке: `clang -O3 -stdlib=libstdc++ inherit.cpp -o inherit&&./inherit`
// clang -O3 -stdlib=libstdc++ inherit.cpp -o inherit&&./inherit
#include <stdio.h>
#include <stdlib.h>
class iBem {
protected: int setMod(){return 1;};
};
/*
* `class Block: public iBem`: Public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class.
* A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
*/
class Block: public iBem {
public: int hide(){return this->setMod();}
};
class Block2: public iBem {
// Use setMod via proxy
public: int foo(){
Block* anotherBlock = new Block();
return anotherBlock->hide();
}
// Use setMod immediately
public: int bar(){
// Block* anotherBlock = new Block();
// return anotherBlock->setMod(); // -- error
}
};
class Block2Mod: public Block2 {
// Use setMod immediately
public: int baz(){
return this->setMod();
}
};
int main(void) {
Block2* block = new Block2();
Block2Mod* blockMod = new Block2Mod();
printf("%d", block->foo());
// printf("%d", block->bar()); // -- error
printf("%d", blockMod->baz()); // -- ok
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment