By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,387 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,387 IT Pros & Developers. It's quick & easy.

enforcing function calling order

P: n/a
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

Sep 13 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
dd*****@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

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?
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

Sep 13 '06 #2

P: n/a
dd*****@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

Sep 13 '06 #3

P: n/a

Greg wrote:
dd*****@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
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

Sep 13 '06 #4

P: n/a

<dd*****@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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?
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

Sep 13 '06 #5

P: n/a
dd*****@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
Sep 13 '06 #6

P: n/a
Julián Albo wrote:
dd*****@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.
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
Sep 13 '06 #7

P: n/a
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?

Sep 13 '06 #8

P: n/a
sh**********@comcast.net 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?
By avoiding it.

--
Salu2
Sep 13 '06 #9

P: n/a
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

Sep 14 '06 #10

P: n/a
aa
dd*****@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

Sep 14 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.