Method parameter pointer for object, without knowing class name?

Hello,
I am trying to send objects of characters (people, creatures etc...) to a method:
void attack(class * characterObject){} // EACH CHARACTER HAS THIS METHOD

The thing is, I want to send different types of character object references to the method, so this attack method will work on every type of character I pass as an argument to it. Each type of character is made from there own class (people, trolls, whatever...)

obviously the "class" keyword does not work. Is there anything like an "object" keyword or something that will allow any types of objects in?

Sorry if this is confusing.

Thanks.
Last edited on
If all of these provide a common interface...

1
2
3
4
5
6
7
8
9
10
11
12
13

class Character {
     /* what you want to do with a character */
};

class Troll : public Character {
    /* Troll specific behavior */
};

class Human : public Character {
   /* Human specific behavior */
};


And then you can have a method:

 
void attack (Character* character);


Which will work for all Characters (be they troll, human elf or whatever).

EDIT: Fixed inheritance declarations. Thanks ne.
Last edited on
Either

1
2
3
4
template < typename CHARACTER_TYPE > void attack( CHARACTER_TYPE* characterObject )
{
      characterObject->do_something() ; // EACH CHARACTER HAS THIS METHOD
}


Or

1
2
3
4
5
6
7
8
9
10
11
12
struct character { virtual ~character() {} virtual void do_something() = 0 ; /* ... */ };

struct creature :  character { virtual void do_something() override ;  /* ... */ };

struct robot :  character { virtual void do_something() override ;  /* ... */ };

// etc...

void attack( character* characterObject )
{
     characterObject->do_something() ;
}


Or both.
1
2
3
4
5
6
7
8
9
10
11
12
class character {
//...
};

void attack(character * characterObject){
//...
}

//[...]

character troll;
attack (&troll);


I'm just guessing, I don't know if I'm right, so wait for a truly answer! ;)

BTW, your question is indeed confusing... LOL
Last edited on
JCaselles that is correct.

So I think the post by hanst99 might be what I need.

So can someone explain what
class troll : Character {}
actually does?

does it add a secondary class to Character?

Thanks for all the replies guys!
It means "troll inherits from Character". In effect, each troll is also a character. Please look up some resource on Object Oriented Programming, especially in the context of C++ (as it's unfortunately not quite as straight forward as it might be here) for further details.
Thank you, will do.
> does it add a secondary class to Character?

See: http://www.cs.bu.edu/teaching/cpp/inheritance/intro/


You could also just overload the the function name:

1
2
3
4
void attack( troll* characterObject ) { /* ... */ }
void attack( creaturel* characterObject ) { /* ... */ }
void attack( human* characterObject ) { /* ... */ }
// ... etc. 
class troll : Character {}
It means "troll inherits from Character". In effect, each troll is also a character
Nope, it means that troll has a character.
Because the default especifier is private
In order to inherit you need to class troll: public Character
Ohps sorry. Haven't been programming C++ for a while... Well, that just emphasizes point two of my post lol.
Ok I now have a basic understanding of inheritance, so this makes much more sense now. Thank you all for the help!
Topic archived. No new replies allowed.