Thanks for your patients Weaknessforcats,
I wouldn't use the this pointer in the constructor initializer list.
I guess anything that generates a warning has to be questionable and so I guess I shouldn't use the
this pointer in the constructor initializer list either.
What you are trying to do is get the derived address into the base object.
Does this mean getting the derived address of the App_Class object into the base objects of A_Class and B_Class? (Although App_Class is not a derived class, neither is A_Class or B_Class).
The Visitor uses a base method that has a Visitor* argument. That base method is overridden by a derived method that also takes a Visitor* argument.
The derived method calls a Visitor->VisitDerived(this).
VisitDerived is a derived class of Visitor.
So I make a Visitor class which is derived from some base class that has a method with a Visitor* argument. Then the Visitor class should overwrite this method?
Should my Visitor class be derived from App_Class or from A_Class/B_Class?
Should the VisitDerived class be the App_Class or the A_Class/B_Class or is this some completely different class?
So you end up in a VisitDerived object with the address of the derived object in the other hierarchy.
So VisitDerived should be the A_Class/B_Class thus ending up with the address of the derived object in the other hierarchy (APP_Class)? That's what I want right?
So in my case I would want to create a Visitor class and derive it from App_Class. I would need to put into the App_Class an empty method that takes an argument Visitor* and then in the Visitor class I should overwrite this method.
So far I have this:
- class Visitor; // forward reference
-
-
class A_Class {
-
public:
-
void someFunctionsInA();
-
};
-
-
class B_Class {
-
public:
-
void someFunctionsInB();
-
};
-
-
class App_Class {
-
public:
-
A_Class a;
-
B_Class b;
-
-
LogFile log;
-
-
bool writeToLogFile(string s);
-
-
void runVisitor(Visitor *v) {};
-
};
-
-
class Visitor : public App_Class {
-
void runVisitor(Visitor *v) {
-
// Not sure what to put in here.
-
// v->writeToLogFile(s)
-
};
-
};
This compiles ok. The next thing I thought I was to do was to make the A_Class and B_Class derived from Visitor but I can not do this. It seems that I am creating a circle of declarations. I must have miss-understood something.
lets have a look at the example ...
Say you have a Car derived form Vehicle and you suddenly need to do a cost analysis on various Cars but there is no cost in formation in the Car class.
Or in my case: I have a class A_Class that is not derived from anything and I suddenly need to record its progress in a log file but there is no log file function in the A_Class class.
You write a CarCostAnalyzer class (derived from Visitor) and you call the Car object using Vehicle->Visit(CarCostAnalyzer*).
Ah! So I should write a completely new class? called logFileInterface and derive this from Visitor (is this the VisitDerived class mentioned above?) which in turn is derived from App_Class? But then I can't call A_Class->runVisitor(logFileInterface*) because the A_Class isn't a Visitor object.
...
I hope you can see why I am getting confused. I am sure that once I figure this out it will all suddenly become clear. I will keep on trying to figure this out by reading your comments above and the HowTo post on C++ Visitors.