C ++ proverbs: Understanding Terminology terms

Source: Internet
Author: User

This is a small C ++ vocabulary that all programmers should understand. The entries below are important enough to ensure that they are fully consistent.

The declaration tells the compiler about the name and type of something, but it skips some details. The following are declarations:

extern int x; // object declaration

std::size_t numDigits(int number); // function declaration

class Widget; // class declaration

template<typename T> // template declaration
class GraphNode; // (see Item 42 for info on
// the use of "typename")

Note that even for built-in types, I prefer to regard integers as an "object". Some people keep the "object" name for user-defined types, but I am not one of them. The return type of the numDigits function is std: size_t, that is, size_t type in the namespace std. This namespace is where everything in the C ++ standard library is actually located. However, because the C Standard Library (strictly speaking, from C89) can also be used in C ++, symbols inherited from C (such as size_t) may exist in the global scope, std, or both, depending on which header file is # pinned ded. In this book, I assume that the C ++ header file is # encoded ded, which is why I replaced size_t with std: size_t. When the standard library components are involved in the line, I generally do not mention std, which relies on your approval that everything like size_t, vector, and cout is in std. In the sample code, I always include std, because the real Code cannot be compiled without it.

By the way, size_t is only a type of unsigned typedef for C ++ to count something (for example, the number of characters in a string based on char, number of elements in an STL container, and so on ). It is also the type held by the operator [] function of vector, deque, and string. This is a convention to be followed when we define our own operator [] function in Item 3.

Each function declaration indicates its signature, that is, its parameters and return types. A function's signature is of the same type. For numDigits, the identification mark (signature) is std: size_t (int), that is, "the function holds an int and returns a std: size_t ". The official "signature" C ++ definition does not exclude the return type of the function, but in this book, it is more useful to consider the return type as part of the recognition flag.

Definition provides the compiler with details that are omitted during declaration. An object is defined as a place where the compiler sets aside memory for the object. Provides code Ontology for a function or a function template. For a class or a class template, the definition lists the class or template members:

int x; // object definition

std::size_t numDigits(int number) // function definition.
{
  // (This function returns
  std::size_t digitsSoFar = 1; // the number of digits
  // in its parameter.)
  while ((number /= 10) != 0) ++digitsSoFar;
  return digitsSoFar;
}

class Widget {
  // class definition
  public:
   Widget();
   ~Widget();
  ...
};

template<typename T> // template definition
class GraphNode {
public:
  GraphNode();
  ~GraphNode();
  ...
};

Initialization is the process of setting the first value of an object. For user-defined objects, initialization completes the task through the constructor. The default constructor is a constructor that can be called without any arguments. Such a constructor can be neither a parameter nor a default value for each parameter:

class A {
public:
  A(); // default constructor
};

class B {
public:
  explicit B(int x = 0, bool b = true); // default constructor; see below
}; // for info on "explicit"

class C {
public:
  explicit C(int x); // not a default constructor
};

Here, constructor B and C are declared as explicit ). This is to prevent them from being used for implicit conversions (implicit type conversions), although they can also be used for explicit conversions ):

void doSomething(B bObject); // a function taking an object of
// type B

B bObj1; // an object of type B

doSomething(bObj1); // fine, passes a B to doSomething

B bObj2(28); // fine, creates a B from the int 28
// (the bool defaults to true)

doSomething(28); // error! doSomething takes a B,
// not an int, and there is no
// implicit conversion from int to B

doSomething(B(28)); // fine, uses the B constructor to
// explicitly convert (i.e., cast) the
// int to a B for this call. (See
// Item 27 for info on casting.)

Constructor declared as explicit (explicit) is generally more desirable than non-explicit (non-explicit) because they can prevent compiler execution exceptions (often unintentional) type conversion. Unless I have a good reason to allow a constructor to be used for implicit conversions (implicit type conversions), I declare it as explicit ). I hope you can follow the same rule.

Constructor declared as explicit (explicit) is generally more desirable than non-explicit (non-explicit) because they can prevent compiler execution exceptions (often unintentional) type conversion. Unless I have a good reason to allow a constructor to be used for implicit conversions (implicit type conversions), I declare it as explicit ). I hope you can follow the same rule.

Note how I highlight the forced cast in the above sample code. Throughout this book, I use this to highlight the materials you should pay attention. (I also highlight the chapter number, but it is only because I want it to look better .)

Copy constructor is used to initialize another object of the same type as an object, and copy assignment operator) it is used to copy the values of an object to another object of the same type:

class Widget {
public:
  Widget(); // default constructor
  Widget(const Widget& rhs); // copy constructor
  Widget& operator=(const Widget& rhs); // copy assignment operator
  ...
};
Widget w1; // invoke default constructor
Widget w2(w1); // invoke copy constructor
w1 = w2; // invoke copy
// assignment operator

When you see something that looks like a value assignment, read it carefully, because "=" can also be used to call the copy constructor in syntax:

Widget w3 = w2; // invoke copy constructor!
Fortunately, the copy constructor is easily distinguished from the copy assignment. If a new object is defined (like w3 in the above line of code), a constructor must be called; it cannot be a value assignment. If no new object is defined (like in the above line "w1 = w2" code), no constructor can be called, so it is a value assignment.

A copy constructor is a very important function because it defines how an object is passed by passing values. For example, consider this:

bool hasAcceptableQuality(Widget w);

...
Widget aWidget;
if (hasAcceptableQuality(aWidget)) ...

The parameter w is passed to hasAcceptableQuality by passing the value. Therefore, in the preceding call, aWidget is copied to w. The copy action is executed by the copy constructor of the Widget. Passing the value means "calling the copy constructor ". (In any case, passing user-defined types by passing values is usually a bad idea. Passing a reference to a const is usually a better choice .)
STL is a Standard Template Library. As part of the C ++ Standard Library, STL is dedicated to containers (such as vector, list, set, map, and so on ), iterators (for example, vector <int>: iterator, set <string>: iterator, etc.), algorithms (algorithms) (for example, for_each, find, sort, and other functions. Many related functions are provided through function objects, which act as objects similar to functions. Such objects come from classes that overload operator () -- function call operators --. If you are not familiar with STL, you should have a decent reference manual for future reference when reading this book, because STL is so useful for me that I can't help but use it. Once you use it a little bit, you will feel the same way.

Programmers who come to C ++ from languages like Java or C # may be surprised by the concept of undefined behavior. For various reasons, constructs in C ++ do not have a definite definition: you cannot reliably predict what will happen during runtime. Here are two examples of codes with undefined behaviors:

int *p = 0; // p is a null pointer

std::cout << *p; // dereferencing a null pointer
// yields undefined behavior
char name[] = "Darla"; // name is an array of size 6 (don’t
// forget the trailing null!)

char c = name[10]; // referring to an invalid array index
// yields undefined behavior

To emphasize that undefined behaviors are unpredictable and may be annoying, experienced C ++ programmers often say that programs with undefined behaviors can delete your hard disk. This is true: a program with undefined behaviors can delete your hard disk. However, the possibility is not very high. What's more likely is that the program is capricious, sometimes running normally, sometimes completely finished, and sometimes produces incorrect results. Powerful C ++ programmers can avoid undefined behaviors at their best. In this book, I will point out many things you must pay attention.

The other may

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.