forloRn_
Legacy Member
Ik heb hieronder dezelfde code staan in Java en C++. De Java-versie werkt zonder problemen maar de C++-versie compileert niet. 't Kan aan mijn kater liggen maar ik zie echt niet waarom. Niet meteen weglopen want het is eenvoudige code hoor.
Java:
C++:
Output van g++:
Als je protected vervangt door public werkt het wel. Iemand een idee?
Java:
Code:
import java.util.*;
abstract class Entity {
public abstract void collidesWith(Entity other);
protected void collidedWith(Spaceship spaceship) {}
protected void collidedWith(Asteroid asteroid) {}
}
class Spaceship extends Entity {
public void collidesWith(Entity other) {
other.collidedWith(this);
}
protected void collidedWith(Asteroid asteroid) {
System.out.println("spaceship destroyed");
}
}
class Asteroid extends Entity {
public void collidesWith(Entity other) {
other.collidedWith(this);
}
protected void collidedWith(Spaceship spaceship) {
System.out.println("asteroid doesn't have a scratch");
}
}
public class DoubleDispatch {
public static void main(String[] args) {
Entity spaceship = new Spaceship();
Entity asteroid = new Asteroid();
List<Entity> entities = new ArrayList<Entity>();
entities.add(spaceship);
entities.add(asteroid);
for (int i = 0; i < entities.size(); i++) {
Entity current = entities.get(i);
for (int j = i + 1; j < entities.size(); j++) {
Entity next = entities.get(j);
if (overlap(current, next)) {
current.collidesWith(next);
next.collidesWith(current);
}
}
}
}
private static boolean overlap(Entity e1, Entity e2) {
return true;
}
}
C++:
Code:
/*
* doubledispatch.cpp
*
* Created on: Sep 25, 2009
* Author: forlorn
*/
#include <iostream>
#include <vector>
using namespace std;
class Entity;
class Spaceship;
class Asteroid;
bool overlap(const Entity & e1, const Entity & e2);
class Entity {
public:
virtual void collidesWith(Entity & other) const = 0;
protected:
virtual void collidedWith(const Spaceship & spaceship) {}
virtual void collidedWith(const Asteroid & asteroid) {}
};
class Spaceship: public Entity {
public:
void collidesWith(Entity & other) const {
other.collidedWith(*this);
}
protected:
void collidedWith(const Asteroid & asteroid) {
cout << "spaceship destroyed" << endl;
}
};
class Asteroid: public Entity {
public:
void collidesWith(Entity & other) const {
other.collidedWith(*this);
}
protected:
void collidedWith(const Spaceship & spaceship) {
cout << "asteroid doesn't have a scratch" << endl;
}
};
int main() {
Spaceship spaceship;
Asteroid asteroid;
vector<Entity *> entities;
entities.push_back(&spaceship);
entities.push_back(&asteroid);
for (vector<Entity *>::iterator current = entities.begin(); current != entities.end(); current++) {
for (vector<Entity *>::iterator next = current + 1; next != entities.end(); next++) {
if (overlap(**current, **next)) {
(*current)->collidesWith(**next);
(*next)->collidesWith(**current);
}
}
}
return 0;
}
bool overlap(const Entity & e1, const Entity & e2) {
return true;
}
Output van g++:
Code:
../doubledispatch.cpp: In member function ‘virtual void Spaceship::collidesWith(Entity&) const’:
../doubledispatch.cpp:23: error: ‘virtual void Entity::collidedWith(const Spaceship&)’ is protected
../doubledispatch.cpp:30: error: within this context
../doubledispatch.cpp: In member function ‘virtual void Asteroid::collidesWith(Entity&) const’:
../doubledispatch.cpp:24: error: ‘virtual void Entity::collidedWith(const Asteroid&)’ is protected
../doubledispatch.cpp:42: error: within this context
Als je protected vervangt door public werkt het wel. Iemand een idee?

Visibility werkt toch op klasseniveau, en niet op objectniveau?




