关于c语言中的强制类型转换

scarftree 2016-10-13 04:28:41
想问一下,就是有个老师说,在c中强制类型转换有两种格式 ,一个是(int)a,另一个是(int)空格(a)
知道后面是先求表达式的值后强制转换,但是后面一个加括号的中间一定要有空格吗?
...全文
454 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 2016-10-13
  • 打赏
  • 举报
回复
不需要空格也行,有更多个空格也行,空格没有什么影响,不过习惯不写空格
AlbertS 2016-10-13
  • 打赏
  • 举报
回复
引用 6 楼 duringsummer 的回复:
[quote=引用 1 楼 shihengzhen101 的回复:] 哪有两种? 就一种!!! 那就是(int)a 至于a加不加括号要根据你实现的逻辑是怎样的 如果a是表达式,加括号表示对结果强转,不加括号表示对第一个操作数强转然后再运算 若果a是一个变量,加不加一样 至于你说的空格,有没有都行
其实我想问的就是第二种要不要括号[/quote] 你不是文的加不加空格吗,答案是加不加都行
scarftree 2016-10-13
  • 打赏
  • 举报
回复
引用 1 楼 shihengzhen101 的回复:
哪有两种? 就一种!!! 那就是(int)a 至于a加不加括号要根据你实现的逻辑是怎样的 如果a是表达式,加括号表示对结果强转,不加括号表示对第一个操作数强转然后再运算 若果a是一个变量,加不加一样 至于你说的空格,有没有都行
其实我想问的就是第二种要不要括号
赵4老师 2016-10-13
  • 打赏
  • 举报
回复
static_cast、dynamic_cast、reinterpret_cast和const_cast之间的区别
C-style cast举例:
 int i;
 double d;
 i = (int) d;
上面的代码就是本来为double类型的d,通过(int)d将其转换成整形值,并将该值赋给整形变量i (注意d本身的值并没有发生改变)。这就是典型的c-style类型转换。
下面是一个简单的程序:
#include <iostream>
using namespace std;
int main(void)
{
         int i;
         double d = 11.29;
         i = (int)d;
         cout << i << endl;
         cout << d << endl;
         return 0;
}
输出结果:
11
11.29
我们发现d值本身并没有发生任何变化。
在简单的情况下,上面这种类型转换可以很好地工作,但在C++中往往还是不够的,为此ANSI-C++新标准定义的四个转换符,即
     static_cast
    dynamic_cast
reinterpret_cast
      const_cast
同时在C++环境中,原先的C-Style的类型转换仍旧可以使用。
1)  static_cast
    用法:static_cast <typeid> (expression)
    说明:该运算符把expression转换为typeid类型,但没有运行时类型检查来确保转换的安全性。
    用途:
    a) 用于类层次结构中基类和派生类之间指针或者引用的转换。
        up-casting (把派生类的指针或引用转换成基类的指针或者引用表示)是安全的;
        down-casting(把基类指针或引用转换成子类的指针或者引用)是不安全的。
    b) 用于基本数据类型之间的转换,如把int转换成char,这种转换的安全性也要由开发人员来保证。
    c) 可以把空指针转换成目标类型的空指针(null pointer)。
    d) 把任何类型的表达式转换成void类型。
    注意: static_cast不能转换掉expression的const、volitale或者__unaligned属性。
