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?
#include<iostream>
usingnamespace std;
class A{
public:
virtualvoid Display(){cout <<"I'm A\n";}
};
class B : public A{
public:
virtualvoid Display(){cout <<"I'm B\n";}
};
class C : public A{
public:
virtualvoid 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:
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 :/
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:
// 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.
#include<iostream>
#include<map>
#include<string>
usingnamespace std;
class A{
public:
virtualvoid Display(){cout <<"I'm A\n";}
};
class B : public A{
public:
virtualvoid Display(){cout <<"I'm B\n";}
};
class C : public A{
public:
virtualvoid 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:
virtualvoid 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!
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.