指定函数参数类型,但不变量 [英] Specifying function parameter type, but not variable

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

问题描述

我以前见过这样的示例代码

I have seen example code like this before

class C
{
    C();
    ~C();
    foo(T1, T2);
}

C::foo(T1, T2)
{
    //not using T1/T2
}

与常规代码相比

class D
{
    D();
    ~D();
    bar(T1 t1, T2 t2);
}

D::bar(T1 t1, T2 t2)
{
    //using t1 and t2
}

我想知道不为可用性定义类型变量的目的是什么?难道大多数人只是为了暗示api的那些参数当前未被使用,而是为了确保将来向后兼容吗?

And I am wondering what is the purpose of not defining your type variables for usability? Do most people do this just to hint that those parameters of the api are not currently in use, but to ensure backward compatibility in the future?

可能是用于RTTI,还是引用静态变量(尽管我见过的各种示例均未将其用于此目的,但它们甚至都不是模板函数,这些变量只是未使用的).我曾尝试搜索此功能,但我什至不知道它叫什么或要搜索什么.

Is it possibly for RTTI, or referencing static variables (although the various samples I've seen were not using it for this purpose, they werent even templated functions, the variables were simply unused). I've tried searching for this feature but I am not even sure what it is called or what to search.

基本上,使用这种方法的原因/好处/缺点是什么?

Basically, what are the reasons/benefits/downsides for using this approach?

推荐答案

通常这样做是为了禁止有关未使用变量的编译器警告.当您不创建变量名时,编译器不会警告您该变量未在函数中使用.

This is commonly done to suppress compiler warnings about unused variables. When you do not create a variable name, the compiler will not warn you that the variable is not being used in the function.

就像您说的那样,通常对于特定的实现并没有使用参数:

Like you stated, its usually that parameters are not being used for the particular implementation:

class an_iface
{
public:
    an_iface();

    virtual int doSomething(int valNeeded)=0;
}

#define NOT_SUPPORTED -1
class something : public an_iface
{
public:
    something();
    int doSomething (int) { return NOT_SUPPORTED; }
}

class somethingMore : public an_iface
{
public:
    somethingMore();
    int doSomething(int stuff) { return stuff * 10; }
}

这篇关于指定函数参数类型,但不变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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