Replace Pointer To Function Array?

I

Immortal Nephi

The programmer uses procedural programming like C style as an example
below.

int Func_Count = 0;

void Run() { Func_Run[Func_Count++](); }

They decide to move to OOP using class. Most OOP programmers never
use pointer to function array. Sometimes, they use switch keyword
instead. Switch may not be good to hold hundreds or thousands of
member functions.

Do you know the example how OOP class can be used with pointer to
function array in different way? Ordinary pointer to function is
faster than pointer to member function.

Please advise.
 
A

Alf P. Steinbach

* Immortal Nephi:
The programmer uses procedural programming like C style as an example
below.

int Func_Count = 0;

void Run() { Func_Run[Func_Count++](); }

They decide to move to OOP using class. Most OOP programmers never
use pointer to function array.

Huh, they don't?

Sometimes, they use switch keyword instead.

Possibly "they" do that "sometimes".

Switch may not be good to hold hundreds or thousands of
member functions.

Uhm, the most important reasons for not using a 'switch' for this kind of thing
have to do with the introduction of redundancy and lack of access restrictions,
making many various parts of a system dependent on detailed knowledge about the
individual functions, the current function set, the data they handle and the
invariants and assumptions, so that maintainance has to update all those various
code parts e.g. for a bugfix or introduction of additional functionality.

Do you know the example how OOP class can be used with pointer to
function array in different way?

There is no "the" example.

There is an unbounded near infinite set of examples.

Without further context it's practically impossible to guess what you mean (it's
also difficult to say whether this is a *HOMEWORK* question in disguise).

Ordinary pointer to function is faster than pointer to member function.

Please post the code you used used for this comparision, info about which
compiler and compiler options, OS and machine details, and results.


Cheers & hth.,

- Alf
 
P

paulkp

Most OOP programmers never use pointer to function array.  Sometimes,
they use switch keyword instead.

No. The proper way in OOP is virtual function.
Switch may not be good to hold hundreds or thousands of
member functions.

That sounds like an error in design. Most likely there is no
class hierarchy and no inheritance if you end up in thousands
of member functions in one class.
 
D

DerTopper

The programmer uses procedural programming like C style as an example
below.

int Func_Count = 0;

void Run() { Func_Run[Func_Count++](); }

They decide to move to OOP using class.  Most OOP programmers never
use pointer to function array.  Sometimes, they use switch keyword
instead.  Switch may not be good to hold hundreds or thousands of
member functions.

Do you know the example how OOP class can be used with pointer to
function array in different way?  Ordinary pointer to function is
faster than pointer to member function.

I don't know whether I understand you right, so I'll rephrase your
question and answer the reprashed question:
Q: How would you model the scenario where a array of function pointers
should be traversed and each function of the array should be invoked
in an object-oriented fashion?

A: I'd introduce one interface and multiple implementations of this
interface (one for each function that is pointed to in the function
array):

class IFunctor
{
public:
virtual void DoSomething () = 0;
};

class SomeImplementation : public IFunctor
{
public:
void DoSomething ()
{
// Some code.
}
};

class SomeOtherImplementation : public IFunctor
{
public:
void DoSomething ()
{
// Some code.
}
};

void Run (std::vector<IFunctor>& FunctionArray)
{
for (std::vector<IFunctor>::iterator it = FunctionArray.begin ();
it != FunctionArray.end ();
++it)
it->DoSomething ();
}

Regards,
Stuart
 
S

Sana

On Feb 20, 4:29 am, (e-mail address removed) wrote:
[...]
I don't know whether I understand you right, so I'll rephrase your
question and answer the reprashed question:
Q: How would you model the scenario where a array of function pointers
should be traversed and each function of the array should be invoked
in an object-oriented fashion?

A: I'd introduce one interface and multiple implementations of this
interface (one for each function that is pointed to in the function
array):

class IFunctor
{
public:
  virtual void DoSomething () = 0;

};

class SomeImplementation : public IFunctor
{
public:
  void DoSomething ()
  {
    // Some code.
  }

};

class SomeOtherImplementation : public IFunctor
{
public:
  void DoSomething ()
  {
    // Some code.
  }

};

void Run (std::vector<IFunctor>& FunctionArray)
{
  for (std::vector<IFunctor>::iterator it = FunctionArray.begin ();
       it != FunctionArray.end ();
       ++it)
    it->DoSomething ();

}

One cannot have a vector of IFunctor and store derived types. Objects
will get sliced. It has to be vector<IFunctor*>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top