Sign in to follow this  
smitty1276

C++ Method-level inheritance?

Recommended Posts

I'm looking for an elegant way to force all methods of a class to execute some other function before executing (if anyone's used Ruby on Rails, something like before and after filters for actions). Of course, this is C++ so I might have to just explicitly call the function at the top of each new method, but I'd like to get around that if possible. Does anyone know of any libraries that allow for that sort of thing? Maybe some of the boost function libraries? I don't know alot about boost, but it seems to do almost everything. :-)

Share this post


Link to post
Share on other sites
I'm not sure of the ramifications of it but you might be able to define within the base class, two functions. One with the before stuff that you want to happen and the other a pure virtual that is called after in the primary function.


class Base
{
public:
void DoSomething()
{
// The before stuff goes here.
ReallyDoSomething();
}

protected:
virtual void ReallyDoSomething() = 0;
}

class Derived : public Base
{
protected:
void ReallyDoSomething()
{
// The stuff after the DoSomething
}
}

Derived d;
d.DoSomething();


I haven't used C++ in a while and I don't even know if it'll compile. Just thought I'd throw it out there to see if it helps. I'm sure someone will correct me pretty quickly though. [wink]

Share this post


Link to post
Share on other sites
Instead of making the public method the one which is virtual make a protected method which is and then call that from a normal public function. The protected method is then the one which is overridden in the sub-class



class foo
{
public:
void doStuff() { doSomethingElse(); doStuffImpl(); doEnding();};
protected:
virtual void doStuffImpl() = 0; // Must override me!
}

class bar : public foo
{
protected:
virtual void doStuffImpl() { /* code here */};
}



I'm pretty sure that's how I've done it in the past.

Share this post


Link to post
Share on other sites
You have to call methods explicitly. Is this for an inheritance situation, or just something you want to do for every function in a particular isolated class? If its just the one class, just call the methods manually -- you may want to leverage some kind of RAII-based invoker object that you simply declare at the top of each function -- if the invoker object calls your "on enter" function in its constructor and your "on exit" function in the destructor, it will make the whole thing a bit cleaner, especially if you have multiple exit points (such as via exceptions).

If you're talking about an inheritance situation where you want to make sure any subclass of the base always calls a certain function first and last in any overridden virtual methods, the solution is generally to expose a non-virtual public form of the function, which itself calls the required on-entry function, then the virtual method, then the on-exit function. This frees the subclasser from having to remember to do this.

Can you clarify further, if neither of these suggestions are appropriate?

Share this post


Link to post
Share on other sites
You can't do it with default C++ syntax, but you can do it if you have some syntactic sugar wrapped around it:

class shielded_interface {
public:
// whatever you want to expose
};
class public_interface {
public:
virtual shielded_interface& access();
virtual shielded_interface const& access() const;
};

class public_interface_impl:
public virtual public_interface,
private virtual shielded_interface
{
private:
virtual void pre_access() const {
// do nothing by default
};
virtual void pre_access() {
public_interface_impl const& tmp = *this;
tmp.pre_access();
};
public:
virtual shielded_interface& access() {
pre_access();
return *this;
};
virtual shielded_interface const& access() const
{
pre_access();
return *this;
};
};



now, your class interface inherits from public_interface, and your class implementation from public_interface_impl.

Whenever someone wants to call your shielded_interface functions, they first have to call the method "access", which calls pre_access() before each call.

You can override pre_access (either the const or non-const versions -- I set it up so the non-const calls the const version if you don't override it) and have it do whatever you want.

post_access is a bit trickier, and it might require you to design your own custom calling convention to do it practically.

Share this post


Link to post
Share on other sites
Okay, cool, I'll give that a try. It would be nice if there was a way to make all functions call the "pre_access" function without having to define a "wrapper" function for each new real function, but I suppose that's as close as I'll get.

Thanks guys!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this