473,804 Members | 3,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A good use of fn ptrs?

Is this a good use of fn ptrs?

#include <iostream>
#include <cstdlib>
using namespace std;

typedef void (*v_v_fptr)();

void Error(){cout << "Error" << endl;}

void Success(){cout << "Success" << endl;}

v_v_fptr select(bool arg){if(arg==tr ue)return Success; else return
Error;}

int main()
{
bool val;
cout << "1|0?: " << endl;
cin >> val;
v_v_fptr ptr=select(val) ;
ptr();
system("PAUSE") ;
return 0;
}

Or should I use fn objs instead? Thanks!!!

Dec 17 '05
14 1477
"Protoman" <Pr**********@g mail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Why use exceptions? This is just an examp; in a real prog, I'd use it
to determine what fn to call based on the val of a var.


Well, if you're alread evaluating a variable to determine which function to
call... why not just call the function there instead?

Instead of:
v_v_fptr ptr=select(val) ;
ptr();

Just have:
select(val);

and have your select function do:

void select(bool arg)
{
if(arg==true)
cout << "Success" << endl;
else
cout << "Error" << endl;
}

Or, if you want to keep your success and error functions call them instead.

void select(bool arg)
{
if(arg==true)
Success();
else
Failure();
}

I don't see that you are gaining anything by returning a function to call,
then just calling the function. You are just adding an (error prone) level
of complexity for no good reason.

Just because you *can* do something doesn't mean you *should*.
Dec 18 '05 #11

Jim Langston wrote:
"Protoman" <Pr**********@g mail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Why use exceptions? This is just an examp; in a real prog, I'd use it
to determine what fn to call based on the val of a var.


Well, if you're alread evaluating a variable to determine which function to
call... why not just call the function there instead?

Instead of:
v_v_fptr ptr=select(val) ;
ptr();

Just have:
select(val);

and have your select function do:

void select(bool arg)
{
if(arg==true)
cout << "Success" << endl;
else
cout << "Error" << endl;
}

Or, if you want to keep your success and error functions call them instead.

void select(bool arg)
{
if(arg==true)
Success();
else
Failure();
}

I don't see that you are gaining anything by returning a function to call,
then just calling the function. You are just adding an (error prone) level
of complexity for no good reason.

Just because you *can* do something doesn't mean you *should*.


I was just doing it to learn about returning fn ptrs from fns; in a
real prog I would definitely not do this unless I had a *very good*
reason.

Dec 18 '05 #12
Gianni Mariani wrote:
Ben Pope wrote:
Gianni Mariani wrote: ...
Is attempting to open a file that is not there an exception or an error?

If the file was expected to exist, or if the file was necessary for
the program to operate, then almost certainly an exception.

If the filename was entered by the user, then probably an error,
although that error could easily be propagated up the stack by an
exception...

As is often the case, it depends!


So now, exceptions are glorified cross function gotos. Probably not a
good thing.


Quite different to a goto.

When a failure occurs, it is often the case that you do not know how to
handle the error, and so you inform your caller. If you do it via error
codes, then the caller has to check the return value, and decide what to
do, possibly informing its caller via a return code.

This results in lots of checks of calls into function for errors that
rarely occur, and manually propagating the error up the call stack.

With exceptions, you can short-cut the call up the stack. This means
you deal with the error when you know how to do so.
I was on the fence for quite a while on exceptions and I'm now leaning
on a very minimal usage of exceptions. Opening a file is in almost
every case I have come across, not a case for using exceptions, even if
you expect the file to exist.
Often, yes. If for example the filename was provided on the command
line, to the program, and you do a bunch of stuff, then try to open the
file and it doesn't exist, rather than manually propagate the error up
the call stack with return codes you can just throw, and then catch it
near the top, and inform the user they can't spell.
It's also very unfortunate that exception specifiers are dynamically
handled rather than allowing the compiler to do a static analysis.
Yes, I would have preferred to have to explicitly specify anything that
a function could throw, and for the compiler to provide static analysis:

#include <iostream>

void fun1() {
throw "foo";
}
void fun2() throw() {
throw "bar";
}
void fun3() throw() {
fun1();
}

int main() {
try {
fun1();
} catch (char* e) {
std::cout << "This is expected to fail: " << e << std::endl;
}
try {
fun3();
} catch (char* e) {
std::cout << "This is NOT expected to fail: " << e << std::endl;
}

}

It's a shame that more analysis is not done to ensure that fun3 does not
throw. VC8 warns that fun2 throws, but does not warn that fun3 calls a
function that throws.
Multithreaded code is almost always impossible to manage using exceptions.
Surely you have to manage throwing, anyway?
I'm not trying to say exceptions are bad, I'm saying they're very
limited in utility. Also, I find it a fallacy that code is easier to
write. Handling exceptions correctly can get quite involved making the
code much more difficult to write if you have to write try/catch blocks
everywhere.
Exceptions are for exceptional situations, things which are really bad,
things you don't really know how to deal with. If you write code to
deal with it, it means you expect it to happen, in which case it may not
be best to consider it an exceptional situation.

The normal process of handling errors via return codes is often the most
practical way of dealing with an error, especially if the caller of the
function experiencing the error can deal directly with it.

If it really is exceptional, it's likely that you can't deal with it
directly at the call site, you may have to get 10 calls up the stack to
deal with it, so it's often more manageable to throw.
Anyhow, I'm sure that there are plenty o people who would debate this,
and I'm looking forward to a constructive one.


It could be interesting!

Ben Pope.
Dec 19 '05 #13

