|
-
February 28th, 2008, 05:05 AM
#1
Inheritance: Ensure call of base fucntion from derived class
Hi,
I´ve got two classes: Base and Derived. Base offers some virtual (non pure virtual method) that are overwritten by Derived. Is it possible to ensure that Derived calls Base function from the overwritten Derived function?
Here´s an example:
Code:
class Base
{
public:
Base()
{
}
virtual Base()
{
}
virtual void func1()
{
// do some stuff
}
virtual void func2()
{
// do some more stuff
}
};
class Derived1 : public Base
{
public:
Derived1()
{
}
void func1()
{
// OK, calls Base func
Base::func1();
// do some Derived specific stuff
}
void func2()
{
// oops, forgot to call Base::func2()
// do some Derived specific stuff
}
};
- Guido
-
February 28th, 2008, 05:10 AM
#2
Re: Inheritance: Ensure call of base fucntion from derived class
Perhaps this will work:
Code:
class Base
{
public:
Base()
{
}
virtual ~Base() {}
void func1()
{
// do some stuff
func1_();
}
void func2()
{
// do some more stuff
func2_();
}
protected:
virtual void func1_()
{
// do nothing
}
virtual void func2_()
{
// do nothing
}
};
class Derived1 : public Base
{
public:
Derived1()
{
}
protected:
void func1_()
{
// do some Derived specific stuff
}
void func2_()
{
// do some Derived specific stuff
}
};
The idea is that Base provide hooks for the Derived class to implement.
-
February 28th, 2008, 05:36 AM
#3
Re: Inheritance: Ensure call of base fucntion from derived class
Great suggestion, I think it will fit my needs.
Thank you
PS:
Unfortunately I have no reputation to spread at the moment.
- Guido
-
February 28th, 2008, 06:24 AM
#4
Re: Inheritance: Ensure call of base fucntion from derived class
Same here.. What do I have to do to get reputation to spread?
Dont forget to rate my post if you find it useful.
-
February 28th, 2008, 06:30 AM
#5
Re: Inheritance: Ensure call of base fucntion from derived class
Look out for good posts by various other people and add to their reputation
-
February 29th, 2008, 08:30 AM
#6
Re: Inheritance: Ensure call of base fucntion from derived class
Hi again,
I gave some thought to your suggestion, laserlight, and find it very useful for flat hierachies with only one level of inheritance. How do I implement it for two or more levels, is it possible at all?
- Guido
-
February 29th, 2008, 08:34 AM
#7
Re: Inheritance: Ensure call of base fucntion from derived class
I gave some thought to your suggestion, laserlight, and find it very useful for flat hierachies with only one level of inheritance. How do I implement it for two or more levels, is it possible at all?
EDIT:
Okay, I did some experimentation, and I think that one cannot really prevent the problem elegantly. Perhaps the best you can do is to provide yet another hook function, and specify that subclasses should implement that instead of the original hook function from the base class.
Last edited by laserlight; February 29th, 2008 at 08:55 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|