#Question regarding scope, polymorphism, and inheritance

9 messages · Page 1 of 1 (latest)

kindred totemBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

steel copper
#

Here's the code:```cpp
#include <iostream>

#define PRINT(text) std::cout << text << std::endl
#define NEWLINE std::cout << std::endl

void EndProgram();

class Entity {
public:
float x, y;
int Id;

Entity() {
    AssignIndex();
    this->x = 0.f;
    this->y = 0.f;
    InitPrint();
}

~Entity() {
    PRINT("Destroyed entity " << Id << " of type " << GetType());
}

void InitPrint() {
    PRINT("Created entity " << Id << " of type " << GetType());
}

virtual std::string GetType() {
    typedef std::remove_pointer<decltype(*this)>::type A;
    A& self = *this;
    std::string type = typeid(self).name();
    return type.substr(type.find(" " + 1));
}

private:
static unsigned int id_iterator;

void AssignIndex() {
    Id = id_iterator++;
}

};
unsigned int Entity::id_iterator = 0;

class Player : public Entity {
public:
const char* Name;

Player() : Entity() {
    Name = "Jeff";
}

std::string GetType() override {
    typedef std::remove_pointer<decltype(this)>::type A;
    A& self = *this;
    std::string type = typeid(self).name();
    return type.substr(type.find(" " + 1));
}

};

void DoStuff() {
Entity e1 = Entity();
e1.InitPrint(); NEWLINE;

Entity e2 = Entity();
e2.InitPrint(); NEWLINE;

Player p1 = Player();
p1.InitPrint(); NEWLINE;

Entity e3 = Entity();
e3.InitPrint(); NEWLINE;

}

int main() {
DoStuff();
EndProgram();
}

#

Here's the console output:```
Created entity 0 of type class Entity
Created entity 0 of type class Entity

Created entity 1 of type class Entity
Created entity 1 of type class Entity

Created entity 2 of type class Entity
Created entity 2 of type class Player

Created entity 3 of type class Entity
Created entity 3 of type class Entity

Destroyed entity 3 of type class Entity
Destroyed entity 2 of type class Entity
Destroyed entity 1 of type class Entity
Destroyed entity 0 of type class Entity

----------- End Program -----------

Press enter to end the application.


kindred totemBOT
#

@steel copper

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

craggy birch
steel copper
#

I have a problem that I'm not quite sure how to solve. I have a class Player that inherits from Entity, and every entity is supposed to print its assigned Id and class/type to console on creation. My issue is that the GetType function prints the subclass when called from the main() function, but always prints the base class Entity when called from functions defined inside the Entity class.
My question is: How do I get the identity of a subclass from within the scope of the base class?

Here's the code:

kindred totemBOT
#