C ++ basics

zhaozj2021-02-12  172

There are a lot of knowledge of C , there are four library books "C programming", "c primer", this article just wants to talk about the knowledge you have seen by I personally. Plus your own understanding. Many things may not systematically, and knowledge is also thinking about it.

First talk about the size of the object,

Class A

{

}

Main ()

{

A a a;

Cout << SizeOf (a) << endl;

}

What is its result?

The answer is 1.

why? It does not define any variables, according to the truth, should be 0. This is to ensure that the memory address of each object is unique. such as:

A a, b;

If the length is 0, then the memory address of A, B may be one, so that some unnecessary trouble will occur.

Then

Class A

{

Char M;

}

Main ()

{

A a a;

Cout << SizeOf (a) << endl;

}

How much is the result of printing.

The answer is 4

This question is called alignment. Once, I will encounter such depressed problems. The code in our project is transmitted with array, such as coordinates X, Y, then this assignment.

* (int *) & w [0] = x;

* (int *) & w [4] = y;

(Perhaps everyone is very small), such code has a shortcomings, sending data, taking data is very inconvenient, and because there is a subscript, therefore, the code is not easy to use the loop structure (still not four data). Only one assignment, there is a problem, that is, when the project is agreed, all the code must be changed. Not good Reuse.

Therefore, I want to transform the program code by means of structural strcut, or class, but encounter data alignment problems.

For example, I define a structure

Struct a

{

CHAR TYPE;

Int Y;

}

Then, use this.

CHAR BUF [MAX];

A a a;

Memcpy (BUF, & A, SIZEOF (A));

However, it is found that the contents of BUF [] is not what I need.

In fact, the content is shown as follows:

I have read some information, saying that this is for byte alignment, the CPU access data is in four digits, so that it will not take four digits, one is char Type, the three is the data of Y. As a programmer, it is not familiar with this.

There is also a similar problem, in the future, again.

Access to class data

There are three privileges: public, private, protected.

C is an object-oriented language, so many concepts are derived from real life.

For example, this permission issue can be explained so.

The basic class is equivalent to the father, the subclasses are equivalent to the child, and there is a friend class, which is equivalent to friends.

For example, for your father's thing (member variable) is shared by anyone, everyone can know, not a secret. Then, use public to describe such something.

If, some things are just not known, that is, son, good friends can share the secrets of sharing, then such something with protected. Similarly, there are things, adults don't want children to know, but they are not retained for good friends, such things, in real life, then, this is described in this kind of private.

Welcome everyone to criticize, guide,

Thank you for checking.

Contact: Information Industry Electronics 28 Institute

E_mail: taozhangzhi@sohu.com

转载请注明原文地址:https://www.9cbs.com/read-6232.html

New Post(0)