473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

method prolog/epilog

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.
Jul 22 '05 #1
10 2239
Andrey Tatarinov wrote:
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
Jul 22 '05 #2
Victor Bazarov wrote:
Is it possible to create method epilogue in C++?


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.
Jul 22 '05 #3
Andrey Tatarinov wrote:
Victor Bazarov wrote:
Is it possible to create method epilogue in C++?


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.


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.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 22 '05 #4
Victor Bazarov wrote:
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?
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.


Jul 22 '05 #5
Rolf Magnus wrote:
Victor Bazarov wrote:

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?


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.

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.



V
Jul 22 '05 #6
Phlip wrote:
Is it possible to create method epilogue in C++?

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.


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.
Jul 22 '05 #7
On Fri, 30 Jul 2004 18:54:09 +0400, Andrey Tatarinov
<el********@dezcom.mephi.ru> wrote:
Victor Bazarov wrote:
Is it possible to create method epilogue in C++?


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.


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
Jul 22 '05 #8
On Fri, 30 Jul 2004 18:54:09 +0400 in comp.lang.c++, Andrey Tatarinov
<el********@dezcom.mephi.ru> wrote,
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/gr*********...ws.hevanet.com

Jul 22 '05 #9
* Andrey Tatarinov:
Phlip wrote:
>Is it possible to create method epilogue in C++?

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.


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.


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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #10
Andrey Tatarinov <el********@dezcom.mephi.ru> wrote in message news:<2m************@uni-berlin.de>...
[snip]
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
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Greg Scharlemann | last post by:
I have set up Apache Axis with a Resin server. Everything seems to be set up correctly, axis will validate if I display the http://axis.scharlemann.com/axis/happyaxis.jsp file. However when I...
0
by: Dominique | last post by:
I am trying to communicate to a prolog server from a java client, however even though the connection is successfully made every time I try to perform QueryExecute I get an error, either Socket...
1
by: Ahmad Jalil Qarshi | last post by:
Hi! I want to use Prolog code in my VB or C# application. but dont know how to make it possible to communicate with Prolog code from VB or C#. if some one know about that, it will help me a...
0
by: Craig Watt | last post by:
Please Help! I have an XML File using the DataPacket standard (am working with Delphi/Informix poeple): <?xml version="1.0" standalone="yes"?> <DATAPACKET version="2"> <METADATA> <FIELDS>...
13
by: RK | last post by:
I apologize if this is a stupid question, I'm asking Python group for perspective on Ruby, but I don't see how the alternative of going to a ruby group for a perspective on Ruby is going to do me...
7
by: Neo | last post by:
I want to log information for a function call like void Add(void) { } When above function gets called, it should output strings like Entering Add Exiting Add
4
by: Neo | last post by:
I want to log information for a function call like void Add(void) { } When above function gets called, it should output strings like Entering Add
39
by: CJM | last post by:
I'm in the process of partially revamping a corporate website. My main brief was to reorganise much of the content and to update a lot of the copy, but in the process I'm also trying to correct...
4
Dormilich
by: Dormilich | last post by:
Hi, I'm trying to save a xhtml snippet to file. Is there a way besides fwrite() to save an xml string without the xml prolog (e.g. DOMDocument::save() automatically inserts the xml prolog)? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.