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 10 1740 dd*****@gmail.c om 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 dd*****@gmail.c om 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
Greg wrote:
dd*****@gmail.c om 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
<dd*****@gmail. comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.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 dd*****@gmail.c om 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
Julián Albo wrote:
dd*****@gmail.c om 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
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? sh**********@co mcast.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
ddtbhai,
do u want like this
//this is not c++ code...appropri ate 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,p rivate.
It would help to understand your question...
chhers,
Raxit This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Penn Markham |
last post by:
Hello all,
I am writing a script where I need to use the system() function to call
htpasswd. I can do this just fine on the command line...works great
(see attached file, test.php). When my webserver runs that part of the
script (see attached file, snippet.php), though, it doesn't go through.
I don't get an error message or anything...it just returns a "1"
(whereas it should return a "0") as far as I can tell. I have read the
PHP...
|
by: |
last post by:
Tables:
=====
User
------
ID
Name
NationID
StateID
|
by: Foxy Kav |
last post by:
Hi everyone, im a first year UNI student doing a programming subject
and im stuck on how to get rid of my global variables, char
stringarray and char emptystring. I was wondering if
anyone could show me a example of how to do this. I was told to pass
the arrays from function to function, but i do not know how ....
Thanxs if you can help.
My code (First two functions only - there's more):
|
by: Muthu |
last post by:
I've read calling conventions to be the order(reverse or forward) in
which the parameters are being read & understood by compilers.
For ex. the following function.
int Add(int p1, int p2, int p3);
The parameters here can be read either in the forward order from p1
till p3 or reverse order from p3 till p1.
Can anyone explain what is the advantage/disadvantage of either of
|
by: Zac Panepucci |
last post by:
Hello There,
I am using the following constructs to parse named arguments:
function whatever() {
if (this.yo) {
if(this.yo=='man')
alert('wuzup');
else
| |
by: kernelxu |
last post by:
hi,everybody.
I calling function setbuf() to change the characteristic of standsrd
input buffer.
some fragment of the progrem is:
(DEV-C++2.9.9.2)
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char buf = {0};
|
by: Thelma Lubkin |
last post by:
I have a form w/ 11 identical text boxes in each of which user can
enter a number between 1 and 11. Each defaults to 0, which I use to
flag empty. Each has the 1 to 11 restriction as its validation rule,
but I also need to enforce no duplicates among them. I haven't been
able to figure out how to do this in the validation rule, so I have a
function that gets as a parameter a number to identify which textbox
is calling it that handles that...
|
by: John Friedland |
last post by:
My problem: I need to call (from C code) an arbitrary C library
function, but I don't know until runtime what the function name is,
how many parameters are required, and what the parameters are. I can
use dlopen/whatever to convert the function name into a pointer to
that function, but actually calling it, with the right number of
parameters, isn't easy.
As far as I can see, there are only two solutions:
1) This one is portable. If...
|
by: william |
last post by:
My situation is here:
an array of two dimension can only be defined locally within a
function(because the caller don't know the exact size ). Then the
question is: how should the caller access this array for future use?
The code is:
**********************
caller()
{
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |