C++关于函数重载的问题

blackpool 2010-10-11 09:01:06
C++关于函数重载的问题

已有代码如下:

template <class TMathType, class TLineType>
void
ClassA<TMathType, TPointType>
::FunctionA(LineStartPointer pLine)
{
// Some functions
}

现在需要一个函数重载:

template <class TMathType, class TLineType>
void
ClassA<TMathType, TPointType>
::FunctionA(LineStartConstPointer pLine)
{
// Some functions
}

整个函数实现全部一样,只是参数的类型不同,
一个是普通指针LineStartPointer,
一个是const指针LineStartConstPointer。

// Some functions这里的具体实现完全一样,如果不想复制实现的代码到重载函数,
请问应该怎么办?

谢谢!
...全文
133 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
blackpool 2010-10-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ji4ming2 的回复:]

把原来的函数中的

//Some functions

换成

LineStartPointer tmpLine=(LineStartPointer)pLine;
ClassA::FunctionA(tmpLine);

可以么?
[/Quote]

也就是说,原函数为:

template <class TMathType, class TLineType>
void
ClassA<TMathType, TPointType>
::FunctionA(LineStartPointer pLine)
{
// Some functions
}

函数重载为:

template <class TMathType, class TLineType>
void
ClassA<TMathType, TPointType>
::FunctionA(LineStartConstPointer pLine)
{
LineStartPointer tmpLine=(LineStartPointer)pLine;
ClassA::FunctionA(tmpLine);
}

请问这种强制转换总是合法的吗? 可以把Const指针转为非Const的吗?

非常感谢!!
malipeng0086 2010-10-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ji4ming2 的回复:]

把原来的函数中的

//Some functions

换成

LineStartPointer tmpLine=(LineStartPointer)pLine;
ClassA::FunctionA(tmpLine);

可以么?
[/Quote]

支持这种做法,简单明了。
sea_spray 2010-10-12
  • 打赏
  • 举报
回复
看的不是很明白
Rainqin123 2010-10-12
  • 打赏
  • 举报
回复
自己可以做个模板,库函数就是这样做来的...
ji4ming2 2010-10-12
  • 打赏
  • 举报
回复
把原来的函数中的

//Some functions

换成

LineStartPointer tmpLine=(LineStartPointer)pLine;
ClassA::FunctionA(tmpLine);

可以么?
blackpool 2010-10-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fanlehai 的回复:]

Some functions写个函数调用
[/Quote]

Some functions中使用了pLine, 那么如果写一个函数调用,又将面临pLine是什么类型的问题。

不是吗?

谢谢!
blackpool 2010-10-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 taodm 的回复:]

模板。
[/Quote]

楼上是说在模板中再加上一个LineStarPointerType?

template <class TMathType, class TLineType, class LineStarPointerType>
void
ClassA<TMathType, TPointType>
::FunctionA(LineStartConstPointer pLine)
{
// Some functions
}

可是整个程序中还有FunctionB,C,D...他们都没有使用pLine这个参数,全都要加上吗?

而且调用此函数的主程序也的改动吧?

谢谢!
Learn-anything 2010-10-11
  • 打赏
  • 举报
回复
Some functions写个函数调用
taodm 2010-10-11
  • 打赏
  • 举报
回复
模板。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