使用typedef别名作为参数重载成员方法 [英] Overloading member methods with typedef aliases as parameters

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

问题描述

我在C ++中重载方法有些麻烦。

I’m having some trouble overloading methods in C++.

typedef char int8_t;
class SomeClass{
public:
…
void Method(int8_t paramater);
void Method(char paramater);
};

由于 int8_t 的typedef为 char 他们只是别名,他们可能指的是相同的类型,在这种情况下重载将无法工作。

Since int8_t is typedef as char they are just aliases, they may refer to the same type in which case overloading won’t work.

使他们在同一时间工作?你能提出同样的解决方案。
注意:我不想添加模板方法。

I want to make them work at the same time? Can you suggest solution to the same. Note: I do not want to add templated method.

以下是错误:


错误:SomeClass :: Method(char)的多重声明

Error: Multiple declaration for SomeClass::Method(char)


推荐答案

p>您可以通过尝试以下方式获得某种程度的改进:

You might gain some degree of improvement by trying this:

void Method(char paramater);
void Method(signed char paramater);
void Method(unsigned char paramater);

如果实现定义 int8_t

但是,一个狡猾的实现可以做这样的事情:

However, a devious implementation could do something like this:

typedef __special_secret_sauce int8_t;

然后你需要为 int8_t 。很难为 int8_t 定义另一个重载来应对这些实现,同时不能为typedef int8_t as signed char 。有人说这是不可能的。

and then you would need to define another overload for int8_t. It's pretty tough for you to define another overload for int8_t to contend with those implementations and at the same time not define it for implementations that typedef int8_t as signed char. Someone else said it's not even possible.

可能有一些实现,其中 int8_t 根本不存在。如果你只是定义重载的三个变体的char,那么你会没有问题。

There can be implementations where int8_t doesn't exist at all. If you just define overloads for the three variations of char then you'll have no problem there.

这篇关于使用typedef别名作为参数重载成员方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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