What is an action class?



What is an action class?..

Answer / ritesh pal

The simplest and most obvious way to specify an action in C++ is to write a function. However, if the action has to be delayed, has to be transmitted 'elsewhere' before being performed, requires its own data, has to be combined with other actions, etc then it often becomes attractive to provide the action in the form of a class that can execute the desired action and provide other services as well.

Manipulators used with iostreams is an obvious example.

Explanation:
A common form of action class is a simple class containing just one virtual function.
class Action
{
public:
virtual int do_it( int )=0;
virtual ~Action( );
}
Given this, we can write code say a member that can store actions for later execution without using pointers to functions, without knowing anything about the objects involved, and without even knowing the name of the operation it invokes. For example:
class write_file : public Action
{
File& f;
public:
int do_it(int)
{
return fwrite( ).suceed( );
}
};
class error_message: public Action
{
response_box db(message.cstr( ),"Continue","Cancel","Retry");
switch (db.getresponse( ))
{
case 0: return 0;
case 1: abort();
case 2: current_operation.redo( );return 1;
}
};

A user of the Action class will be completely isolated from any knowledge of derived classes such as write_file and error_message.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

What character terminates all character array strings a) b) . c) END

0 Answers  


What is a lambda function c++?

0 Answers  


What can c++ be used for?

0 Answers  


Is dev c++ a good compiler?

0 Answers  


What is the v-ptr?

0 Answers  






What are dynamic type checking?

0 Answers  


What is the difference between containment and delegation?

0 Answers  


Can you please explain the difference between static and dynamic binding of functions?

0 Answers  


Difference between declaration and definition of a variable.

0 Answers  


What are namespaces in c++?

0 Answers  


What do you mean by Stack unwinding?

1 Answers  


Explain register storage specifier.

0 Answers  


Categories