pointer to function

How can access to a variable defined in a function which is part of a derived class from a base class? Is the follow code ok?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

CClassBase{
public:
void FunctionBase;
CClassDerived  *m_pVble; // define a pointer to an object of the Derived class
};

CClassDerived{

void FunctionDerived;

};	

void CClassBase::FunctionBase{
nVble=m_pVble->Vble; // pointer to vble of the derived class. is ok?
}


void CClassDerived::FunctionDerived{
Wstring Vble;
Vble = getInfoString();	
}


Thank you in advance
Last edited on
You cannot access a variable defined in a function (from another function). It's a local variable.

A function name must end with (): void FunctionBase();

You can have a pointer to the derived class. But then you need a forward declaration/reference:

https://en.wikipedia.org/wiki/Forward_declaration

1
2
3
4
5
CClassBase{
public:
void FunctionBase(); // Note: ()
class CClassDerived  *m_pVble; // Note: class -> forward reference
};


1
2
3
4
5
6
class CClassDerived; // Note: forward declaration
CClassBase{
public:
void FunctionBase(); // Note: ()
CClassDerived  *m_pVble;
};


Line 15: nVble is not defined -> error
@coder777 i think you need to add 'class' keyword before 'CClassBase' though.

1
2
3
4
5
6
class CClassDerived; // Note: forward declaration
class CClassBase{///addition of class here
public:
void FunctionBase(); // Note: ()
CClassDerived  *m_pVble;
};

i think you need to add 'class' keyword before 'CClassBase' though.
Yes
Thanks
Topic archived. No new replies allowed.