473,473 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calling a function

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.

Jul 1 '07 #1
4 1247
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
Jul 1 '07 #2
On 2007-07-01 04:21, sara wrote:
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).

--
Erik Wikström
Jul 1 '07 #3
* 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.
Jul 1 '07 #4

sara <sa********@gmail.comwrote in message...
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::ostream &out ){
out<<"Do something here."<<std::endl;
}
};

class First : public Common{ public:
void Thing( std::ostream &out){
out<<"class First: ";
DoThing( out );
}
};
class Second : public Common{ public:
void Thing( std::ostream &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.

--
Bob R
POVrookie
Jul 1 '07 #5

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

Similar topics

8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
6
by: jchao123 | last post by:
Dear All, I have an MDB file (Access 2000/XP) which contains generic routines I use in various apps (eg, API calls, File access classes etc). I have compiled into an MDE file which I reference...
14
by: ericellsworth | last post by:
Hi, I'm trying to use a class to pass variables back and forth from a form opened in dialog mode. I have created a class which invokes a form in its show method, like so: Public Sub Show() '...
1
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
2
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
15
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which...
11
by: briankirkpatrick | last post by:
Forgive me if my post seems a little amateurish... I'm requesting assistance from some of you smart folks out there to get the managed calls write that meet the specification in the esa.h for...
16
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.