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!!! 14 1475
Protoman wrote: Is this a good use of fn ptrs?
.... Or should I use fn objs instead? Thanks!!!
fn objs are more extensible and you loose nothing.
How would I make it use fn objs? Like this:
class select
{
public:
v_v_fptr operator()(bool val){if(val==tr ue)return Success;else return
Error;}
};
//...
v_v_fptr ptr=select()(va l);
ptr();
Could you help? Thanks!!!
I am programming for more that 10 years in C++ but I have not seen such
a way of error checking. But syntatically it is correct. If I were you I
would use exceptions.
Protoman wrote: 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!!!
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.
Protoman wrote: 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.
exceptions divide execution code from error checking code, that's the
reason why they were created....
Viktor Prehnal wrote: Protoman wrote:
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. exceptions divide execution code from error checking code, that's the reason why they were created....
Is attempting to open a file that is not there an exception or an error?
Gianni Mariani wrote: Viktor Prehnal wrote: Protoman wrote:
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. exceptions divide execution code from error checking code, that's the reason why they were created....
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!
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
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.
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.
Multithreaded code is almost always impossible to manage using exceptions.
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.
Anyhow, I'm sure that there are plenty o people who would debate this,
and I'm looking forward to a constructive one.
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.
Well, that is what throw and catch do: transfer control to the handler
unwinding the stack as much as necessary to get to the handlers context. I
can see two main differences to goto: (a) goto is a little less restricted
in where it can take you, but (b) the target of the jump is always known,
whereas a throw can take you to a place that you do not know in advance.
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.
I tend to advocate throwing whenever the construction of an object fails.
The reason is that I usually do not see a good way of dealing with
half-constructed objects. So, failure to claim a resource is, in my book,
always a good candidate for a throw().
It's also very unfortunate that exception specifiers are dynamically handled rather than allowing the compiler to do a static analysis.
Multithreaded code is almost always impossible to manage using exceptions.
Could you elaborate on this one. I have no experience in multithreaded
programming, so I do not really understand what you are refering to.
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.
The unfortunate thing is that you have to beware of exceptions whether you
use them or not: code beyond your control may throw just as well. In the
context of another thread it recently dawned upon me that very innocent
looking code can actually leak resources when exceptions enter the picture:
template < typename T >
class X {
T* data;
public:
X ( T const & t )
: data ( new T )
{
*data = t; // this leaks the pointer if the assignment throws.
}
}; // X
Better:
template < typename T >
class X {
T* data;
public:
X ( T const & t )
: data ( new T( t ) )
{}
}; // X
The upshot is: you really have to go through your code thinking "this line
might throw, what happens" at every single line you encounter. I find that
very hard, indeed.
Best regards
Kai-Uwe Bux This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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.
|
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...
|
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.
|
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
| |
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
|
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...
|
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.
|
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
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |