Polymorphic object pointer question

I have a abstract class with one single virtual member function. From this class I derive a lot of other classes, and in one of those derived classes it tries to pass a abstract pointer to point at itself. Take this example:

class A: public Baseclass
{
public: void function(); // The virtual function from Baseclass
private:
};

void A::function()
{
Baseclass *p = this; // Does this make p polymorph into class A?
}

The code should make my question obvious. Does it work, if not, how should I do it instead?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
using namespace std;
class A{
public:
	virtual void Display(){cout <<"I'm A\n";}
};
class B : public A{
public:
	virtual void Display(){cout <<"I'm B\n";}
};
class C : public A{
public:
	virtual void Display(){cout <<"I'm C\n";}
	void whoAmI(){
		cout << "Here I'm calling the this pointer, which points to the object itself\n";
		this->Display();
	}
};
int main(){
	// Three objects of diffrent classes
	class A a;
	class B b;
	class C c;
	a.Display();
	b.Display();
	c.Display();

	// one pointer of the base class
	class A* x;
	// Assign a value to the pointer
	x = &c;
	x->Display();
	// Assign another value to the pointer
	x= &b;
	x->Display();

	// Now note that when we assigned different values to the pointer
	// it picked up the correct class's display(), this is polymorphism
	
	c.whoAmI();

return 0;
}
Thanks for your reply, but that didn't answer my question.

The thing is that I have a member function, inside a derived class, that needs to pass itself (polymorphed) to a function outside that class that looks like this:

1
2
3
4
void external_function(Baseclass *a)
{
         a->runvirtualfunction();
}


So, I have a derived class that looks like this:

1
2
3
4
class B: public Baseclass
{
         ...
};


and class B contains a member function that looks like this:

1
2
3
4
5
6
7
void B::memberfunction()
{
           // Baseclass *p = this;  // DOES NOT WORK (IT SEEMS)
           external_function(p);    // where p is supposed to point at this instance  
                                    // of the class B. Baseclass *p = this does not
                                    // seem to work.
}


Summary: Can I assign an abstract pointer to the this pointer? It does not seem to work, as it generates a segfault :/
1
2
3
4
5
6
7
8

void B::memberfunction()
{
// this will work, because we are just passing a pointer

                      external_function(this); 
}
Last edited on
Thanks again! But the segfault still lurks. To example my problem (and hopefully reproduce it), I give you the contents of the external_function and what it does:

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



// First, a struct that is required to reproduce the segfault

struct TheStruct
{
          Baseclass *p;
          // ... and of course other members
};

// Earlier, a map-object(std::map) was instanced:
// map<std::string, TheStruct> a;


void external_function(string id, Baseclass *p)
{
                 a[id].p = p;   // Here the segfault appears
}


Sorry for the unstructured code, but I hope it reproduces the segfault on your side.
what is 'a' here ?
the std::map container
It is compiling for me, the error may be for something else
Here is the code I tested for your reference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<map>
#include<string>
using namespace std;
class A{
public:
	virtual void Display(){cout <<"I'm A\n";}
};
class B : public A{
public:
	virtual void Display(){cout <<"I'm B\n";}
};
class C : public A{
public:
	virtual void Display(){cout <<"I'm C\n";}
	void whoAmI(){
		cout << "Here I'm calling the this pointer, which points to the object itself\n";
		this->Display();
	}
};
struct TheStruct
{
          A *p;
          // ... and of course other members
};

map<std::string, TheStruct> xyz;

void external_function(string id, A *z)
{
         xyz[id].p =z;
		 cout << "I'm in external function\n";
		 cout<< id.c_str()<<"\n";
		 z->Display();
} 


class D: public A{
public:
	virtual void foo(){
		string s="John";
		external_function(s,this);
	
	}

};


int main(){
	// Three objects of diffrent classes
	class A a;
	class B b;
	class C c;
	a.Display();
	b.Display();
	c.Display();

	// one pointer of the base class
	class A* x;
	// Assign a value to the pointer
	x = &c;
	x->Display();
	// Assign another value to the point
	x= &b;
	x->Display();

	// Now note that when we assigned different values to the pointer
	// it picked up the correct class, this is polymorphism
	
	c.whoAmI();
// This class has the external function callled from foo()
class D d;

d.foo();

return 0;
}
Thanks a lot for your help! The segfault appears when I assign the pointer in the struct to the this pointer. The error that causes this is now up to me to find.. somewhere else, in the older and darker places of my code, where no comments are left to tell the stories about the lost pointers that resides there. Wish me luck!
Just curious, if you replace:
[code=c++]
a[id].p = p; // Here the segfault appears
[/code]

with:
[code=c++]
map<std::string, TheStruct>::iterator iter;
iter = mymap.find( id );

if( iter != mymap.end() ) {
(iter->second).p = p;
}
else {
cout << "ID doesn't exist in the map!";
}
[/code]

does it work or no?
Last edited on
To the original question, can a derived class have a base-class type pointer to point the self or similar (of same family) object, YES.
Can it be a polymorphic too, YES, as well. It all how you code it to run and give desired result.

You may not be able to cast to another object at same level in hierarchy, but with a help of dynamic_cast<> to determine which type of object it points-to in run-time, you would be able to program the code in a right direction.

Good luck :)
Last edited on
Topic archived. No new replies allowed.