LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-18-2011, 05:47 PM   #1
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 67
[c++] Mixing abstract classes and multiple inheritance


Why are the 'defense' method calls actually using the 'offense' methods.

Also, is there a way to remove the commented base classes for 'Player' so that the casting isn't necessary?

Code:
#include <iostream>

class Offense
{
  public:
	virtual void doOffense() =0;
};

class OffenseComputer : public Offense
{
  public:
	void doOffense() { std::cout << "oc" << std::endl; }
};

class OffenseHuman    : public Offense
{
  public:
	void doOffense() { std::cout << "oh" << std::endl; }
};


class Defense
{
  public:
	virtual void doDefense() =0;
};

class DefenseComputer : public Defense
{
  public:
	void doDefense() { std::cout << "dc" << std::endl; }
	
};

class DefenseHuman    : public Defense
{
  public:
	void doDefense() { std::cout << "dh" << std::endl; }
};


class Player    //      : public Offense, public Defense
{
  public:
	int getNumber() { return 1; }
};

class PlayerComputer  : public Player, public OffenseComputer, public DefenseComputer
{
};

class PlayerHuman     : public Player, public OffenseHuman,    public DefenseHuman
{
};


int main()
{
	Player *p1 = new PlayerComputer;
	Player *p2 = new PlayerHuman;

	((Offense*) p1)->doOffense();
	((Defense*) p1)->doDefense();

	((Offense*) p2)->doOffense();
	((Defense*) p2)->doDefense();
}
This results in:
Code:
~$ g++ -Wall hello.cc && ./a.out
oc
oc
oh
oh

Last edited by Meson; 04-18-2011 at 05:48 PM.
 
Old 04-18-2011, 10:53 PM   #2
Shum
LQ Newbie
 
Registered: Oct 2007
Posts: 23

Rep: Reputation: 0
You're not casting properly.

Add another virtual method to Defense _before_ doDefense and you'll see that when you try to call doDefense like that you get a segfault. It was calling doOffense because doOffense happened to be in the same place in memory as where it was looking for doDefense.

Edit:
Your inheritance tree could look more like:

Code:
class Player;
class Offense : public Player;
class Defense : public Player;
class Human : public Player;
class Computer : public Player;
class OffenseHuman : public Offense, public Human;
class DefenseHuman : public Defense, public Human;
class OffenseComputer : public Offense, public Computer;
class DefenseComputer : public Defense, public Computer;
Although if you have a variable of type Player* you can't call doOffense and doDefense methods on it because you don't know whether its an Offense or Defense or either.
Do you need to Offense and Defense to be two separate classes? Because in your code all Players are both anyway.

Last edited by Shum; 04-18-2011 at 11:01 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Virtual Multiple Inheritance Problem PatrickNew Programming 7 05-27-2009 02:47 PM
PHP workaround for multiple inheritance? konqi Programming 3 03-08-2009 04:10 AM
c++ Abstract Base Classes exodist Programming 1 06-11-2005 06:05 PM
Multiple Inheritance in Java mikeshn Programming 2 02-25-2002 08:13 PM
RAD tools revisited: Multiple Inheritance and the Web Citizen Bleys Programming 1 02-11-2002 01:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration