Below is one way to do this with minor changes to the OP code.
#include <cassert> class Class1 { public: Class1( int attribute ) : attribute_( attribute ) { } void Method1() { class Class2 { public: Class2( Class1 * parent ) : parent_( parent ) { } int parentAttribute() const { return parent_->attribute_; } private: Class1 * parent_; }; Class2 c2( this ); assert( c2.parentAttribute() == attribute_ ); } private: int attribute_; }; int main() { Class1 c1( 42 );; c1.Method1(); }
The code is also available at http://codepad.org/MUF3a8jL
source share