473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1796

"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<ClassA*>(pGeneric))
OverloadedFunc(pA);
else if (pB = dynamic_cast<ClassB*>(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<ClassA*>(pGeneric))
OverloadedFunc(pA);
else if (pB = dynamic_cast<ClassB*>(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<ClassA*>(pGeneric))
OverloadedFunc(pA);
else if (pB = dynamic_cast<ClassB*>(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********@hotmail.com> wrote in message
news:ll********************************@4ax.com...
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_child)
{
pointer_to_child->Initialize();
// or pointer_to_child->Create_Thread();
}
Alternatives:
1. Create a table of <id, pointer to create function> and use
the std::lower_bound 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.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

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

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


Not necessarily. Creation of objects based on type is the essence
behind the Factory Design Pattern, and not indicative of a problematic
design.

A common implementation for the Factory is message parsing. Establish
a parent message class, then create specific messages based upon
incoming data. Each specific message would be a different type (which
is a descendant of the parent). I've seen this implementation in many
embedded systems, but not so elegantly designed (and also using the
C language).

I also used the Factory in implementing a emulator for a processor.
The parent class was "Instruction" and child classes were addition,
rotate, etc. A program was a vector of {pointers to} instructions.

--
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.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #11
On Thu, 5 Aug 2004 14:46:10 +0100, "Joe" <no*@work.com> wrote:

"tom_usenet" <to********@hotmail.com> wrote in message
news:ll********************************@4ax.com.. .
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.


That's good to avoid - it's generally a sign of bad design.
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.


That is clearer; it sounds like you want a factory method (using a map
of ids to class creation functions (which will be template
specializations)) and the visitor pattern. A web search will reveal
all, or buy a book on OO design patterns.

Alternatively, you could use multiple dispatch (a method that is
polymorphic on more than 1 parameter, aka a multimethod). Again a web
search will help.

Tom
Jul 22 '05 #12
On Thu, 05 Aug 2004 14:37:53 GMT, Thomas Matthews
<Th****************************@sbcglobal.net> wrote:
tom_usenet wrote:
On Thu, 5 Aug 2004 13:06:00 +0100, "Joe" <no*@work.com> wrote:

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
Not necessarily. Creation of objects based on type is the essence
behind the Factory Design Pattern, and not indicative of a problematic
design.


Would you call that a type switch though? Actually, what the OP had
wasn't really a type switch either, but rather he needs a factory and
the visitor pattern (or multimethods).
A common implementation for the Factory is message parsing. Establish
a parent message class, then create specific messages based upon
incoming data. Each specific message would be a different type (which
is a descendant of the parent). I've seen this implementation in many
embedded systems, but not so elegantly designed (and also using the
C language).


I did a C++ based one on QNX a few years back, using a generic object
factory and a highish performance templated serialization technique
(at least it serialized a vector of PODs in one memcpy call, didn't
use virtual functions, and only required classes to implement a single
operator overload to be polymorphically serializable to and from a
stream). I'm sure I'd do a bit better job of it now, but not that much
better (performance was a major constraint).

Tom
Jul 22 '05 #13

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

Similar topics

17
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...
4
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. ...
5
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...
15
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...
16
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
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...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
15
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
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,...
10
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
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...

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.