Method parameter pointer for object, without knowing class name?

May 19, 2012 at 10:33am
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 May 19, 2012 at 10:35am
May 19, 2012 at 10:50am
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 May 19, 2012 at 1:15pm
May 19, 2012 at 10:52am
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.
May 19, 2012 at 11:03am
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 May 19, 2012 at 11:05am
May 19, 2012 at 11:09am
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!
May 19, 2012 at 11:13am
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.
May 19, 2012 at 11:15am
Thank you, will do.
May 19, 2012 at 11:17am
> 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. 
May 19, 2012 at 12:02pm
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
May 19, 2012 at 12:06pm
Ohps sorry. Haven't been programming C++ for a while... Well, that just emphasizes point two of my post lol.
May 19, 2012 at 7:30pm
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.