| re: calling parent virtual function wtih out calling it
scott wrote:
[color=blue]
> hi all, hope some one can help me. Ill try and explain what im trying to do
> as best i can.
>
> i have a parent class that has a vertual function, lets call it virtual int
> A(). That vertual function does somthing that must be done. This meens
> that when a child class inherits the class and creates its own vertual int
> A() the parent class must also be called. the prob is i can not use the
> base class name and then its functino name after it from with in the child
> class to call it. is there any way to make it so that all the verutal
> functions that are made are exectued. The reson its a verutal function is
> becase i only have the memmory address of the parent class but i need to
> call the highest child classes function. this meens that when i call the
> parents class verutal function it actualy calls the highest childs vertual
> funtion instead. But then i want it to call all its parent function in turn
> after it.
>
> Hope what iv said makes sence and hope some one can help.
>
> Thx for any help given.
>
> Scott.
>
>[/color]
Take a look at the Template Method pattern. The basic idea is that the
base class has a function that looks something like this:
class C{
public:
void do_something(){
set_up_state();
vfunc();
clean_up();
}
virtual vfunc() = 0;
};
class Q: public C{
public:
virtual vfunc(){
// ...
}
};
In your code you then instantiate a Q object and call its do_something()
function. That function can do all the pre/post process that the base
class requires, with a call to vfunc() in the middle.
Just for the record, there shouldn't be any problem with calling the
parent's virtual function from the child. What error were you seeing?
HTH,
Jacques. |