473,804 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overloading a function

Joe
Hi,

Can I pass a "generic" class pointer as an argument to a function?
For instance say classA and B are both derived from Z.

{
int iType =1;

Class? * pGeneric;

if(iType==0)
pGeneric= new ClassA;
else
pGeneric= new ClassB;
OverloadedFunc( pGeneric);
}

Where

OverloadedFunc( ClassA *pA){}
OverloadedFunc( ClassA *pB){}

or must I do:

if(iType==0)
{ ClassA *pA= new ClassA;
OverloadedFunc( pB); }
else
{ ClassB *pB= new ClassB;
OverloadedFunc( pB); }

Thanks

Jul 22 '05 #1
12 1827

"Joe" <no*@work.com > wrote in message
news:41******** **************@ news-text.dial.pipex .com...
Hi,

Can I pass a "generic" class pointer as an argument to a function?
For instance say classA and B are both derived from Z.

{
int iType =1;

Class? * pGeneric;

if(iType==0)
pGeneric= new ClassA;
else
pGeneric= new ClassB;
OverloadedFunc( pGeneric);
}

Where

OverloadedFunc( ClassA *pA){}
OverloadedFunc( ClassA *pB){}

or must I do:

if(iType==0)
{ ClassA *pA= new ClassA;
OverloadedFunc( pB); }
else
{ ClassB *pB= new ClassB;
OverloadedFunc( pB); }

Thanks


Why not use the base class?

void OverloadedFunc( ClassZ *pZ){}

That would be the usual thing to do.

john
Jul 22 '05 #2

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2n******** ***@uni-berlin.de...

"Joe" <no*@work.com > wrote in message
news:41******** **************@ news-text.dial.pipex .com...
Hi,

Can I pass a "generic" class pointer as an argument to a function?
For instance say classA and B are both derived from Z.

{
int iType =1;

Class? * pGeneric;

if(iType==0)
pGeneric= new ClassA;
else
pGeneric= new ClassB;
OverloadedFunc( pGeneric);
}

Where

OverloadedFunc( ClassA *pA){}
OverloadedFunc( ClassA *pB){}

or must I do:

if(iType==0)
{ ClassA *pA= new ClassA;
OverloadedFunc( pB); }
else
{ ClassB *pB= new ClassB;
OverloadedFunc( pB); }

Thanks


Why not use the base class?

void OverloadedFunc( ClassZ *pZ){}

That would be the usual thing to do.

john


Alternatively (I think I see where you are going now) make OverloadedFunc a
virtual member function of ClassA, ClassB and ClassZ, then you can write

int iType =1;

ClassZ * pGeneric;

if(iType==0)
pGeneric= new ClassA;
else
pGeneric= new ClassB;

pGeneric->OverloadedFunc ();

And if that isn't OK for some reason, use dynamic_cast.

int iType =1;

ClassZ * pGeneric;

if(iType==0)
pGeneric= new ClassA;
else
pGeneric= new ClassB;

ClassA * pA;
ClassB * pB;
if (pA = dynamic_cast<Cl assA*>(pGeneric ))
OverloadedFunc( pA);
else if (pB = dynamic_cast<Cl assB*>(pGeneric ))
OverloadedFunc( pB);
else
error;

john
Jul 22 '05 #3
Joe

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2n******** ***@uni-berlin.de...

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2n******** ***@uni-berlin.de...

"Joe" <no*@work.com > wrote in message
news:41******** **************@ news-text.dial.pipex .com...
Hi,

Can I pass a "generic" class pointer as an argument to a function?

<snip>
Alternatively (I think I see where you are going now) make OverloadedFunc a virtual member function of ClassA, ClassB and ClassZ, then you can write

int iType =1;

ClassZ * pGeneric;

if(iType==0)
pGeneric= new ClassA;
else
pGeneric= new ClassB;

pGeneric->OverloadedFunc ();

That would be a bit messy I think as the overloaded function is part of
another class already.
And if that isn't OK for some reason, use dynamic_cast.

int iType =1;

ClassZ * pGeneric;

if(iType==0)
pGeneric= new ClassA;
else
pGeneric= new ClassB;

