can't get virtual function of parent class to work [duplicate] - c++

This question already has answers here:
C++ overridden virtual function not getting called [duplicate]
(3 answers)
Closed 4 years ago.
I have a parent class with a virtual function in it, I then make a child class and define the function. I then make a vector of vectors and insert one of the child classes into it. I then try to call the virtual function and nothing outputs to the screen. I do not know why this is happening, does anyone know?
Parent Class
class insect{
public:
string type;
int food_cost;
int armor;
int damage;
insect();
void set_food_cost(int x);
void set_armor(int x);
void set_damage(int x);
virtual void attack(){} // this is the problematic function
};
Child Class
class bee: public insect{
public:
bee();
int armor;
int damage;
void set_armor(int x);
void attack();
};
void bee::attack(){
cout << "im a bee, stab stab!\n";
}
Creating Vector of Vectors
vector< vector<insect> > insects_on_board(10);
Adding a bee to the vector of vectors
void add_bee(vector< vector<insect> > &insects_on_board, int &bees){
bees++;
insects_on_board[9].push_back(bee());
}
Function Call
cout << "testing " << insects_on_board.at(9).at(0).type << endl;
insects_on_board.at(9).at(0).attack();
Output
testing B
My Question Again
so in the output im expecting to see "testing B" and then "im a bee, stab stab!"
but only the "testing B" is outputted to the screen, any ideas why the other part is not?

That's because you are storing actual insects and not any bees. Polymorphism (in its basic C++ way) works when you are doing 3 things:
Have a type hierarchy with properly defined and overridden virtual methods (like you are doing here,)
Create child instances (or a variety of parent and child instances,)
Access them through pointers (or references.)
You are missing points 2 and 3.
So, one way to fix it would be to store pointers to insect, and initialize them as either bees or plain insects, like so:
vector<vector<insect *>> insects_on_board (10, vector<insect *>(2)); // note the type
insects_on_board[9][0] = new bee;
insects_on_board[9][1] = new insect;
// Note the use of "->" instead of "."
cout << "testing " << insects_on_board[9][0]->type << endl;
insects_on_board[9][0]->attack();
// Contrast the output with the above's
cout << "testing " << insects_on_board[9][1]->type << endl;
insects_on_board[9][1]->attack();
Update:
Note that (at the basic level) any container that stores insects by value cannot contain anything else; not even classes derived from insect. All the polymorphism and stuff you read and hear about is only applicable for pointers to parent and child types (or references to them.)
So, your add_bee function should look like this:
void add_bee (vector<vector<insect *>> & insects_on_board, int & bees) {
bees++;
insects_on_board[9].push_back(new bee());
}
I've made only two changes in there: the vectors now contain pointers to insect and I'm newing the bees.

Related

Making a vector of superclass type that can store subclass objects

So I started a small little project to work on while I am learning. Basically, what I'm trying to do is a small "game" which I plan to build on as I learn new things.
Here is a brief description and my problem.
Basically, I want to assign various Hero types to a player based on their choice.
I made a base class "Hero" with only a HP parameter so far. After that, I made 2 derived classes from Hero, HeroType1, HeroType2 which will have specific abilities and so on.
I decided on storing various hero types in std::vector<Hero*> Heroes. Basically, I start my "game" by calling initializeHeroes function which, depending on the player choice creates a new object of type NewHero1 or NewHero2 and stores it an the vector mentioned before. The thing is, no matter what I tried so far, I can't access derived member functions when I want to use them later, only those of the Hero class.
What feels like a good solution: declare global variables player1, player2 and assign to them after players choose the HeroType. However, I can't do that because the data type has to be known before compiling. Sorry if this is a stupid and basic question, my knowledge is still very limited and that is why I am asking for some hints here.
I'd kindly like to ask on how would you approach this, I know it is a very simple issue, but I'm still a beginner and I'm trying to figure out the best way to solve this. Thanks in advance.
If you would like to call a member function from a element from std::vector<Hero*> Heroes and you know somehow that this element points to a Hero2-type, then you could create a new temporary variable Hero2 * tmpPtr and set this variable to the element whose memberfunction you want to call (tmpPtr = Heroes[i]). Then you should be able to call a memberfunction like this: tmpPtr->hero2Memberfuncion().
Full code:
#include <iostream>
#include <vector>
class SomeClass
{
public:
void a() {
std::cout << "a" << std::endl;
}
};
class AnotherClass : public SomeClass
{
public:
void b() {
std::cout << "b" << std::endl;
}
};
void main() {
std::vector<SomeClass *> vec;
AnotherClass v;
vec.push_back(&v);
AnotherClass * tmpPtr = (AnotherClass *)vec[0];
tmpPtr->b(); //Output: "b"
}
However if you want for example loop through the whole vector and for every element run a memberfunction that has the same name but the body of that function differs depending on to what Hero-type the element points, then you may want to use virtual functions. Example:
#include <iostream>
#include <vector>
class SomeClass
{
public:
virtual void a() {
std::cout << "from SomeClass" << std::endl;
}
};
class AnotherClass : public SomeClass
{
public:
void a() {
std::cout << "from AnotherClass" << std::endl;
}
};
void main() {
std::vector<SomeClass *> vec;
AnotherClass v1;
vec.push_back(&v1);
vec[0]->a(); //Output: "from AnotherClass"
SomeClass v2;
vec.push_back(&v2);
vec[1]->a(); //Output: "from SomeClass"
}

