Calling a function

S

sara

Hi All,

I have multiple classes and almost all of them want to call a single
function. One way to do this is to create a single class consisting
of that function and then initialize the class whenever I want to call
the function. In this way the used class has only one member function.
Is there a more efficient way?

Thanks.
 
P

Paulo da Silva

sara escreveu:
Hi All,

I have multiple classes and almost all of them want to call a single
function. One way to do this is to create a single class consisting
of that function and then initialize the class whenever I want to call
the function. In this way the used class has only one member function.
Is there a more efficient way?

Thanks.

Choose one and let the efficiency to the compiler ...

#include <iostream>

using namespace std;

void f1(char const *x)
{ cout << "f1: " << x << endl;
}

class f2
{public:
void operator()(char const *x) const
{ cout << "f2: " << x << endl;
}
};

class MyFuncs
{public:
static void f3(char const *x)
{ cout << "f3: " << x << endl;
}
};

int main()
{ f1("My test 1"); // The simplest
f2()("My test 2"); // The trickyest
MyFuncs::f3("My test 3"); // The OO'est
return 0;
}

HTH
Paulo da Silva
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi All,

I have multiple classes and almost all of them want to call a single
function. One way to do this is to create a single class consisting
of that function and then initialize the class whenever I want to call
the function. In this way the used class has only one member function.
Is there a more efficient way?

The idea of putting the method in a class and instantiating that class
each time you want to call the method is the least efficient, and (in my
opinion) the ugliest. If you go this road at least make the method
static so you don't have to instantiate the class. But if you do it like
this (a class with only one static method) I think you have a class that
serves no purpose, so get rid of the class and make the method a free
function (put it in a namespace if you don't want global functions).
 
A

Alf P. Steinbach

* sara:
Hi All,

I have multiple classes and almost all of them want to call a single
function.

A class doesn't do anything. An instance of a class does not do
anything. A member function can do something.

What did you mean to describe?

One way to do this is to create a single class consisting
of that function and then initialize the class whenever I want to call
the function.

Classes are not initialized. They can be instantiated. You do not need
to instantiate a class to call a static member function, and you don't
need to let a function be a member of class.

In this way the used class has only one member function.
Is there a more efficient way?

Depends what your question is about. Give an example.
 
B

BobR

sara said:
Hi All,
I have multiple classes and almost all of them want to call a single
function. One way to do this is to create a single class consisting
of that function and then initialize the class whenever I want to call
the function. In this way the used class has only one member function.
Is there a more efficient way?
Thanks.

As others said, it depends on what you are trying to do.
Just for an idea:

#include <iostream>
#include <vector>

class Common{ public:
virtual ~Common(){} // look up "rule of three"
virtual void DoThing( std::eek:stream &out ){
out<<"Do something here."<<std::endl;
}
};

class First : public Common{ public:
void Thing( std::eek:stream &out){
out<<"class First: ";
DoThing( out );
}
};
class Second : public Common{ public:
void Thing( std::eek:stream &out){
out<<"class Second: ";
DoThing( out );
}
};

int main(){
using std::cout; // for NG post
First MyFirst;
MyFirst.Thing( cout );
Second MySecond;
MySecond.Thing( cout );

cout<<std::endl;
std::vector<Common*> Rack;
Rack.push_back( &MyFirst );
Rack.push_back( &MySecond );
for( std::size_t i(0); i < Rack.size(); ++i ){
First *pF( dynamic_cast<First*>( Rack.at(i) ) );
if( pF ){ pF->Thing( cout );}
Second *pS( dynamic_cast<Second*>( Rack.at(i) ) );
if( pS ){ pS->Thing( cout );}
} // for(i)

return 0;
} // main()
/* -output-
class First: Do something here.
class Second: Do something here.

class First: Do something here.
class Second: Do something here.
*/

If you are worried about 'efficient', measure.
 

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
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top