(C++) ProtectedExample1
February 24, 2017 · View on GitHub
(C++) ProtectedExample1
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
STL: GNU ISO C++ Library, version
4.9.2
Qt project file: ./CppProtectedExample1/CppProtectedExample1.pro
TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ -Werror SOURCES += main.cpp
./CppProtectedExample1/main.cpp
struct Base { Base(const int x = 0) : m_x(x) {} virtual ~Base() {} protected: int m_x; }; struct Derived : public Base { void SetX(const int x) { m_x = x; } //For a derived class, m_x is public }; int main() { Base b; //b.m_x = 0; //Not allowed! Only derived classes have access to Base::m_x Derived d; d.SetX(42); }