tricky c++ question

Here's a bit of code

(from CvPlayer.cpp)

1
2
3
4
5
6
7
8
9
10
#include "CvTeamAI.h"
#include "CvPlayerAI.h"
#include "CvPlayer.h"

void CvPlayer::doTurn()
{
(...)
	AI_doTurnPost();
(...)
}



(from CvPlayer.h)

1
2
3
4
5
6
7
8
class CvPlayer
{
(...)
public:
(...)
	virtual void AI_doTurnPost() = 0;
(...)
};



(from CvPlayerAI.cpp)

1
2
3
4
5
6
7
#include "CvPlayerAI.h"
#include "CvTeamAI.h"

void CvPlayerAI::AI_doTurnPost()
{
(...)
}



(from CvPlayerAI.h)

1
2
3
4
5
6
7
8
9
10
#include "CvPlayer.h"

class CvPlayerAI : public CvPlayer
{

public:
(...)
	void AI_doTurnPost();
(...)
};



(from CvTeam.cpp)

1
2
3
4
5
6
7
8
9
#include "CvTeamAI.h"
#include "CvPlayerAI.h"
#include "CvTeam.h"

void CvTeam::doTurn()
{
(...)
	AI_doTurnPost();
}



(from CvTeam.h)

1
2
3
4
5
6
7
8
class CvTeam
{
(...)
public:
(...)
	virtual void AI_doTurnPost() = 0;
(...)
};



(from CvTeamAI.cpp)

1
2
3
4
5
6
7
#include "CvTeamAI.h"
#include "CvPlayerAI.h"

void CvTeamAI::AI_doTurnPost()
{
(...)
}



(from CvTeamAI.h)

1
2
3
4
5
6
7
8
9
10
#include "CvTeam.h"

class CvTeamAI : public CvTeam
{
(...)
public:
(...)
	void AI_doTurnPost();
(...)
};


I'm wondering which implementation of AI_doTurnPost() will execute in both the calls that are shown here... I presume that the one in CvTeam.cpp will use the definition provided by CvTeamAI.cpp (please correct me if I'm wrong), but what about the call made in CvPlayer.cpp? Will it use the definition that's in CvPlayerAI.cpp, or will just return 0 like it says in CvPlayer.h? And in both CvPlayer.h and CvTeam.h, what does the keyword virtual does there?

Thank you in advance,
AeonFlux1212

by the way I tried to be as accurate as possible with the includes on top of each block of code, in case it's relevent to the question...
Last edited on
There's no ambiguity about what will be called.

Both CvPlayer::AI_doTurnPost and CvTeam::AI_doTurnPost are pure virtual functions. This means they do NOT have an implementation in their respective base classes. The =0 is what indicates a pure virtual function. The notation does not mean the function returns 0. In fact, it can't since it's a void function.

A virtual function is one that is overloaded in a derived class.
http://www.cplusplus.com/doc/tutorial/polymorphism/
http://www.cplusplus.com/forum/general/21422/

A classic example of an abstract class is
1
2
3
4
class Shape
{
public:
  double area () = 0;

You can't instantiate Shape directly since it's an abstract class. But you can derive Square and Circle from it, both of which implement area(). Now if I have a pointer to some object derviced from Shape, I can call shape->area() and the appropriate function will be called.
I see that in CvTeam.h and in CvPlayer.h the functions are defined as virtual, but in classes CvTeamAI and CvPlayerAI they're not... and that the definitions provided by CvTeamAI.cpp and CvPlayerAI.cpp are not the same :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void CvPlayerAI::AI_doTurnPost()
{
	PROFILE_FUNC();

	if (isHuman())
	{
		return;
	}

	if (isBarbarian())
	{
		return;
	}

	if (isMinorCiv())
	{
		return;
	}

	AI_doDiplo();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void CvTeamAI::AI_doTurnPost()
{
	AI_updateWorstEnemy();

	AI_updateAreaStragies(false);

	if (isHuman())
	{
		return;
	}

	if (isBarbarian())
	{
		return;
	}

	if (isMinorCiv())
	{
		return;
	}

	AI_doWar();
}


Do you see what I mean?
In this case I presume that the one from CvPlayerAI will be used in the CvPlayer.cpp call and the one from CvTeamAI will be use in the CvTeam.cpp call... is that right?
The CvTeam and CvPlayer inheritance trees are unrelated. You just happen to have two classes (four if you count the abstract base classes) that have a function with the same name. This isn't a problem since the function name is qualified by the class it occurs in.
ok thanks a lot!
Topic archived. No new replies allowed.