Skip to main content

C++ Primer Sec. 2.5 Dealing with Types

Dealing with Types

type alias

typedef double wages; // wages is a synonym for double
typedef wages base, *p; // base is a synonym for double, p for double*

C++11: alias declaration

using SI = Sales_item; // SI is a synonym for Sales_item

As usual, a const that appears in the base type modifies the given type. The type of pstring is “pointer to char.” So, const pstring is a constant pointer to char—not a pointer to const char. It can be tempting, albeit incorrect, to interpret a declaration that uses a type alias by conceptually replacing the alias with its corresponding type.

typedef char *pstring;
const pstring cstr = 0; // cstr is a constant pointer to char
const pstring *ps; // ps is a pointer to a constant pointer to char
const char *cstr = 0; // wrong interpretation of const pstring cstr

C++11: auto. auto tells the compiler to deduce the type from the initializer.

The initializers for all the variables in the declaration using auto must have types that are consistent with each other.

auto i = 0, *p = &i; // ok: i is int and p is a pointer to int
auto sz = 0, pi = 3.14; // error: inconsistent types for sz and pi

auto ordinarily ignores top-level consts, keeps low-level consts

int i = 0, &r = i;
auto a = r; // a is an int (r is an alias for i, which has type int)
const int ci = i, &cr = ci;
auto b = ci; // b is an int (top-level const in ci is dropped)
auto c = cr; // c is an int (cr is an alias for ci whose const is top-level)
auto d=&i; // d isan int*(& ofan int objectis int*)
auto e = &ci; // e is const int*(& of a const object is low-level const)
// if we want the deduced type to have a top-level const, we must say so explicitly
const auto f = ci; // deduced type of ci is int; f has type const int
// error: type deduced from i is int; type deduced from &ci is const int
auto &n = i, *p2 = &ci;

The decltype type specifier

C++11: decltype, which returns the type of its operand. The compiler analyzes the expression to determine its type but does not evaluate the expression

decltype(f()) sum = x; // sum has whatever type f returns

decltype returns the type of that variable, including top-level const and references

const int ci = 0, &cj = ci;
decltype(ci) x = 0; // x has type const int
decltype(cj) y = x; // y has type const int& and is bound to x
decltype(cj) z; // error: z is a reference and must be initialized
// decltype of an expression can be a reference type int i = 42, *p = &i, &r = i;
decltype(r + 0) b; // ok: addition yields an int; b is an (uninitialized) int
decltype(*p) c; // error: c is int& and must be initialized
// decltype of a parenthesized variable is always a reference
decltype((i)) d; // error: d is int& and must be initialized
decltype(i) e; // ok: e is an (uninitialized) int

Comments

Popular posts from this blog

C++ Primer Ch1. Introduction

Different compilers use different suffix conventions; the most common include .cc , .cxx , .cpp , .cp , and .C .  To compile: $ CC prog1.cc prog1.exe is generated on Windows, a.out is generated on UNIX To run: $prog1 or $.\prog1 The command used to run the C++ compiler varies across compilers and operating systems. $ g++ -o prog1 prog1.cc Depending on the release of the GNU compiler you are using, you may need to specify -std=c++0x to turn on C++ 11 support. Typically, we put all the #include directives for a program at the beginning of the source file. std::cout << "Enter two numbers:" << std::endl;  Our output statement uses the << operator twice. Because the operator returns its left-hand operand, the result of the first operator becomes the left-hand operand of the second.    Equivalent to: (std::cout << "Enter two numbers:") << std::end;   ...

C++ Primer Sec. 2.6 Defining our own data structures

Defining our own data structures Class definition must have ; at the end. Preprocessor The most common technique for making it safe to include a header multiple times relies on the preprocessor. #include Header guards #ifndef SALES_DATA_H #define SALES_DATA_H #include struct Sales_data { std::string bookNo; unsigned units_sold = 0; double revenue = 0.0; }; #endif SALES_DATA_H can be any name, just it has to be unique throughout the program. preprocessor variables usually are written in all uppercase.

C++ Primer Sec. 2.3 Compound Types

Compound Types References Reference is not an object. It is an alias. It is just another name for an already existing object. int ival = 1024; int &refVal = ival; //refVal referes to (is another name for) ival int &refVal2; // error: a reference must be initialized When we define a reference, instead of copying the initializer's value, we bind the reference to its initializer. Once initialized, a reference remains bound to its initial object. There is no way to rebind a reference to refer to a different object. Because there is no way to rebind a reference, references must be initialized. When we assign to a reference, we are assigning to the object to which the reference is bound. int i = refVal; //OKay i= 1024 Because references are not objects, we may not define a reference to a reference. We can define multiple references in a single definition. Each identifier that is a reference must be preceded by the & symbol: int ...