Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

enforcing function calling order

0 views
Skip to first unread message

ddt...@gmail.com

unread,
Sep 13, 2006, 11:43:44 AM9/13/06
to
Hello folks,

Just wanted to know if there are some 'standard' approaches to
enforcing an order in the invocation of calling functions. It is
usually needed when initializing some object.

e.g

class A
{
void setFunc1(int a);
void setFunc2(int a);
void setFunc3(int a);
};

now, when a user of this object initializes the object, i want to
enforce :
setFunc1 "called before" setFunc2 "called before" setFunc3

At a quick glance some form of solution that stores some flags
internally and raises exceptions come to mind. But has anyone tried
some particularly elegant solution that achieves the same ends?

Thanks in advance,
ddtbhai

mlimber

unread,
Sep 13, 2006, 12:08:02 PM9/13/06
to

Why can't you do initialize in the constructor? It's far better to have
the constructor throw and never have the chance of an invalid object
floating around (cf. http://www.artima.com/intv/goldilocks3.html).

Cheers! --M

Greg

unread,
Sep 13, 2006, 12:17:20 PM9/13/06
to
ddt...@gmail.com wrote:
> Hello folks,
>
> Just wanted to know if there are some 'standard' approaches to
> enforcing an order in the invocation of calling functions. It is
> usually needed when initializing some object.
>
> e.g
>
> class A
> {
> void setFunc1(int a);
> void setFunc2(int a);
> void setFunc3(int a);
> };
>
> now, when a user of this object initializes the object, i want to
> enforce :
> setFunc1 "called before" setFunc2 "called before" setFunc3

If there is some reason why these routines cannot be called from the
constructor - then the next best solution is to make all three private
and then declare one public function that calls the three private
methods in the "expected" order.

After all, if it is not obvious to the client of class "A" which method
has to be called before another, then there should be no requirement
that one has to be called before another. After all, how is the client
expected to know that a required order exists? The last thing any
programmer should want in their program is a set of unwritten
requirements that everyone working on the code is just expected to
"know".

Greg

ddt...@gmail.com

unread,
Sep 13, 2006, 12:50:58 PM9/13/06
to

Hi Greg, M,

The *main* issue that i was trying to avoid is the "you just know"
syndrome as Greg mentioned. In my example, each function has a single
argument, but this can also be arbitrarily long. Say each of the set
functions have 6 arguments each, then a single function that can order
the 3 invocations internally will end up with 18 arguments.

While there is nothing really wrong about that, it certainly isn't
pretty !

Which is why i was looking for an approach which can guarantee an order
of invocation, while barely being noticed by the end user of the class.

Best,
ddtbhai

Howard

unread,
Sep 13, 2006, 1:13:53 PM9/13/06
to

<ddt...@gmail.com> wrote in message
news:1158162221.4...@b28g2000cwb.googlegroups.com...

One way would be to first make sure those functions are private (they are in
the above example, but I doubt you meant them to be, since then you couldn't
call them from outside in the first place). Then, add a public function
which accepts the parameters required to call all three functions itself.

For example,

class A
{
private:
void Func1( int a );
int Func2( int b ); // returning value here, just as an example
void Func3( int c );
public:
void Funcs( int a, int b );
};

A::Funcs( int a, int b )
{
Func1( a );
int c = Func2( b ); // compute value needed for Func3
Func3( c );
}


-Howard

Julián Albo

unread,
Sep 13, 2006, 1:26:25 PM9/13/06
to
ddt...@gmail.com wrote:

> The *main* issue that i was trying to avoid is the "you just know"
> syndrome as Greg mentioned. In my example, each function has a single
> argument, but this can also be arbitrarily long. Say each of the set
> functions have 6 arguments each, then a single function that can order
> the 3 invocations internally will end up with 18 arguments.

Write a class that collects the arguments, and pass an instance of it as
constructor's argument. This has also the advantage that you can easily
establish default arguments.

--
Salu2

Bo Persson

unread,
Sep 13, 2006, 4:25:36 PM9/13/06
to

Or split the class into several separate objects.

What kind of class would need 18 parameters for construction?! This is
almost always a sign that the class does more that one thing. Why not
have a separate class for each of the different tasks?


Bo Persson


shadow...@comcast.net

unread,
Sep 13, 2006, 4:40:42 PM9/13/06
to
Bo Persson wrote:
> Julián Albo wrote:

> > Write a class that collects the arguments, and pass an instance of
> > it
> > as constructor's argument. This has also the advantage that you can
> > easily establish default arguments.
>
> Or split the class into several separate objects.
>
> What kind of class would need 18 parameters for construction?! This is
> almost always a sign that the class does more that one thing. Why not
> have a separate class for each of the different tasks?
>


How does that solve the problem of enforcing function calling order?

Julián Albo

unread,
Sep 13, 2006, 5:04:27 PM9/13/06
to
shadow...@comcast.net wrote:

By avoiding it.

--
Salu2

raxitsh...@yahoo.co.in

unread,
Sep 14, 2006, 12:34:08 AM9/14/06
to
ddtbhai,

do u want like this
//this is not c++ code...appropriate arguments and return type assumed
.

{
startup();
execute();
cleanup();
}


1. no one can able to call execute directly,
2. order of execution must be (start,execute,clean) or exception
throw.
3. Specify which function you want that some one can derived and able
to override, and all or directly specify which one is virtual/normal
and attribute like pub,protected,private.

It would help to understand your question...


chhers,
Raxit

aa

unread,
Sep 14, 2006, 9:49:57 AM9/14/06
to
ddt...@gmail.com wrote:
> class A
> {
> void setFunc1(int a);
> void setFunc2(int a);
> void setFunc3(int a);
> };
>
> now, when a user of this object initializes the object, i want to
> enforce :
> setFunc1 "called before" setFunc2 "called before" setFunc3

That sounds like a "finite state machine" to me (although a very simple
one). You could try smc (I think on sourceforge) or Boost.Statechart
or sth. similar.

I wouldn't use that for initialisation though, but many people already
stated that.


cheers,

aa

0 new messages