代码之家  ›  专栏  ›  技术社区  ›  m1tk4

C++的C++操作符与(MyStultType){1,2,3}是什么?

  •  0
  • m1tk4  · 技术社区  · 16 年前

    是否有C++的铸造操作符(或其组合)相当于下面的旧样式:

    struct MyStruct {
       int i;
       int j;
       int k;
    };
    
    void do_something_with_mystruct( MyStruct ms ) {
       ...
    };
    
    int main( int argc, char** argv ) {
       do_something_with_mystruct( (MyStruct){1,2,3} );
    };
    
    2 回复  |  直到 16 年前
        1
  •  4
  •   zwol    16 年前

    构造

    (MyStruct) {1,2,3}
    

    其实不是演员!它是isoc99“复合文字”。没有任何等价的使用任何C++ *_cast<> 运算符,因为C++(甚至C++ +0x)不包含这个构造。有些编译器将它作为C++的扩展实现,但您仍然必须这样编写。参见示例 http://gcc.gnu.org/onlinedocs/gcc-4.4.4/gcc/Compound-Literals.html

        2
  •  -1
  •   David    16 年前

    reinterpret_cast<ToType>(fromType);
    

    它对位模式进行盲转换。在大多数情况下是不安全的,在需要的时候很有用。