重载函数 [英] overloading a function

查看:62
本文介绍了重载函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我可以传递泛型吗?类指针作为函数的参数?

例如,说classA和B都是从Z派生的。


{

int iType = 1;


班级? * pGeneric;


if(iType == 0)

pGeneric = new ClassA;

else

pGeneric = new ClassB;

OverloadedFunc(pGeneric);

}


其中


OverloadedFunc(ClassA * pA){}

OverloadedFunc(ClassA * pB){}


或我必须这样做:


if(iType == 0)

{ClassA * pA = new ClassA;

OverloadedFunc(pB); }

else

{ClassB * pB = new ClassB;

OverloadedFunc(pB); }


谢谢

解决方案



" Joe" ; < no*@work.com>在消息中写道

news:41 ********************** @ news-text.dial.pipex.com ...... < blockquote class =post_quotes>

我可以传递泛型吗?类指针作为函数的参数?

例如,说classA和B都是从Z派生的。

{
int iType = 1;

课程? * pGeneric;

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

pGeneric = new ClassB;

OverloadedFunc (pGeneric);
}



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

或者我必须这样做:

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

谢谢




为什么不使用基类?


void OverloadedFunc(ClassZ * pZ){}


那是常见的事情。


john




" John Harrison" <乔************* @ hotmail.com>在消息中写道

news:2n *********** @ uni-berlin.de ...


" Joe" < no*@work.com>在消息中写道
新闻:41 ********************** @ news-text.dial.pipex.com ...



我可以传递泛型吗?类指针作为函数的参数?

例如,说classA和B都是从Z派生的。

{
int iType = 1;

课程? * pGeneric;

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

pGeneric = new ClassB;

OverloadedFunc (pGeneric);
}



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

或者我必须这样做:

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

谢谢



为什么不使用基类?

void OverloadedFunc(ClassZ * pZ){}

这将是通常的事情。

约翰




或者(我想我现在看到你要去的地方) make OverloadedFunc一个

ClassA,ClassB和ClassZ的虚拟成员函数,然后就可以写了


int iType = 1;


ClassZ * pGeneric;


if(iType == 0)

pGeneric = new ClassA;

else

pGeneric = new ClassB;


pGeneric-> OverloadedFunc();


如果那不是'' t出于某种原因,请使用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))

Overl oadedFunc(pA);

else if(pB = dynamic_cast< ClassB *>(pGeneric))

OverloadedFunc(pB);

else

错误;


john




" John Harrison" ; <乔************* @ hotmail.com>在消息中写道

news:2n *********** @ uni-berlin.de ...


John Harrison ; <乔************* @ hotmail.com>在消息中写道
新闻:2n *********** @ uni-berlin.de ...


" Joe" < no*@work.com>在消息中写道
新闻:41 ********************** @ news-text.dial.pipex.com ...



我可以传递泛型吗?类指针作为函数的参数?



< snip>
或者(我想我现在看到你要去的地方)make OverloadedFunc
aA类,ClassB和ClassZ的虚拟成员函数,那么你可以写一下iType = 1;

ClassZ * pGeneric;

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

pGeneric = new ClassB;

pGeneric-> OverloadedFunc();



我觉得这有点乱,因为重载的功能已经是另一个类的b / b
的一部分了。

如果由于某种原因不行,请使用dynamic_cast。

int iType = 1;

ClassZ * pGeneric;

如果(iType == 0)
pGeneric = new ClassA;

pGeneric = new ClassB;

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

错误;




看起来比我原来的更干净。

因此,您似乎无法定义通用。指示的类型

知道

它指向哪个类,所以当作为参数传递时,一个超载的

函数可以ID它的类型并称其正确版本。


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

解决方案


"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



"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



"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.


这篇关于重载函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