method prolog/epilog

A

Andrey Tatarinov

Hi all!

Is it possible to create method epilog in C++?

It means that I want to define such code in base class, that would be
executed _after_ execution of overrided method in child class, without
manually execution, cause child should not know anything about it.
 
V

Victor Bazarov

Andrey said:
Is it possible to create method epilog in C++?

It means that I want to define such code in base class, that would be
executed _after_ execution of overrided method in child class, without
manually execution, cause child should not know anything about it.

I think the simple way is to define a public function in the base class
without making it virtual, then inside call a virtual function and also
perform all the epilogue activities:

class B {
public:
void foo(int i) {
real_foo(i); // calls derived's real_foo if it's overridden
epilogue(); // that's where the epilogue is
}

protected:
virtual void real_foo(int); // that's the one to be overridden
};

class D : public B {
protected:
virtual void real_foo(int); // whatever it should do
};

Now, if you want the epilogue to be also derived-class-specific,
make it a virtual function as well. Otherwise keep it only in B.

Victor
 
A

Andrey Tatarinov

Victor said:
I think the simple way is to define a public function in the base class
without making it virtual, then inside call a virtual function and also
perform all the epilogue activities:

Thanks, that seems to be solution in some cases, unfortunately I need
epilogue for base class constructor.
 
P

Phlip

Andrey said:
Thanks, that seems to be solution in some cases, unfortunately I need
epilogue for base class constructor.

You mean the base constructs, the derived class constructs, and the base
does one more thing?

Why? What's the actual scenario? You are asking about a solution without
stating the problem.

The simplest fix is for the derived class constructor to call a base class
method. Making classes easy to use right and hard to use wrong is a lofty
goal, but sometimes you must rely on a sternly worded comment.
 
R

Rolf Magnus

Victor said:
class B {
public:
void foo(int i) {
real_foo(i); // calls derived's real_foo if it's overridden
epilogue(); // that's where the epilogue is
}

protected:

Why protected?
 
V

Victor Bazarov

Rolf said:
Victor Bazarov wrote:




Why protected?

In general, to force the users of 'B' to always use 'foo' and not
be tempted to use 'real_foo' (to circumvent the epilogue call).
Private would prohibit derived classes to fall back on it if they
need to.

V
 
A

Andrey Tatarinov

Phlip said:
You mean the base constructs, the derived class constructs, and the base
does one more thing?
exactly

Why? What's the actual scenario? You are asking about a solution without
stating the problem.

consider I have generic data storage

class CData {};

it can do a lot of things, for example indexing of data, some data
transformation and so on.

I have derived class

class CBinData{};

that knows how to extract data from file. I want to fill data storage,
and after that init indexing and other one-time routines.
The simplest fix is for the derived class constructor to call a base class
method. Making classes easy to use right and hard to use wrong is a lofty
goal, but sometimes you must rely on a sternly worded comment.

That would be solution in case when I wont be able to create epilogue
for constructor.
 
T

tom_usenet

Thanks, that seems to be solution in some cases, unfortunately I need
epilogue for base class constructor.

Ahh, your stuck then. There's been a recent long thread on
"post-constructors" and "pre-destructors", and whether they should be
added to the language.

For now, just call the relevent code at the end of the derived class
constructor, or use a factory function that enforces this.

Tom
 
D

David Harmon

On Fri, 30 Jul 2004 18:54:09 +0400 in comp.lang.c++, Andrey Tatarinov
Thanks, that seems to be solution in some cases, unfortunately I need
epilogue for base class constructor.

There is an ongoing >100-post discussion on this topic in
comp.lang.c++.moderated; look for the thread with
Subject: Achieving virtualness from base class constructors

http://groups.google.com/[email protected]
 
A

Alf P. Steinbach

* Andrey Tatarinov:
consider I have generic data storage

class CData {};

it can do a lot of things, for example indexing of data, some data
transformation and so on.

I have derived class

class CBinData{};

that knows how to extract data from file. I want to fill data storage,
and after that init indexing and other one-time routines.

Perhaps CBinData can better be made independent and just _used_ by
CData.

Perhaps, if not, CBinData can pass a data extractor object up to
the CData constructor.

Those would both be clean solutions.
 
P

puppet_sock

Andrey Tatarinov said:
Thanks, that seems to be solution in some cases, unfortunately I need
epilogue for base class constructor.

I'm confused. Do you mean you want the base class to do some
of its own contstruction only after the child class is finished
doing its construction? If that's not the case, then the rest
of my post is crap and should be ignored.

But if it is, three things. First, I can't see any instance where
you would actually want to do that. And second, it's a good
trick if you can do it. And third, if I was a reviewer for such
code and I found this trick, I'd recommend getting rid of it.

It really sounds like you are attempting to do something the
hard way here. Maybe what you want is a ctor that takes a
parameter? Then you could use that parameter in an initializer
in the child class to specify appropriate construction of
the base class parts.
Socks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top