C++学习系列七:Type conversions||Exceptions||Preprocessor directives

  • Type conversion

  • Implicit conversion

Implicit conversions are automatically performed when a value is copied to a compatible type.

short a = 2000;
int b;
b = a;

The value of a is promoted from short to int without the need of any explicit operator. THis is known as a standard conversion. Standard conversions after fundamental data types, and allow the conversions between numberical types (short to int , int to float, double to int …), to or from bool, and some pointer conversions.

  • Implicit conversion with classes

In the world of classes, implicit conversions can be controlled by means of three member functions:

1. Single-argument constructors

2. Assignment operator
3. Type-cast operator
  • Keyword explicit

On a function call, C++ allows one implicit conversion to happen for each argument. This may be somewhat problematic for classes, because it is not always what is intended.

  • Type casting

C++ is a strong-typed language. Many conversions, specially those that impley a different interpretation of the value, require an explicit conversion, known in C++ as type-casting. There exsit two main syntaxes for generic type-casting:

double x = 10.3;
int y;
y = int (x);   // functional notation
y = (int) x;   // c-like cast notation
  • dynamic_cast

dynamic_cast can only be used with pointers and references to classes (or with void*). Its purpose is to ensure that the result of the type conversion pointes to a valid complete object of the destination pointer type.

This naturally includes pointer upcast (converting from pointer-to-derived to pointer-to-base), in the same way as allowed as an implicit conversion.

But dynamic_cast can also downcast (convert from pointer-to-base to pointer-to-derived) polymorphic classes (those with virtual members) if -and only if- the pointed object is a valid complete object of the target type.

  • static_cast

static_cast can perform conversions between pointers to related classes, not only upcasts (from pointer-to-derived to pointer-to-base), but also downcasts (from pointer-to-base to pointer-to-derived).

  • reinterpret_cast

reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes. The operation result is a simple binary copy of the valuye from one pointer to the other. All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked.

  • const_cast

This type of casting manipulates the constness of the object pointed by a pointer, either to be set or to be removed.

  • typeid

typeid allows to check the type of an expression:

typeid (expression)

This operator returns a reference to a constant object of type type_info that is defined in the standard header .

  • Exceptions

Exceptions provide a way to react to exceptional circumstances (like runtime errors) in programs by transferring control to special functions called headlers.

To catch exceptions , a portion of code is placed under exception inspection.This is done by enclosing that portion of code in a try-block. When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored.

An exception is thrown by using the throw keyword from inside the try block. Exception handlers are declared with the keyword catch, which must be placed immediately after the try block:

#include <iostream>
using namespace std;

int main () {
    try
    {
        throw 20;
    }
    catch (int e)
    {
        cout << "An exception occurred, Exception Nr. " << e << '\n';
    }
    return 0;
}
  • Exception specification

Older code may contain dynamic exception specifications. They are now deprecated in C++, but still supported. A dynamic exception specification follows the declaration of a function, appeding a throw specifier to it.

double myfunction (char param) throw (int);
  • Standard exceptions

The C++ Standard library provides a base class specifically designed to declare objects to be thrown as exceptions. It is called std::exception and is defined in the header. This class has a virtual member function called what that returns a null-terminated character sequence (of type char*) and that can be overwritten in derived classes to caontain some sort of description of the exception.

  • Preprocessor directives

Preprocessor directives are lines included in the code of programs preceded by a hash sigh (#).

THese lines are not program statements but directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is acutally generated by regualr statements.

These preprocessor directives extend only accross a single line of code. As soon as a newline character is found, the preprocessor directive is ends. No semicolon (;) is expected at the end of a preprocessor directive. THe only way a preprocessor directive can extend through more than one line is by preceding he newline character at the end of the line by a backslash(\).

  • More definitiaons (#define, #undef)

To define preprocessor macros we can use #define. Its syntax is :

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值