Inheritance and Function Arguments - c++

My book covered superficially many test topics from the Object-Oriented domain and I need some explanation and a hint which book does cover such topics.
There's a test question:
#include <iostream>
class A {
public:
virtual void f(int n=2) {// takes the argument value from here
std::cout << n+1 << " in A";
}
};
class B : public A {
private:
virtual void f(int n=3) {
std::cout << n - 1 << " in B";// this class is used
}
};
int main() {
A* p = new B;
p->f();
}
Output:
1 in B, that is the argument from A fed into B.
I understand why the object of the class B is created. I don't understand why is the value of 2 taken as the argument. I also checked if the class A's value would be taken if the variable's name were changed -- it is still used.
Any literature to read about this or educational videos on such OOP topics is welcome.

You have more info here : Can virtual functions have default parameters?
The idea is "the static type of p is A so it will use the default value of A::f"
IMHO, it's a detail and probably not what you should be focusing if you are learning OOP, but it's great you did see it.

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"
}

Creating a class member that is automatically calculated from other class members?

I'm an absolute newbee when it comes to programming and I'm trying to teach myself the basics by just solving some easy "problems" in C++.
I have searched the web for an exact answer to my question before posting it here and haven't found one so far, however that may be because of (1).
So, what I'm looking for is a way to declare a class member that gets automatically calculated from other members of the same class, so that the calculated class member can be used just like an explicitly defined class member would. For example imagine a struct called creature that has the properties/members creature.numberofhands, creature.fingersperhand and finally the property creature.totalfingers that automatically gets calculated from the above members.
Heres an example of the closest I got to what I wanted to achieve:
#include <iostream>
typedef struct creature {
int numberofhands;
int fingersperhand;
int totalfingers();
} creature;
int creature::totalfingers()
{
return numberofhands * fingersperhand;
};
int main()
{
creature human;
human.numberofhands = 2;
human.fingersperhand = 5;
printf("%d",human.totalfingers());
return(0);
}
What's really annoying me about this, is that I have to treat the calculated one DIFFERENTLY from the explicitly defined ones, i.e. I have to put "()" after it.
How can I change the code, so I can use: human.totalfingers without ever explicitly defining it?
The simplest option would be to use public member functions and make the actual properties hidden.
Something like this:
class Creature {
public:
Creature(int numhands, int fingersperhand) // constructor
: m_numhands{numhands}, m_fingersperhand{fingersperhand}
{ }
int fingersPerHand() const { return m_fingersperhand; }
int numberOfHands() const { return m_numhands; }
int totalFingers() const { return numberOfHands() * fingersPerHand(); }
private:
const int m_numhands;
const int m_fingersperhand;
};
The private member variables are an implementation detail. Users of the class just use the three public member functions to get the different number of fingers after construction and don't need to care that two of them are returning constant stored numbers and the third returns a calculated value - that's irrelevant to users.
An example of use:
#include <iostream>
int main()
{
Creature human{2, 5};
std::cout << "A human has "
<< human.totalFingers() << " fingers. "
<< human.fingersPerHand() << " on each of their "
<< human.numberOfHands() << " hands.\n";
return 0;
}
If - as per your comment - you don't want to use a constructor (although that's the safest way to ensure you don't forget to initialize a member), you can modify the class like this:
class CreatureV2 {
public:
int fingersPerHand() const { return m_fingersperhand; }
int numberOfHands() const { return m_numhands; }
int totalFingers() const { return numberOfHands() * fingersPerHand(); }
void setFingersPerHand(int num) { m_fingersperhand = num; }
void setNumberOfHands(int num) { m_numhands = num; }
private:
// Note: these are no longer `const` and I've given them default
// values matching a human, so if you do nothing you'll get
// human hands.
int m_numhands = 2;
int m_fingersperhand = 5;
};
Example of use of the modified class:
#include <iostream>
int main()
{
CreatureV2 human;
std::cout << "A human has "
<< human.totalFingers() << " fingers. "
<< human.fingersPerHand() << " on each of their "
<< human.numberOfHands() << " hands.\n";
CreatureV2 monster;
monster.setFingersPerHand(7);
monster.setNumberOfHands(5);
std::cout << "A monster has "
<< monster.totalFingers() << " fingers. "
<< monster.fingersPerHand() << " on each of their "
<< monster.numberOfHands() << " hands.\n";
CreatureV2 freak;
freak.setFingersPerHand(9);
// Note: I forgot to specify the number of hands, so a freak get
// the default 2.
std::cout << "A freak has "
<< freak.totalFingers() << " fingers. "
<< freak.fingersPerHand() << " on each of their "
<< freak.numberOfHands() << " hands.\n";
return 0;
}
Note: all of the above assumes you are using a modern C++14 compiler.
What you have described is one of the reasons why encapsulation and "member variables should be private" is the recommended way of doing things in C++.
If every variable is accessed through a function, then everything is consistent, and refactoring from a member variable to a computation is possible.
Some languages, like C# or D, have the concept of "properties", which provide a way around the issue, but C++ does not have such a construct.
For fun, the proxy way to avoid extra parenthesis, (but with some extra costs):
class RefMul
{
public:
RefMul(int& a, int& b) : a(a), b(b) {}
operator int() const { return a * b; }
private:
int& a;
int& b;
};
struct creature {
int numberofhands;
int fingersperhand;
RefMul totalfingers{numberofhands, fingersperhand};
};
Demo
Note: to use RefMul with printf, you have to cast to int:
printf("%d", int(human.totalfingers));
That cast would not be required if you use c++ way to print:
std::cout << human.totalfingers;
If you're after consistency, you can make your changes the other way around. Replace the two member variables with constant methods which simply return copies of the member variables. That way, the way you access data is consistent and you don't have to worry about some code changing the values of the member variables when it shouldn't.
Others have provided very good answers. If you are looking for consistency, probably the easiest way is to make use of member functions (as #Jesper Juhl has answered).
On the other hand, if you strictly want to use class members that are calculated automatically from other members, you can use properties. Properties (as are defined in C# and Groovy) are not a standard feature of C++ but there are ways to implement them in C++. This SO question has a very good overview of the ways that properties can be defined and used in C++. My favorite way of defining properties is taking advantage of Microsoft-specific extension for properties in Visual C++ (obviously, this approach is specific to Microsoft Visual C++). A documentation of properties in Visual C++ can be found in MSDN. Using properties in Visual C++, your code can be modified to:
struct creature {
int numberofhands; // use of public member variables are generally discouraged
int fingersperhand;
__declspec(property(get = get_totalfingers)) // Microsoft-specific
int totalfingers;
private:
int fingers;
int get_totalfingers()
{
return numberofhands * fingersperhand; // This is where the automatic calculation takes place.
}
};
This class can be used like this:
#include <iostream>
int main()
{
creature martian;
martian.numberofhands = 2;
martian.fingersperhand = 4; // Marvin the Martian had 4!
// This line will print 8
std::cout << "Total fingers: " << martian.totalfingers << std::endl;
return 0;
}
As I said earlier, properties are not a standard feature of C++ but there are ways to get them in C++ which either rely on smart tricks or using compiler-specific features. IMHO, using simple functions (as #Jesper Juhl described) is a better alternative.

C++ Member to member function pointers OR event driven programming

I come from the C# world so events and event handlers are everyday stuff.
Recently i have been studying about wxWidgets
I've been googling about this for a week now and i have found that most C++ programmers hardly understand the term "event" or "callback", perhaps it's ambiguous in the world of C++.
Here's a simple example of the event model.
class A{
public:
A(){
child.MySuperClickEvent = this.HandleSuperClick;
}
private:
B child;
void HandleSuperClick(B child){
// do stuff
}
}
class B{
public:
/*TBA*/ MySuperClickEvent;
private:
void ClickPrivate(){
MySuperClickEvent(this);
}
}
class B has it's own designated purpose and when finished it triggers an event.
The idea is that class B shouldn't have any knowledge of it's domain, making it more reusable.
I have read about std::function<> and function pointers. which all seems to take examples in handling static functions but as soon as were talking "Member to Member" things starts to get greasy.
So summarize, the question is simple. How do i make a member function pointer that can be assigned to a member externally?
There are a great many ways to implement such things, varying in terms of memory overheads, how many callbacks are supported, thread safety, use of e.g. weak_ptrs to see if the objects involved still exist etc..
Just as a taste to get you started - here's a simple single-threaded "observer" implementation doing basically what you ask for in the question:
#include <iostream>
#include <list>
struct Observer
{
virtual ~Observer() { }
virtual void on_event_x() { }
};
class Observable
{
public:
void add_observer(Observer& o)
{
observers_.push_back(&o);
}
void do_some_stuff()
{
std::cout << "before 1st event\n";
fire_event_x();
std::cout << "between 1st & 2nd events\n";
fire_event_x();
std::cout << "after 2nd event\n";
}
private:
std::list<Observer*> observers_;
void fire_event_x()
{
for (auto& observer : observers_)
observer->on_event_x();
}
};
struct My_Observer : Observer
{
My_Observer(int id) : id_(id) { }
void on_event_x() override
{
std::cout << "My_Observer::on_event_x() id " << id_ << '\n';
}
int id_;
};
int main()
{
My_Observer my_observer_1 { 1 };
My_Observer my_observer_2 { 2 };
Observable x;
x.add_observer(my_observer_1);
x.add_observer(my_observer_2);
x.do_some_stuff();
}
Runtime output:
before 1st event
My_Observer::on_event_x() id 1
My_Observer::on_event_x() id 2
between 1st & 2nd events
My_Observer::on_event_x() id 1
My_Observer::on_event_x() id 2
after 2nd event
If that doesn't suit you functionally, please say exactly why.
As this question is tagged wxWidgets, let me answer the more narrow variant of it, i.e. how to use member function as event handler in wxWidgets:
This is done with the help of Bind() method which can be used with a member function directly (you pass the pointer to the function itself and the object to call it on) or any arbitrary functor, i.e. anything that can be called using the standard function call syntax, such as an std::function<> object which, in turn, can be used to store any callable object.

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;

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