PDA

View Full Version : Inheritance of nested class



bibhukalyana
6th August 2011, 10:09
hi everyone,

I have 2 nested class (nest_1,nest_2).1st class is inherited by 2nd class and i want to access member of 1st nested class in a function of 2nd class.
I tried like...

.h file


class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int main_data;


private:
Ui::MainWindow *ui;
public:
class nest_1
{
public:
int data1;
};
class nest_2 : public nest_1
{
public:
int data2;
int test();
};
};

class A : public MainWindow
{
public:
int data;

int test();
};



.cpp file



int MainWindow::nest_2::test()
{
// here i want to access data1
}


int A::test()
{
main_data = 0; //here i am able to access main_data
}



I tried same thing for MainWindow and A.It is working.But for nested class it is not.

Can any one tell me how to access it or any error in my code ?

with regards
bibhu.

wysota
6th August 2011, 10:20
Works for me...

class Outer {
public:

void call() { b.method(); }
private:
class A {
public:
int x;
};

class B : public A {
public:
void method() { x = 7; }
};

B b;
};


int main() {
Outer o;
o.call();
return 0;
}

bibhukalyana
6th August 2011, 18:54
thanks for reply........
i will try it.

if you want to use scope resolution operator(::) then how you will use it.