2)  dynamic_cast
    用法:dynamic_cast <typeid> (expression)
    说明:该运算符把expression转换成typeid类型的对象。typeid必须是类的指针、类的引用或者void*。如果typeid是类的指针类型,
        那么expression也必须是指针,如果typeid是一个引用,那么expression也必须是一个引用。一般情况下,dynamic_cast用
        于具有多态性的类(即有虚函数的类)的类型转换。
               dynamic_cast依赖于RTTI信息,其次,在转换时,dynamic_cast会检查转换的source对象是否真的可以转换成target类型,
       这种检查不是语法上的,而是真实情况的检查。先看RTTI相关部分,通常,许多编译器都是通过vtable找到对象的RTTI信息
       的,这也就意味着,如果基类没有虚方法,也就无法判断一个基类指针变量所指对象的真实类型,这时候,dynamic_cast只能
       用来做安全的转换,例如从派生类指针转换成基类指针。而这种转换其实并不需要dynamic_cast参与。也就是说,dynamic_cast
       是根据RTTI记载的信息来判断类型转换是否合法的。
    用途:主要用于类层次之间的up-casting和down-casting,还可以用于类之间的交叉转换。在进行down-casting时,dynamic_cast
        具有类型检查的功能,比static_cast更安全。检测在运行时进行。如果被转换的指针不是一个被请求的有效完整的对象指针,
        返回值为NULL。当用于多态类型时,它允许任意的隐式类型转换以及相反过程。不过,与static_cast不同,在后一种情况里
        (注:即隐式转 换的相反过程),dynamic_cast会检查操作是否有效。也就是说,它会检查转换是否会返回一个被请求的有
        效的完整对象。
    注意:dynamic_cast不能转换掉expression的const、volitale或者__unaligned属性。
3)   reinterpret_cast
     用法:reinterpret_cast <typeid>(expression)
     说明:转换一个指针为其他类型的指针,也允许将一个指针转换为整数类型,反之亦然。这个操作符能够在非相关的类型之间进行
        转换。操作结果只是简单的从一个指针到别的指针的值的二进制拷贝,在类型之间指向的内容不做任何类型的检查和转换。这
        是一个强制转换。使用时有很大的风险,慎用之。
     注意:reinterpret_cast不能转换掉expression的const、volitale或者__unaligned属性。
4)   const_cast
     用法:const_cast<typeid>(expression)
     说明:这个类型操纵传递对象的const属性,或者是设置或者是移除。如:
           Class C{…}
           const C* a = new C;
           C* b = const_cast<C*>(a);
           如果将上面的const_cast转换成其他任何其他的转换,编译都不能通过,出错的信息大致如下:
           “…cannot convert from 'const class C *' to 'class C *'”。
     下面的代码是四种casting方法的典型用法示例:
     #include <iostream>
     using namespace std;
     class Base
     {
     public:
         int _base;
         virtual void printinfo()
         {
              cout << _base << endl;
         }
     };
     class Derived : public Base
     {
     public:
         int _derived;
         virtual void printinfo()
         {
              cout << _derived << endl;
         }
     };
     int main(void)
     {
         Base    b1;
         Derived d1;
         int    aInt    = 10;
         long   aLong   = 11;
         float  aFloat  = 11.11f;
         double aDouble = 12.12;
         Derived* pd = static_cast<Derived*>(&b1);       // down-casting 不安全
         Base*    pb = static_cast<Base*>(&d1);          // up-casting   安全
         Derived& d = static_cast<Derived&>(b1);         // down-casting 不安全
         Base& b = static_cast<Base&>(d1);               // up-casting   安全
         aInt = static_cast<int>(aFloat);                // 基本数据类型转换
         void* sth = static_cast<void*>(&aDouble);       // 将double指针类型转换成void指针类型
         double* bDouble = static_cast<double*>(sth);    // 将void指针类型转换成double指针类型
         cout << *bDouble << endl;
         Base* pb1 = dynamic_cast<Base*>(&d1);
         //Derived* pd1 = dynamic_cast<Derived*>(&b1);   // 编译时有warning,运行时出错
         int bInt = reinterpret_cast<int>(pb1);          // 将地址或指针转换成整数
         cout << bInt << endl;
         pb1 = reinterpret_cast<Base*>(bInt);            // 将整数转换成地址或指针
         int* cInt = reinterpret_cast<int*>(&aFloat);    // 这个转换的结果会出乎意料
         cout << (int)*cInt << endl;
         const Base* bBase = new Base();
         Base* cBase = const_cast<Base*>(bBase);
         //Base* dBase = dynamic_cast<Base*>(bBase);     // 不能通过编译
         //Base* eBase = static_cast<Base*>(bBase);      // 不能通过编译
         //Base* fBase = reinterpret_cast<Base*>(bBase); // 不能通过编译
         return 0;
    }
