科学网

 找回密码
  注册

tag 标签: conversion

相关帖子

版块 作者 回复/查看 最后发表

没有相关内容

相关日志

[转载]warning:deprecated conversion from string constant to 'char
depengchen 2013-10-25 10:15
Linux 环境下当 GCC 版本比较高时,编译代码可能出现的问题 问题是这样产生的,先看这个函数原型: void someFunc(char *someStr); 再看这个函数调用: someFunc(I'm a string!); 把这两个东西组合起来,用最新的g++编译一下就会得到标题中的警告。 为什么呢?原来 char *背后的含义是:给我个字符串,我要修改它。 而理论上,我们 传给函数的字面常量是没法被修改的 。 所以说,比较和理的办法是 把参数类型修改为 const char * 。 这个类型说背后的含义是: 给我个字符串,我只要读取它。 很自然的延伸一下。 如果我既要传字面常量又要传字符串变量怎么办呢?......重载 实验: 对deprecated conversion from string constant to 'char *'此类警告的详细解释 假定你想使用一个char*类型的变量,有时指向一个字符串,有时指向另外一个字符串。开始的代码就像这样: char *msg; msg = hello; msg = good-bye; 编译器会对这段代码给出两段警示,说”deprecated conversion from string constant to 'char *',意思就是说你没有能力修改字符串的内容。如果将代码写成这样,如: char *msg = hello; *msg = 'j'; printf( %s/n, hello ); 编译器会通过编译,实际上会将msg指向的内容从hello转变为jello, 正确的解决方法是将 msg声明为一个指向不变字符串的指针 : const char *msg; msg = hello; msg = good-bye; 这段代码可以成功编译,并且将msg指向的值如愿改变,但如果 你将指针指向的指进行赋值 : *msg = 'j'; 将会产生一个错误,不能修改一个字符串常量 注意如下的代码,此代码编译时不会出现警告也不会出现任何错误: const char *msg; char buf ; //注意不能使用char *buf; sprintf( buf, %03d/n, 7 ); msg = buf; 改变buf的内容是可以的,因为它并没有被声明为常量。在这种情况下,msg将指向一个字符串,007/n. 像这种语句 *buf = 'x'; 将会正确编译执行,但像 *msg = 'x'; 将会产生一个警告,因为msg指向的内容不允许改变 还有一种方法是 使用强制转换 ,使用强制转换意味着你清楚会出现什么情况,不需要编译器为你做出判断,例如下面的代码将不会产生警告: char *msg; msg = (char *) hello; 但一旦你使用强制转换,编译器对如下语句进行编译时,也不会出现错误或警告 *msg = 'j'; 这个错误将一直存在,但并不会被发现,直到运行时。那时再找出错点就相当麻烦了,比编译器提醒你,麻烦多了。所以,最好不要对字符串使用强制转换。 Constant 指针 根据constant的位置不同,可以有以下四种情况: const char* const msg_0; const char *msg_1; char* const msg_2; char *msg_3; 其中,msg_0是一个constant指针指向一个const字符串。这个声明编译器会给出一个警告,因为msg_0的指向没有被初始化,而且之后的语句也无法对mg_0进行赋值,如 const char const *msg_0 = hello; 会编译成功,但 *msg_0 = 'j';或者 msg_0 = good-bye; 将会产生错误 msg_1既可以指向一个const字符串,也可以指向一个可变的字符串,但是不能修改它所指向的字符串的内容。 编译msg_2这条语句,会出现和编译msg_0一样的错误。 因为指针是一个常量,所以它应该首先被赋值 。 如果刚开始已经赋值,那么它可以对指向的字符串内容进行修改 ,如: char buf ; char * const msg_2 = buf; 这段代码里,msg_2指向buf ,并且永远指向这个地址,不会改变; 对于msg_3,就没太多可以说的。你可以改变指针,也可以改变指针指向的内容
个人分类: 程序设计|0 个评论
[转载]C++类型转换操作符(type conversion operator)
depengchen 2013-10-15 11:33
C++类型转换操作符(type conversion operator) 类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义将类类型值转变为其他类型值的转换。转换操作符在类定义体内声明,在保留字 operator 之后跟着转换的目标类型。boost::ref和boost::cref就使用到了类型转换操作符。 函数原型 !-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -- T1:: operator T2() ;    // T1的成员函数,(T2)a类型转换 1. 转换函数必须是成员函数,不能指定返回类型,并且形参表必须为空;返回值是隐含的,返回值是与转换的类型相同的,即为上面原型中的T2; 2. T2表示内置类型名(built-in type)、类类型名(class type)或由类型别名(typedef)定义的名字;对任何可作为函数返回类型的类型(除了 void 之外)都可以定义转换函数,一般而言,不允许转换为数组或函数类型,转换为指针类型(数据和函数指针)以及引用类型是可以的; 3. 转换函数一般不应该改变被转换的对象,因此转换操作符通常应定义为 const 成员; 4. 支持继承,可以为虚函数; 5. 只要存在转换,编译器将在可以使用内置转换的地方自动调用它; 先通过一个简单的例子来说明如何使用类型转换操作符 按 Ctrl+C 复制代码 #include iostream class D { public: D(double d) : d_(d) {} /* “(int)D”类型转换 */ operator int() const { std::cout "(int)d called!" std::endl; return static_castint(d_); } private: double d_; }; int add(int a, int b) { return a + b; } int main() { D d1 = 1.1; D d2 = 2.2; std::cout add(d1, d2) std::endl; return 0; } 按 Ctrl+C 复制代码 在24行执行add(d1,d2)函数时“(int)D”类型转换函数将被自动调用,程序运行的输出为: (int)d called! (int)d called! 3 类型转换操作符 vs 类型转换构造函数(conversion constructor) 有时候使用conversion constructor就能实现类型转换,这种方式效率更高而且也更直观,下面举例说明: !-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -- 1 #include iostream 2 3 class A 4 { 5 public : 6 A( int num = 0 ) : dat(num) {} 7 8 /* (int)a类型转换 */ 9 operator int () { return dat; } 10 11 private : 12 int dat; 13 }; 14 15 16 class X 17 { 18 public : 19 X( int num = 0 ) : dat(num) {} 20 21 /* (int)a类型转换 */ 22 operator int () { return dat; } 23 24 /* (A)a类型转换 */ 25 operator A() { 26 A temp = dat; 27 return temp; 28 } 29 30 private : 31 int dat; 32 }; 33 34 35 int main() 36 { 37 X stuff = 37 ; 38 A more = 0 ; 39 int hold; 40 41 hold = stuff; // convert X::stuff to int 42 std::cout hold std::endl; 43 44 more = stuff; // convert X::stuff to A::more 45 std::cout more std::endl; // convert A::more to int 46 47 return 0 ; 48 } 49 上面这个程序中X类通过“operator A()”类型转换来实现将X类型对象转换成A类型,这种方式需要先创建一个临时A对象再用它去赋值目标对象;更好的方式是为A类增加一个构造函数: !-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -- A( const X rhs) : dat(rhs) {} 同时,请注意上面程序的第45行more的类型在调用std::cout时被隐式地转成了int! 一个简单boost::ref实现 通过重载type conversion operator,我们就可以自己实现一个简版的boost::ref。 !-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -- 1 #include iostream 2 3 template class T 4 class RefHolder 5 { 6 public : 7 RefHolder(T ref ) : ref_( ref ) {} 8 9 /* “(T)A”类型转换操作符 */ 10 operator T () const { 11 return ref_; 12 } 13 14 private : 15 T ref_; 16 }; 17 18 19 template class T 20 inline RefHolder T ByRef(T t) { 21 return RefHolder T (t); 22 } 23 24 int inc( int num) { 25 num ++ ; 26 return num; 27 } 28 29 30 int main() { 31 int n = 1 ; 32 std::cout inc(ByRef(n)) std::endl; // RefHolderint被转换成了int类型 33 34 return 0 ; 35 } 36 参考文章: 1. Wiki - Operators in C and C++ 2. Cast operator overload 3. Overloading the typecast operator 4. GenericProgramming: Change the Way You Write Exception-Safe Code Forever 5. C++ Primer - 14.9 Conversions and Class Types
个人分类: 程序设计|0 个评论
常用Gene ID Conversion Tools
热度 1 michael0214 2012-10-18 16:42
在自己的研究工作中,经常会遇到一些需要对Gene ID进行转换的情况。目前存在着大量的生物信息数据库,每个数据库都有自己定义的ID命名规则,转换起来实在是一个很大的工作。举个例子,之前构建的Human PPI network, 共选用相关数据库有6个,其中对于蛋白的命名相关的形式多达近10中,耗费了一个多月的时间才完成此ID归一化的过程。当然这个过程中是借助相关的Gene ID Coversion Tools。 下面就对生物信息中用到的IDs Conversion的相关工具进行列举: 1. DAVID: The D atabase for A nnotation, V isualization and I ntegrated D iscover http://david.abcc.ncifcrf.gov/conversion.jsp . 挺强大的一个工具,不过可能就是速度有点慢...还有一个缺点就是数据不能及时更新。 2. Biomart: http://www.biomart.org/biomart/martview/65c2ea6c079d1b85820fa5bbf5af62b5 这是一个绝对不错的东东,定期发布新版本,而且可以将数据下载到本地进行操作,推荐一下... 3. BioDBnet: http://biodbnet.abcc.ncifcrf.gov/db/db2db.php 4. Hyperlink Management System (HMS): http://biodb.jp/ 5. BridgeDB: http://www.biomedcentral.com/1471-2105/11/5 5. Uniprot 也提供了比较好的转换工具: http://www.uniprot.org/ (ID Mapping) 6. KEGG 的API : http://www.kegg.jp/kegg/rest/keggapi.html (conv) 更多其它可参考资源: 1. http://www.biostars.org/post/show/22/gene-id-conversion-tool/ 2. http://hum-molgen.org/NewsGen/08-2009/000020.html
23108 次阅读|3 个评论
Fortran to C++ conversion
baibing 2012-9-12 20:48
fable - Automatic Fortran to C++ conversion http://cci.lbl.gov/fable/
个人分类: 学习随笔|1 次阅读|0 个评论
科学网开博,存放些东西
lishangtong33 2012-3-25 20:11
gene id conversion gene_id in different database sometimes differs, tools to conveniently convert gene_id to fit data processing. http://biit.cs.ut.ee/gprofiler/gconvert.cgi http://david.abcc.ncifcrf.gov/conversion.jsp
2792 次阅读|0 个评论
[转载]European research project to boost solar cell efficiency
hanhw 2012-2-25 17:30
MANHASSET, NY -- A three-year European research project aims to push solar-cell efficiency towards 25 percent and reduce power conversion losses by 20 percent. The project is set to meet Europe’s 2020 climate targets and general energy policies. The ERG program is set to improve the efficiency of solar cells, devise innovative harvesting techniques, reduce power-conversion losses, and enhance energy-management strategies. European researchers will focus on the design and development of innovative solar cells that includes printable dye-sensitized solar cells as a low-cost alternative to silicon solutions. The project partners will also research novel power-management techniques for silicon-cell panels that track the maximum power point to boost output from solar arrays and improve power-conversion efficiency at the module and segment levels. ERG will also generate behavioral models for individual components of the smart grid with optimal energy-dispatching and battery-charging algorithms based on inputs from wireless sensor nodes distributed across the network. “ERG’s goal is to achieve significant efficiency improvements along the whole supply chain from PV panels to grid connection and make them available to all partners,” said ERG project coordinator Francesco Gennaro, a Staff Engineer at STMicroelectronics, in a statement. The 27 partners of the so-called ERG JU includes semiconductor makers Infineon Technologies, NXP Semiconductors, ON Semiconductor, STMicroelectronics, and solar panel manufacturers Applied Materials and Power Tech, as well as European industry and academic research organizations. The ENIAC (European Nanoelectronics Initiative Advisory Council) Joint Undertaking (JU) is a public-private partnership on nanoelectronics made up of ENIAC member States, the European Union, and AENEAS (representing European nanoelectronics RD entities). The total cost of the project is EUD25.7 million (about $34 million), partially funded through a combination of European and national grants from Italy, Belgium, Germany, Spain, Ireland, The Netherlands, Slovak Republic and United Kingdom. The ENIAC JU was set up in February 2008 and allocates grants throughout 2013. Funded projects need to be completed by end of 2017. The total value of the RD activities generated through the ENIAC JU is estimated at 3 B (almost $4 billion). http://www.powermanagementdesignline.com/electronics-news/4236142/European-research-project-to-boost-solar-cell-efficiency
个人分类: 新闻|3561 次阅读|0 个评论

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-18 06:09

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部