Ben Pope wrote:
Exceptions are for exceptional situations, things which are really bad,
things you don't really know how to deal with. If you write code to
deal with it, it means you expect it to happen, in which case it may not
be best to consider it an exceptional situation.

The normal process of handling errors via return codes is often the most
practical way of dealing with an error, especially if the caller of the
function experiencing the error can deal directly with it.

If it really is exceptional, it's likely that you can't deal with it
directly at the call site, you may have to get 10 calls up the stack to
deal with it, so it's often more manageable to throw.


You could also find fully expected error situations arising several
levels down the call stack from where they need to be handled simply
because you have sensibly factored your code in many small functions
rather than few larger ones.

In such cases, even though the 'error' might be expected I find
exceptions a much cleaner control structure than having to propogate an
error code up the call stack manually. Debating the definitions of
'error situation' and 'exceptional situation' seems academic to me when
what I am really choosing between is clean code vs cluttered code.

Gavin Deane

Dec 19 '05 #14
Geo
Gianni Mariani wrote:
Ben Pope wrote:
Gianni Mariani wrote: ...
Is attempting to open a file that is not there an exception or an error?

If the file was expected to exist, or if the file was necessary for the
program to operate, then almost certainly an exception.

If the filename was entered by the user, then probably an error,
although that error could easily be propagated up the stack by an
exception...

As is often the case, it depends!


So now, exceptions are glorified cross function gotos. Probably not a
good thing.


while, for if..else.. return, break, continue... are ALL glorified
gotos, you advising against those also ?
I was on the fence for quite a while on exceptions and I'm now leaning
on a very minimal usage of exceptions. Opening a file is in almost
every case I have come across, not a case for using exceptions, even if
you expect the file to exist.

It's also very unfortunate that exception specifiers are dynamically
handled rather than allowing the compiler to do a static analysis.
Possibley, as they stand they are useless, but I'm not conviced they
have any worth anyway.

Multithreaded code is almost always impossible to manage using exceptions.

What's that got to do with anything, C++ is NOT a multithreaded
language, threading is not part of C++, exceptions are, you would be
more correct to say, use exceptions, don't use threads
I'm not trying to say exceptions are bad, I'm saying they're very
limited in utility. Also, I find it a fallacy that code is easier to
write. Handling exceptions correctly can get quite involved making the
code much more difficult to write if you have to write try/catch blocks
everywhere.
But you don't, that's the point, you only need to catch at the place
you wish to deal with the excaption, return codes on the other hand, do
need to be dealt with everywhere

Anyhow, I'm sure that there are plenty o people who would debate this,
and I'm looking forward to a constructive one.


Enjoy... :)

Dec 19 '05 #15

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

Similar topics

10
2035
by: KN | last post by:
I know both are pretty much the same and it comes down to personal choice. But I have to make the choice for the team. Things so far that I am considering 1. XML documentation in C# -- thats good.. not there in VB.net?? 2. Some language features of VB.Net like Redim(makes it easier for developers), but not good enough reason. 3. C# is in more like standard languages and key words used are more
29
3034
by: RAY | last post by:
Hi , my boss has asked I sit in on an interview this afternoon and that I create some interview questions on the person's experience. What is C++ used for and why would a company benefit from someone who could use it? I would like you guys/and gals to give me some good questions & the correct answers so I can give this person a good review for my boss.
113
12362
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same algorithm work with strings that may or may not be unicode 3) Number of bytes back must either be <= number of _TCHARs in * sizeof(_TCHAR), or the relation between output size and input size can be calculated simply. Has to take into account the...
59
5031
by: Alan Silver | last post by:
Hello, This is NOT a troll, it's a genuine question. Please read right through to see why. I have been using Vusual Basic and Classic ASP for some years, and have now started looking at ASP.NET. At first glance, it looks excellent, albeit nothing that couldn't have been done to Classic ASP. I have been through a few tutorials and was impressed with how quickly you can get database info onto a page.
17
14686
by: Brett | last post by:
I'd like references on where to find some good (quality) icons to use for form and application icons (for the EXE). Most of the free stuff isn't that great looking and there isn't a good selection. A site offering previews of icons or purchase of a single icon would be nice. Thanks, Brett
15
2620
by: Alex L Pavluck | last post by:
I am new to programming other than SAS. I read that C# is a good starting language and I have started to create some simple programs with C# 2005 express edition. Can someone let me know if this is indeed a good learning language and also suggest a good learning text. Thanks, Alex
6
2067
by: Jamiil | last post by:
I am not a programmer by any means, but a dedicated aficionado. I have good understanding of Java and C/C++, and now I would like to learn javascript->ajax, but I don't know where to start. My HTML knowledge is basic, however, with a little bit of an effort I can program a small page. Can anyone recommend a good beginner's book on JavaScript? Please, bear in mind that it is my intention to learn to program server and client side, thus the...
244
9632
by: Ajinkya | last post by:
Can anyone suggest me a good compiler for(c/cpp) for windows? I tried dev cpp but its debugging facility is very poor.
76
4099
by: lorlarz | last post by:
Crockford's JavaScript, The Good Parts (a book review). This shall perhaps be the world's shortest book review (for one of the world's shortests books). I like Douglas Crockford (because I am a crabby old man too; plus he _is_ smart and good).. But, how can he write a book on the good parts of JavaScript and not mention functions that address CSS & DOM? Weird. It's like
0
9706
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, 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...
0
9582
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,...
0
10580
Oralloy
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...
0
10082
tracyyun
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...
0
9157
agi2029
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...
0
6854
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();...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
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
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.