ClassA * pA;
ClassB * pB;
if (pA = dynamic_cast<Cl assA*>(pGeneric ))
OverloadedFunc( pA);
else if (pB = dynamic_cast<Cl assB*>(pGeneric ))
OverloadedFunc( pB);
else
error;


That doesn't look any cleaner than my original.
So it appears that you can't define a "generic" type of pointer which
"knows"
what class it points to, so that when passed as a parameter an overloaded
function can ID its type and call its correct version.

Jul 22 '05 #4
Alternatively (I think I see where you are going now) make OverloadedFunc a virtual member function of ClassA, ClassB and ClassZ, then you can write


To OP - Then it should probably be called OverRiddenFunc and not
OverloadedFunc.
Jul 22 '05 #5

"Joe" <no*@work.com > wrote in message
news:41******** **************@ news-text.dial.pipex .com...

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2n******** ***@uni-berlin.de...

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2n******** ***@uni-berlin.de...

"Joe" <no*@work.com > wrote in message
news:41******** **************@ news-text.dial.pipex .com...
> Hi,
>
> Can I pass a "generic" class pointer as an argument to a function?

<snip>

Alternatively (I think I see where you are going now) make

OverloadedFunc a
virtual member function of ClassA, ClassB and ClassZ, then you can write

int iType =1;

ClassZ * pGeneric;

if(iType==0)
pGeneric= new ClassA;
else
pGeneric= new ClassB;

pGeneric->OverloadedFunc ();

That would be a bit messy I think as the overloaded function is part of
another class already.
And if that isn't OK for some reason, use dynamic_cast.

int iType =1;

ClassZ * pGeneric;

if(iType==0)
pGeneric= new ClassA;
else
pGeneric= new ClassB;

ClassA * pA;
ClassB * pB;
if (pA = dynamic_cast<Cl assA*>(pGeneric ))
OverloadedFunc( pA);
else if (pB = dynamic_cast<Cl assB*>(pGeneric ))
OverloadedFunc( pB);
else
error;


That doesn't look any cleaner than my original.
So it appears that you can't define a "generic" type of pointer which
"knows"
what class it points to, so that when passed as a parameter an overloaded
function can ID its type and call its correct version.


You can, but only when that pointer is the object on which a virtual method
is called.

pGeneric->OverloadedFunc ();

Here pGeneric can be declared as ClassZ* but the application knows when it
calls OverloadedFunc whether to class the ClassA version or the ClassB
version.

Since you say that OverloadedFunc is already a member of a different class
it seems that maybe what you are searching for is called 'double dispatch'.
That means making a method call virtual on two different pointers
simultaneously. It true that C++ doesn't support this directly but various
techniques have been tried, it does get messy though. Try googling if you're
interested.

john
Jul 22 '05 #6
On Thu, 5 Aug 2004 13:48:08 +0100, "Joe" <no*@work.com > wrote:
That doesn't look any cleaner than my original.
So it appears that you can't define a "generic" type of pointer which
"knows"
what class it points to, so that when passed as a parameter an overloaded
function can ID its type and call its correct version.


Not in a statically typed language, no! The solution is to use static
or dynamic polymorphism. The latter very much looks like the way to go
here, but you haven't stated what you're doing, so it's hard to tell.

Tom
Jul 22 '05 #7
On Thu, 5 Aug 2004 13:06:00 +0100, "Joe" <no*@work.com > wrote:
Hi,

Can I pass a "generic" class pointer as an argument to a function?
For instance say classA and B are both derived from Z.

{
int iType =1;

Class? * pGeneric;

if(iType==0)
pGeneric= new ClassA;
else
pGeneric= new ClassB;
OverloadedFunc( pGeneric);
}

Where

OverloadedFunc( ClassA *pA){}
OverloadedFunc( ClassA *pB){}

or must I do:

if(iType==0)
{ ClassA *pA= new ClassA;
OverloadedFunc( pB); }
else
{ ClassB *pB= new ClassB;
OverloadedFunc( pB); }


template <class T>
void doFunc()
{
T* pT = new T;
OverloadedFunc( pT);
}

Then:

if (iType == 0)
doFunc<ClassA>( );
else
doFunc<ClassB>( );

But it looks to me like you should be using virtual functions and a
common base class - type-switches are pretty much *always* indicative
of a problematic design.