debuging--Class / object stuff, noob problems

Please tear this code apart, make it complex and scarcely readable, I'd rather learn the hard way once than learn the same thing many times the wrong way.
The base class is as follows:
class baseMob{
private:
int _healthMax;
int _healthCurrent;
int _manaMax;
int _manaCurrent;
int _experiencePoints;
public:
//Set max Hp
void setHealthMax(int);
//Get max Hp
int getHealthMax();
//Set Current Hp
void setCurrentHealth(int);
//Get Current Health
int getCurrentHealth();
//Set Max Mana
void setMaxMana(int);
//Get Max Mana
int getMaxMana();
//Set Current Mana
void setCurrentMana(int);
//Get Current Mana
int getCurrentMana();
//getMob Exp on kill
int getExperiencePoints();
//Set mob Exp points
void setExperiencePoints(int);
//leaving out the member functions for space conservation
};
The individual mob that I'm trying to create is a green slime, which I'm trying to create via the default constructor I've made...
class greenSlime: private baseMob{
public:
greenSlime(){
setHealthMax(100);
setMaxMana(100);
setCurrentHealth(100);
setCurrentMana(100);
setExperiencePoints(150);
}
};
My main function looks like this right now:
greenSlime slime();
for(; slime.getCurrentHealth() >= 0; slime.setCurrentHealth(-1)){
cout << "The current health of the slime is: " << slime.getCurrentHealth() << endl;
if (slime.getCurrentHealth() <= 0 ){
cout << "Player is awarded with: " << slime.getExperiencePoints() << " Experience. ";
}
}
If anyone wants to tear this up and make me look like a jackass, I'd really appreciate the help.
The error that I'm presently getting is:
Project1.cpp:107: error: request for member getCurrentHealth' inslime', which is of non-class type `greenSlime ()()'
Along with other errors of the same type.
Tl;Dr: Class implementation isn't working, posted all my source when I probably could've posted about 1/10th of this and still made sense, and would love to have someone tell me why it's not working and how bad I am.
The problem is that the compiler thinks that the slime declaration line is another type of predeclaration since greenSlime doesn't have a ctor with parameters.
You can fix it by not putting the parenthesis after slime.
// greenSlime slime();
greenSlime slime;
Here is the absolute best advice I can give regarding weird errors that you don't understand. Make a smaller example of the problem makes it easier to uncover what is actually wrong.
Here is what I wrote to test.
struct Foo {
Foo() {}
void bar() {}
};
int main(int argc, char ** argv) {
Foo foo;
foo.bar();
return 0;
}
Others have pointed out problems in terms of why your code doesn't work. I'll make a recommendation about program design.
When explaining inheritance, tutorials and programming classes frequently use toy examples that are very similar to your code. These examples show what inheritance is, but really aren't very good at showing what inheritance is useful for.
I wouldn't use inheritance in this case. I think a better design is to have a class that represents the mob type and holds all the data that is static for all that mob of that type, such as the mob name, starting/max HP, attack types, etc. And then have another class where each instance represents a specific mob and holds data that changes for that mob, such as current hp.
class Mob_type {
string name;
int max_hp;
vector<shared_ptr<Attack_type>> attacks;
public:
Mob_type(string name,int max_hp,vector<shared_ptr<Attack_type>> attacks)
: name(name),max_hp(max_hp),attacks(attacks) {}
int get_max_hp() const { return max_hp; }
};
class Mob {
Mob_type const &type;
int hp;
public:
Mob(Mob_type const &type) : type(type), hp(type.get_max_hp()) {}
Mob_type const &get_type() const { return type; }
int get_hp() const { return hp; }
};
void print_health(Mob const &m) {
cout << m.get_hp() << '/' << m.get_type().get_max_hp() << '\n';
}
int main() {
vector<shared_ptr<Attack_type>> attacks; // ...
Mob_type green_slime("Green Slime",50,attacks);
Mob green_slime_A(green_slime), green_slime_B(green_slime);
fight(green_slime_A,green_slime_B);
cout << "A: ";
print_health(green_slime_A);
cout << "B: ";
print_health(green_slime_B);
}
This way you can have a data file that contains the mob types and all you have to do to add a type is to update the data file.
class greenSlime: private baseMob{
should be:
class greenSlime: public baseMob{
Since the class that you're inheriting from is private, you can't see any of the inherited methods.
Also, what Tom Kerr said, you don't want the parentheses after you declare your object. Basically, if you don't want any parameters, don't use the parentheses when making an object.
Also, very next thing I imagine you'll encounter: you almost never want private inheritance, at least not unless you know you really want it. I'm guessing you meant for the class declaration of greenSlime to be
class greenSlime: public baseMob
First of all if you make private inheritance you won't be able to access any of base class' functions. Public inheritance allows you access base class' public and protected functions and members.
Second if you want to make a pointer of greenSlime class, you must do:
//greenSlime() with parentheses
greenSlime *slime = new greenSlime();
But if you want to make an object of greenSlime with non parameter constructor (default constructor) you must do:
//without parentheses
greenSlime slime;

calling virtual functions through pointers with and without consulting the VM-table

I want to take the address of a member function of a c++ class, store it in a pointer, and call the virtual function later on.
I know some things about it, but do not now how to take the address of a certain implementation of a virtual function that is NOT the implementation of the most descendent class (the actual class of the object).
Here is some sample code:
#include <iostream>
using namespace std;
class ca
{
public:
virtual void vfunc() {cout << "a::vfunc ";}
void mfunc() {cout << "a::mfunc ";}
};
class cb : public ca
{
public:
virtual void vfunc() {cout << "b::vfunc ";}
};
extern "C" int main(int, char **)
{
void (ca:: *ptr_to_vfunc)() = &ca::vfunc;
cout << sizeof(ptr_to_vfunc) << " ";
cb b;
(b.*ptr_to_vfunc)();
ca a;
(a.*ptr_to_vfunc)();
void (ca:: *ptr_to_mfunc)() = &ca::mfunc;
cout << sizeof(ptr_to_mfunc) << " ";
(a.*ptr_to_mfunc)();
}
The output is:
12 b::vfunc a::vfunc 12 a::mfunc
I am working with win32-environment, and the size of member function pointers is 3 * 32-bits values! I did not specify an object when I took the address of the member function and yet, my call invokes the most descendant class' implementation of vfunc().
1) What is going on here? Why 12 bytes in stead of 4?
2) How can I take the address of ca::vfunc() and call it on b, like I normaly would do with b.ca::vfunc().
Ok: Its doing exactly what it it is supposed to do.
But to answer you questions:
1) What is going on here? Why 12 bytes in stead of 4?
Why not.
The standard does not specify a size.
I am not sure why you expect a normal pointer to be 4.
If the question is "why is a method pointer larger than a normal pointer?"
Because the implementation needs the extra space to hold information about the call.
2) How can I take the address of ca::vfunc() and call it on b, like I normaly would do with b.ca::vfunc().
You cant.

Static ctor/dtor observer for arb. C++ classes

I have a series of classes A, B, ... which have many derived classes which are created inside a module I do not wish to change.
Additionally, I have at least one class Z, which has to be informed whenever an object of type A (or derived classes) is created or destroyed. In the future, there may be more classes, Y, X that want to observe different objects.
I am looking for a convenient way to solve this.
At first glance, the problem seemed trivial, but I'm kind of stuck right now.
What I came up with, is two base classes SpawnObserver and SpawnObservable which are supposed to do the job, but I am very unhappy with them for several reasons (see attached simplification of these classes).
When Z is notified, the actual object is either not yet or not anymore existent, due to the order in which base classes are created/destroyed. Although the pointers can be compared when destroying an object (to remove them from some data-structures in Z) this does not work when it is created and it surely does not work when you have multiple inheritance.
If you want to observe only one class, say A, you are always notified of all (A, B, ...).
You have to explicitly if/else through all classes, so you have to know all classes that inherit from SpawnObservable, which is pretty bad.
Here are the classes, which I tried to trim down to the most basic functionality, which you need to know to understand my problem. In a nutshell: You simply inherit from SpawnObservable and the ctor/dtor does the job of notifying the observers (well, at least, this is what I want to have).
#include <list>
#include <iostream>
class SpawnObservable;
class SpawnObserver {
public:
virtual void ctord(SpawnObservable*) = 0;
virtual void dtord(SpawnObservable*) = 0;
};
class SpawnObservable {
public:
static std::list<SpawnObserver*> obs;
SpawnObservable() {
for (std::list<SpawnObserver*>::iterator it = obs.begin(), end = obs.end(); it != end; ++it) {
(*it)->ctord(this);
}
}
~SpawnObservable() {
for (std::list<SpawnObserver*>::iterator it = obs.begin(), end = obs.end(); it != end; ++it) {
(*it)->dtord(this);
}
}
virtual void foo() {} // XXX: very nasty dummy virtual function
};
std::list<SpawnObserver*> SpawnObservable::obs;
struct Dummy {
int i;
Dummy() : i(13) {}
};
class A : public SpawnObservable {
public:
Dummy d;
A() : SpawnObservable() {
d.i = 23;
}
A(int i) : SpawnObservable() {
d.i = i;
}
};
class B : public SpawnObservable {
public:
B() { std::cout << "making B" << std::endl;}
~B() { std::cout << "killing B" << std::endl;}
};
class PrintSO : public SpawnObserver { // <-- Z
void print(std::string prefix, SpawnObservable* so) {
if (dynamic_cast<A*>(so)) {
std::cout << prefix << so << " " << "A: " << (dynamic_cast<A*>(so))->d.i << std::endl;
} else if (dynamic_cast<B*>(so)) {
std::cout << prefix << so << " " << "B: " << std::endl;
} else {
std::cout << prefix << so << " " << "unknown" << std::endl;
}
}
virtual void ctord(SpawnObservable* so) {
print(std::string("[ctord] "),so);
}
virtual void dtord(SpawnObservable* so) {
print(std::string("[dtord] "),so);
}
};
int main(int argc, char** argv) {
PrintSO pso;
A::obs.push_back(&pso);
B* pb;
{
std::cout << "entering scope 1" << std::endl;
A a(33);
A a2(34);
B b;
std::cout << "adresses: " << &a << ", " << &a2 << ", " << &b << std::endl;
std::cout << "leaving scope 1" << std::endl;
}
{
std::cout << "entering scope 1" << std::endl;
A a;
A a2(35);
std::cout << "adresses: " << &a << ", " << &a2 << std::endl;
std::cout << "leaving scope 1" << std::endl;
}
return 1;
}
The output is:
entering scope 1
[ctord] 0x7fff1113c640 unknown
[ctord] 0x7fff1113c650 unknown
[ctord] 0x7fff1113c660 unknown
making B
adresses: 0x7fff1113c640, 0x7fff1113c650, 0x7fff1113c660
leaving scope 1
killing B
[dtord] 0x7fff1113c660 unknown
[dtord] 0x7fff1113c650 unknown
[dtord] 0x7fff1113c640 unknown
entering scope 1
[ctord] 0x7fff1113c650 unknown
[ctord] 0x7fff1113c640 unknown
adresses: 0x7fff1113c650, 0x7fff1113c640
leaving scope 1
[dtord] 0x7fff1113c640 unknown
[dtord] 0x7fff1113c650 unknown
I want to stress, that I am perfectly aware why my solution behaves the way it does. My question is whether you have a better approach of doing this.
EDIT
As an extension to this question (and inspired by the comments below), I'd like to know:
Why do you think this is a terrible approach?
As an additional note: What I an trying to accomplish by this is to install a normal Observer in each and every created object.
EDIT 2
I will accept an answer that solves problem 1 (bold one in the enumeration above) or describes why the whole thing is a very bad idea.
Use the curiously recurring template pattern.
template<typename T> class watcher {
typename std::list<T>::iterator it;
watcher();
~watcher();
void ctord(T*);
void dtord(T*);
};
template<typename T> class Observer {
public:
typedef std::list<T*> ptr_list;
static ptr_list ptrlist;
typedef typename ptr_list::iterator it_type;
it_type it;
typedef std::list<watcher<T>*> watcher_list;
static watcher_list watcherlist;
typedef typename watcher_list::iterator watcher_it_type;
Observer() {
ptrlist.push_back(this);
it_type end = ptrlist.end();
end--;
it = end;
for(watcher_it_type w_it = watcherlist.begin(); w_it != watcherlist.end(); w_it++)
w_it->ctord(this);
}
~Observer() {
ptrlist.erase(it);
for(watcher_it_type w_it = watcherlist.begin(); w_it != watcherlist.end(); w_it++)
w_it->ctord(this);
}
};
class A : public Observer<A> {
};
class B : public Observer<B> {
};
class C : public A, public B, public Observer<C> {
// No virtual inheritance required - all the Observers are a different type.
};
template<typename T> watcher<T>::watcher<T>() {
Observer<T>::watcherlist.push_back(this);
it = watcherlist.end();
it--;
}
template<typename T> watcher<T>::~watcher<T>() {
Observer<T>::watcherlist.erase(it);
}
template<typename T> void watcher<T>::ctord(T* ptr) {
// ptr points to an instance of T that just got constructed
}
template<typename T> void watcher<T>::dtord(T* ptr) {
// ptr points to an instance of T that is just about to get destructed.
}
Not just that, but you can inherit from Observer multiple times using this technique, as two Observer<X> and Observer<Y> are different types and thus doesn't require diamond inheritance or anything like that. Plus, if you need different functionality for Observer<X> and Observer<Y>, you can specialize.
Edit # Comments:
class C DOES inherit from Observer<A> and Observer<B> through A and B, respectively. It doesn't need to know or care whether or not they're being observed. A C instance will end up on all three lists.
As for ctord and dtord, I don't actually see what function they perform. You can obtain a list of any specific type using Observer::ptrlist.
Edit again: Oooooh, I see. Excuse me a moment while I edit some more. Man, this is some of the most hideous code I've ever written. You should seriously consider not needing it. Why not just have the objects that need to be informed about the others do their creation?
Issue 1 isn't easily solved (in fact I think it's impossible to fix). The curiously recurring template idea comes closest to solving it, because the base class encodes the derived type, but you'll have to add a base to every derived class, if you really insist on knowing the derived type when the base is being constructed.
If you don't mind performing your actual operations (other than the bookkeeping, I mean) or examining the list outside the constructor or destructor of each object, you could have it (re)build the minimal list only when the operation is about to be performed. This gives you a chance to use the fully-constructed object, and makes it easier to solve issue 2.
You'd do this by first having a list of objects that have been constructed, but aren't on the 'full' list. And the 'full' list would contain two pointers per constructed object. One is the pointer to the base class, which you'll store from the Observable constructor, possibly multiple times during the construction of a single object. The other is a void *, pointing to the most derived part of the object -- use dynamic_cast<void *> to retrieve this -- and is used to make sure that each object only appears once in the list.
When an object is destroyed, if it has multiple Observable bases, each will try to remove itself from the lists, and when it comes to the full list, only one will succeed -- but that's fine, because each is equally good as an arbitrary base of that object.
Some code follows.
Your full list of objects, iterable in as straightforward a fashion as std::map will allow. (Each void * and each Observable * is unique, but this uses the Observable * as the key, so that it's easy to remove the entry in the Observable destructor.)
typedef std::map<Observable *, void *> AllObjects;
AllObjects allObjects;
And your list of objects that have been constructed, but aren't yet added to allObjects:
std::set<Observable *> recentlyConstructedObjects;
In the Observable constructor, add the new object to the list of pending objects:
recentlyConstructedObjects.insert(this);
In the Observable destructor, remove the object:
// 'this' may not be a valid key, if the object is in 'allObjects'.
recentlyConstructedObjects.erase(this);
// 'this' may not be a valid key, if the object is in 'recentlyConstructedObjects',
// or this object has another Observable base object and that one got used instead.
allObjects.erase(this);
Before you're about to do your thing, update allObjects, if there've been any objects constructed since last time it was updated:
if(!recentlyConstructedObjects.empty()) {
std::map<void *, Observable *> newObjects;
for(std::set<Observable *>::const_iterator it = recentlyConstructedObjects.begin(); it != recentlyConstructedObjects.end(); ++it)
allObjectsRev[dynamic_cast<void *>(*it)] = *it;
for(std::map<void *, Observable *>::const_iterator it = newObjects.begin(); it != newObjects.end(); ++it)
allObjects[it->second] = it->first;
recentlyConstructedObjects.clear();
}
And now you can visit each object the once:
for(std::map<Observable *,void *>::const_iterator it = allObjects.begin(); it != allObjects.end(); ++it) {
// you can dynamic_cast<whatever *>(it->first) to see what type of thing it is
//
// it->second is good as a key, uniquely identifying the object
}
Well... now that I've written all that, I'm not sure whether this solves your problem. It was interesting to consider nonetheless.
(This idea would solve one of the problems with the curiously recurring template, namely that you have lots of base objects per derived object and it's harder to disentangle because of that. (Unfortunately, no solution to the large number of base classes, sorry.) Due to the use of dynamic_cast, of course, it's not much use if you call it during an object's construction, which is of course the advantage of the curiously recurring thing: you know the derived type during the base's construction.
(So, if your'e going with that style of solution, AND you are OK with performing your operations outside the construction/destruction stage, AND you don't mind the (multiple) base classes taking up space, you could perhaps have each base's constructor store some class-specific info -- using typeid, perhaps, or traits -- and merge these together when you build the larger list. This should be straightforward, since you'll know which base objects correspond to the same derived object. Depending on what you're trying to do, this might help you with issue 3.)
Take a look at Signals and Slots especially Boost Signals and Slots

C++ swap problem in inheritance scenario

I want to add swap functionality to two existing C++ classes. One class inherits from the other. I want each classes' instances to only be swappable with instances of the same class. To make it semi-concrete, say I have classes Foo and Bar. Bar inherits from Foo. I define Foo::swap(Foo&) and Bar::swap(Bar&). Bar::swap delegates to Foo::swap. I want Foo::swap to only work on Foo instances and Bar::swap to only work on Bar instances: I can't figure out how to enforce this requirement.
Here's a sample of what's giving me trouble:
#include <algorithm>
#include <iostream>
struct Foo {
int x;
Foo(int x) : x(x) {};
virtual void swap(Foo &other) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
std::swap(this->x, other.x);
};
};
struct Bar : public Foo {
int y;
Bar(int x, int y) : Foo(x), y(y) {};
virtual void swap(Bar &other) {
std::cout << __PRETTY_FUNCTION__ << " ";
Foo::swap(other);
std::swap(this->y, other.y);
};
};
void display(Foo &f1, Foo &f2, Bar &b34, Bar &b56)
{
using namespace std;
cout << "f1: " << f1.x << endl;
cout << "f2: " << f2.x << endl;
cout << "b34: " << b34.x << " " << b34.y << endl;
cout << "b56: " << b56.x << " " << b56.y << endl;
}
int main(int argc, char **argv)
{
{
Foo f1(1), f2(2);
Bar b34(3,4), b56(5,6);
std::cout << std::endl << "Initial values: " << std::endl;
display(f1,f2,b34,b56);
}
{
Foo f1(1), f2(2);
Bar b34(3,4), b56(5,6);
std::cout << std::endl << "After Homogeneous Swap: " << std::endl;
f1.swap(f2); // Desired
b34.swap(b56); // Desired
display(f1,f2,b34,b56);
}
{
Foo f1(1), f2(2);
Bar b34(3,4), b56(5,6);
std::cout << std::endl << "After Heterogeneous Member Swap: " << std::endl;
// b56.swap(f2); // Doesn't compile, excellent
f1.swap(b34); // Want this to not compile, but unsure how
display(f1,f2,b34,b56);
}
return 0;
}
Here's the output:
Initial values:
f1: 1
f2: 2
b34: 3 4
b56: 5 6
After Homogeneous Swap:
virtual void Foo::swap(Foo&)
virtual void Bar::swap(Bar&) virtual void Foo::swap(Foo&)
f1: 2
f2: 1
b34: 5 6
b56: 3 4
After Heterogeneous Member Swap:
virtual void Foo::swap(Foo&)
f1: 3
f2: 2
b34: 1 4
b56: 5 6
You can see in the final output group where f1.swap(b34) "sliced" b34 in a potentially nasty way. I'd like the guilty line to either not compile or blow up at runtime. Because of the inheritance involved, I think I run into the same problem if I use a nonmember or friend swap implementation.
The code is available at codepad if that helps.
This use case arises because I want to add swap to boost::multi_array and boost::multi_array_ref. multi_array inherits from multi_array_ref. It only makes sense to swap multi_arrays with multi_arrays and multi_array_refs with multi_array_refs.
Swap, like assignment and comparison work well with value types and don't work well with bases of class hierarchies.
I've always found it easiest to follow the Scott Meyer's Effective C++ recommendation of not deriving from concrete classes and making only leaf classes concrete. You can then safely implement swap, operator==, etc. as non-virtual functions for leaf nodes only.
While it's possible to have a virtual swap functions the whole point of having virtual base classes is to have dynamic behaviour at runtime so I think you're on to a loser trying to get all incorrect possibilities to fail at compile time.
If you want to go the virtual swap route, then one possible approach is to do something like this.
class Base
{
public:
virtual void Swap(Base& other) = 0;
};
class ConcreteDerived
{
virtual void Swap(Base& other)
{
// might throw bad_cast, in this case desirable
ConcreteDerived& cother = dynamic_cast<ConcreteDerived&>(other);bad_cast
PrivateSwap(cother);
}
void PrivateSwap(ConcreteDerived& other)
{
// swap implementation
}
};
(Somewhat hacky solution)
Add a protected virtual method, isBaseFoo(), make it return true in Foo, and false in Bar, the the swap method for Foo could check it's argument has isBaseFoo()==true.
Evil, and detects the problem only at run-time, but I can't think of anything better, although Charles Bailey's answer might be better, if you allow dynamic_cast<>.
You cannot really do this, but I don't see the point, anyway. It's no worse than slicing on operator= or copy constructors, and you cannot avoid the latter, either. Why should swap be any different?
For the same reason, it's likely not worth it making swap virtual, for the same reason why you don't make operator= virtual.
I think that this case scenario is now addressed by the presence of move semantics in C++11.
I usually use the swap function only to avoid copy duplication in the assignments so it's only used statically, by the derived class that needs to extend the swap function and that knows the static type of its base so there is no need for virtuality (which as it was stated can lead to subtle problems with slicing). In fact I declare the swap function as a protected method in my base to make sure it's not use directly anywhere else.
The most concrete class can then have the final version public if necessary.
What you are actually trying to do is swap instances of classes from a third party inheritance hierarchy. Given that, I'd forget about using swap on the actual classes and add a level of indirection. Using boost::shared_ptr is a good approach; use shared_ptr instances containing whatever class you want, and swap to your heart's content.
In general, solving the problem as you asked it at compile time is hard for all the reasons described by the other answerers.