警告:不推荐从字符串常量转换为“ACE_TCHAR *” [英] warning: deprecated conversion from string constant to ‘ACE_TCHAR*’

查看:213
本文介绍了警告:不推荐从字符串常量转换为“ACE_TCHAR *”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  void尝试编写单元测试,其中我们将构造函数调用为以下形式的通用类: testConstructor(){
int argc = 2;
ACE_TCHAR * argv [] = {Input1,Input2};
MyClass * myClass = new MyClass(argc,argv);
/ ** processing ** /
}

$ c> ACE_TCHAR * 与 char * 相同。上面的代码导致了



警告:从字符串常量转换为ACE_TCHAR *



我也尝试过:

  void testConstructor(){
int argc = 2;
ACE_TCHAR * argv [2];
argv [0] =Input1;
argv [1] =Input2;
MyClass * myClass = new MyClass(argc,argv);
/ **处理** /
}

错误。



我在线阅读,可以通过使用

来减轻这种情况。

const ACE_TCHAR * argv [] = {Input1,Input2};



但是编译因为函数签名而失败。



编辑:我不允许修改第三方代码,我只为其编写单元测试。



任何想法?

解决方案

字符串常量是一个常量。



如果无法做到这一点,你可以使用显式类型转换器

p>

  ACE_TCHAR * argv [] = {const_cast< ACE_TCHAR *>(Input1),
const_cast< ACE_TCHAR *> ;(Input2)};

或先将字符串指定为非常量字符数组

  char input1 [] =Input1; 
char input2 [] =Input2;
ACE_TCHAR * argv [] = {input1,input2};


I'm attempting to write unit tests in which we call the constructor to a generic class in the form:

void testConstructor() {
    int argc = 2;
    ACE_TCHAR* argv[] = {"Input1", "Input2"};
    MyClass *myClass = new MyClass(argc, argv);
    /**processing**/
}

Think of ACE_TCHAR* the same as char*. The code above results in

warning: deprecated conversion from string constant to ‘ACE_TCHAR*’

I have also tried:

void testConstructor() {
    int argc = 2;
    ACE_TCHAR* argv[2];
    argv[0] = "Input1";
    argv[1] = "Input2";
    MyClass *myClass = new MyClass(argc, argv);
    /**processing**/
}

Which results in the same error.

I read online somewhere that this could be mitigated by using

const ACE_TCHAR* argv[] = {"Input1", "Input2"};

But then the compile fails because of the function signature.

Edit: I'm not allowed to modify the 3rd party code, I'm only writing unit tests for it.

Any ideas?

解决方案

A string constant is, as the name says, a constant. So to make the warning go away, the proper solution would indeed be to turn the pointer into a const pointer.

If that cannot be done, you can explicit typecasts

ACE_TCHAR* argv[] = {const_cast<ACE_TCHAR*>("Input1"),
                     const_cast<ACE_TCHAR*>("Input2")};

or assign the strings to non-constant char arrays first

char input1[] = "Input1";
char input2[] = "Input2";
ACE_TCHAR* argv[] = {input1, input2};

这篇关于警告:不推荐从字符串常量转换为“ACE_TCHAR *”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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