Tom
Jul 22 '05 #8
Joe

"tom_usenet " <to********@hot mail.com> wrote in message
news:ll******** *************** *********@4ax.c om...
On Thu, 5 Aug 2004 13:48:08 +0100, "Joe" <no*@work.com > wrote:
That doesn't look any cleaner than my original.
So it appears that you can't define a "generic" type of pointer which
"knows"
what class it points to, so that when passed as a parameter an overloaded
function can ID its type and call its correct version.


Not in a statically typed language, no! The solution is to use static
or dynamic polymorphism. The latter very much looks like the way to go
here, but you haven't stated what you're doing, so it's hard to tell.


Tom,

What's unclear?

I have an ID passed to a method of a class, say class1.
That function then creates one of several classes derived from another base
class depending on the ID.

Whatever one is created I want to perform an overloaded function of class1,
the overloading depending on which derived class was created. Each does
something very similar (kicks off a thread) but a different derived thread
class with a different parameter set - the parameter sets are themselves
classes and have a different lifetime to thread classes (I access them after
the thread has died).

As well as just wondering wether it can be done I wanded to avoid a big
switch or if-else.

In the extreme say ther were dozens of slightly different classes, it would
be nice just to avoid any conditional statements and just have C++ do it
implicitely if possible.

Anything other info you need just say.
Jul 22 '05 #9
Joe wrote:
Tom,

What's unclear?

I have an ID passed to a method of a class, say class1.
That function then creates one of several classes derived from another base
class depending on the ID.

Whatever one is created I want to perform an overloaded function of class1,
the overloading depending on which derived class was created. Each does
something very similar (kicks off a thread) but a different derived thread
class with a different parameter set - the parameter sets are themselves
classes and have a different lifetime to thread classes (I access them after
the thread has died).

As well as just wondering wether it can be done I wanded to avoid a big
switch or if-else.

In the extreme say ther were dozens of slightly different classes, it would
be nice just to avoid any conditional statements and just have C++ do it
implicitely if possible.

Anything other info you need just say.


You need to implement the Factory Design Pattern. Search this newsgroup
for "factory".

Here is how I have implemented the factory:
1. Each class has a parent class. The factory returns a pointer to
a dynamically created child.

2. Each child has a static "create" class that returns a pointer to
a dynamically allocated child:
class Child1
: public Parent
{
public:
static Parent * create(ID_TYPE id);
};

Parent *
Child1 ::
create(ID_TYPE id)
{
if (id == CHILD1_ID)
{
return new Child1;
}
else
return NULL;
}

3. The Factory has a table of pointers to create functions for each
child class. Each function is executed by dereferencing the
pointer (and passing the ID). If the pointer is null, the next
function is executed.

In your case, you could have a virtual method in the parent class
for launching threads (or lets us call it initializing). The factory
could execute this method after successful creation of the child:
if (pointer_to_chi ld)
{
pointer_to_chil d->Initialize() ;
// or pointer_to_chil d->Create_Thread( );
}
Alternatives:
1. Create a table of <id, pointer to create function> and use
the std::lower_boun d to find the appropriate function pointer.
For small quantities, the above method is faster than using
the binary_search.

2. Have the create() function start the thread for that child,
rather than the factory.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #10

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

Similar topics

17
4727
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP manual mentions "overloading" (http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't really overloading at all... Not in the sense it's used in other languages supporting overloading (such as C++ and Java). As one of the...
4
6490
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a bit too broad or not, but I thought I'd give it shot... When is overloading preferable to...
5
5250
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that discussions of new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for currently. I've searched around for about the last too weeks and have read up on new and overloading it to some extent but...
15
24015
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and overriding? 2. When is one preferable to use as opposed to the other? 3. How are virtual functions related to this topic (overloading/overriding) - a real world example of using virtual functions would be very much appreciated.
16
16295
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
39
2192
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any C implementation supporting this feature? I assume some of you will claim that there is no need in function overloading, so I would like to know your arguments too. Thanks,
45
3296
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
15
2789
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
3
3285
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
10
3493
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
0
9704
marktang
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...
0
10562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10319
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 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...
1
10303
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5508
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...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
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
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.