How to get object of original class from function of other class where other class object is member of original class

hi every one
The case is like
class B{
public:
somedata;
somefunction();
}
class A{
public:
data;
function();
}
in somefunction i want a pointer to current object of class A
plz help me m new to c++
If you want a pointer to class A, you can do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class A;
class B{
public:
    somedata;
    somefunction()
    {
        A* somepointer;
    }
}
class A{
public:
    data;
    function();
}


But then you said "current object", which I'm assuming means this. You could try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
class A{
public:
    data;
    function;
};
class B : A{
public:
    somedata;
    somefunction()
    {
        A* object = dynamic_cast<A*>(this);
    }
};
Last edited on
Are you forced to use the classes as you have defined or can you have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A
{
public:
	void* data;
	void function()
	{
	}
};

class B : public A
{
public:
	void* somedata;
	A* somefunction()
	{
		return (A*)this;
	}
};
Topic archived. No new replies allowed.