赵4老师 2016-10-13
  • 打赏
  • 举报
回复
一下内容摘自百度翻译:http://fanyi.baidu.com/#en/zh/ (int)一; int(); 显式转换运算符 C++允许显式类型转换使用的语法类似于函数调用语法。在圆括号中的表达式列表后面的一个简单类型名称,使用指定的表达式构造指定的类型的对象。下面的示例显示了一个显式类型转换为int类型: int i = int(D); 下面的示例使用函数调用结果中定义的点类的修改版本。 #包括< iostream。” 类点 { 公共: 定义默认构造函数。 point() { _x = _y = 0;} 定义另一个构造函数。 点(x,y){ _x = x;_y = y;} //定义“访问”功能 /参考类型。 符号与x() { return _x;} 符号与y() { return _y;} 无效show() { cout <<“=”<< _x <<“,” <<“Y”<< _y <<“\n”;} 私人: 无符号_x; 无符号_y; }; 无效main() { 点,点,点2; //指定点的显式转换 (10,10)。 点=点(10,10); / /使用x()通过分配一个明确的左值 将20转换为无符号类型。 1. x() =符号(20); 1. show(); / /指定默认的点对象2。 2 = point(); show() 2; } 这个程序的输出是: x = 20,Y = 10 x = 0,Y = 0 虽然前面的示例演示了使用常量的显式类型转换,但同样的技术可以在对象上执行这些转换。下面的代码片段演示了这个: int i = 7; 浮D; a浮动(I); 也可以使用“转换”语法指定显式类型转换。前一个例子,重写使用语法的语法,是: a(浮动)我; 当转换从单一值时,两个转换和函数样式转换都有相同的结果。然而,在函数样式语法中,您可以指定一个以上转换的参数。这种差异是重要的用户定义的类型。考虑一个点类及其转换: 点的结构 { 点(短的X,Y){ _x = x;_y = y;} … 短_x,_y; }; … 点角(3,10); 前面的示例,它使用函数样式转换,显示如何将两个值(一个用于X和一个用于Y)转换为用户定义的类型点。 重要的使用显式类型转换与照顾,因为他们重写C++编译器的内置类型检查。 语法 强制转换表达式: 一元表达式 (类型名称)强制转换表达式 转换符号必须用于转换到没有一个简单类型名称(例如指针或引用类型)的类型。转换到可以用一个简单的类型名称来表示的类型,可以用任何形式写。看到关于什么构成一个简单的类型名称的更多信息在6章类型说明符。 类型定义中的类型转换是非法的。
AlbertS 2016-10-13
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
(int)a; int(a); Explicit Type Conversion Operator C++ allows explicit type conversion using a syntax similar to the function-call syntax. A simple-type-name followed by an expression-list enclosed in parentheses constructs an object of the specified type using the specified expressions. The following example shows an explicit type conversion to type int: int i = int( d ); The following example uses a modified version of the Point class defined in Function-Call Results. #include <iostream.h> class Point { public: // Define default constructor. Point() { _x = _y = 0; } // Define another constructor. Point( int X, int Y ) { _x = X; _y = Y; } // Define "accessor" functions as // reference types. unsigned& x() { return _x; } unsigned& y() { return _y; } void Show() { cout << "x = " << _x << ", " << "y = " << _y << "\n"; } private: unsigned _x; unsigned _y; }; void main() { Point Point1, Point2; // Assign Point1 the explicit conversion // of ( 10, 10 ). Point1 = Point( 10, 10 ); // Use x() as an l-value by assigning an explicit // conversion of 20 to type unsigned. Point1.x() = unsigned( 20 ); Point1.Show(); // Assign Point2 the default Point object. Point2 = Point(); Point2.Show(); } The output from this program is: x = 20, y = 10 x = 0, y = 0 Although the preceding example demonstrates explicit type conversion using constants, the same technique works to perform these conversions on objects. The following code fragment demonstrates this: int i = 7; float d; d = float( i ); Explicit type conversions can also be specified using the “cast” syntax. The previous example, rewritten using the cast syntax, is: d = (float)i; Both cast and function-style conversions have the same results when converting from single values. However, in the function-style syntax, you can specify more than one argument for conversion. This difference is important for user-defined types. Consider a Point class and its conversions: struct Point { Point( short x, short y ) { _x = x; _y = y; } ... short _x, _y; }; ... Point pt = Point( 3, 10 ); The preceding example, which uses function-style conversion, shows how to convert two values (one for x and one for y) to the user-defined type Point. Important Use the explicit type conversions with care, since they override the C++ compiler’s built-in type checking. Syntax cast-expression : unary-expression ( type-name ) cast-expression The cast notation must be used for conversions to types that do not have a simple-type-name (pointer or reference types, for example). Conversion to types that can be expressed with a simple-type-name can be written in either form. See Type Specifiers in Chapter 6 for more information about what constitutes a simple-type-name. Type definition within casts is illegal.
我感觉你说的int(a);应该是int的构造函数,不属于强制类型转换
赵4老师 2016-10-13
  • 打赏
  • 举报
回复
(int)a; int(a); Explicit Type Conversion Operator C++ allows explicit type conversion using a syntax similar to the function-call syntax. A simple-type-name followed by an expression-list enclosed in parentheses constructs an object of the specified type using the specified expressions. The following example shows an explicit type conversion to type int: int i = int( d ); The following example uses a modified version of the Point class defined in Function-Call Results. #include <iostream.h> class Point { public: // Define default constructor. Point() { _x = _y = 0; } // Define another constructor. Point( int X, int Y ) { _x = X; _y = Y; } // Define "accessor" functions as // reference types. unsigned& x() { return _x; } unsigned& y() { return _y; } void Show() { cout << "x = " << _x << ", " << "y = " << _y << "\n"; } private: unsigned _x; unsigned _y; }; void main() { Point Point1, Point2; // Assign Point1 the explicit conversion // of ( 10, 10 ). Point1 = Point( 10, 10 ); // Use x() as an l-value by assigning an explicit // conversion of 20 to type unsigned. Point1.x() = unsigned( 20 ); Point1.Show(); // Assign Point2 the default Point object. Point2 = Point(); Point2.Show(); } The output from this program is: x = 20, y = 10 x = 0, y = 0 Although the preceding example demonstrates explicit type conversion using constants, the same technique works to perform these conversions on objects. The following code fragment demonstrates this: int i = 7; float d; d = float( i ); Explicit type conversions can also be specified using the “cast” syntax. The previous example, rewritten using the cast syntax, is: d = (float)i; Both cast and function-style conversions have the same results when converting from single values. However, in the function-style syntax, you can specify more than one argument for conversion. This difference is important for user-defined types. Consider a Point class and its conversions: struct Point { Point( short x, short y ) { _x = x; _y = y; } ... short _x, _y; }; ... Point pt = Point( 3, 10 ); The preceding example, which uses function-style conversion, shows how to convert two values (one for x and one for y) to the user-defined type Point. Important Use the explicit type conversions with care, since they override the C++ compiler’s built-in type checking. Syntax cast-expression : unary-expression ( type-name ) cast-expression The cast notation must be used for conversions to types that do not have a simple-type-name (pointer or reference types, for example). Conversion to types that can be expressed with a simple-type-name can be written in either form. See Type Specifiers in Chapter 6 for more information about what constitutes a simple-type-name. Type definition within casts is illegal.
AlbertS 2016-10-13
  • 打赏
  • 举报
回复
哪有两种? 就一种!!! 那就是(int)a 至于a加不加括号要根据你实现的逻辑是怎样的 如果a是表达式,加括号表示对结果强转,不加括号表示对第一个操作数强转然后再运算 若果a是一个变量,加不加一样 至于你说的空格,有没有都行

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