C + + Written test-1, basic theory

Source: Internet
Author: User

The test paper is to learn from other blog content, recorded in the back to facilitate their own reading review

I. What is the use of const:

Three:

1. Modifying variables, defining read-only variables

2. modifier function parameter or function return value

3. A member function that modifies a class that cannot modify the value of a member variable.

Two. Differences in pointers and references

Reference Links:

Http://www.cnblogs.com/dolphin0520/archive/2011/04/03/2004869.html

Http://www.cnblogs.com/hoodlum1980/archive/2012/06/19/2554270.html

Http://c.biancheng.net/cpp/biancheng/view/169.html

1. The reference is the alias of the variable, the internal implementation is a read-only pointer, and the reference in C + + is implemented by pointers by the compiler, but the implementation transparently handles the programmer at the language level.

The pointer is itself an object that allows the pointer to be assigned and copied

2. References can only be assigned at initialization time, other times cannot be changed, pointers do not have this limit

3. Reference cannot be null, pointer can be null

4. Pointers can have multiple levels and references can be only one level

5.sizeof refers to the size of the pointed variable, and the sizeof pointer gets the size of the pointer itself.

6. When the pointer is passed as a function parameter, the copy of the argument is passed to the formal parameter, such as the following program (a), the reference is passed as a function parameter instead of the copy, and the reference is a true share of the same inner deposit element.

Program (a):

#include <iostream>
#include <stdlib.h>
#include "stdafx.h"
using namespace Std;

void swap (int *a, int *b)
{
int tmp = *A;
*a = *b;
*B = tmp;
int c = 10;
A = &c;
cout << "swap function" << Endl;
cout << "A" is "<< *a <<" "<<" A has a value of "<< a << Endl;
}

int main ()
{
void swap (int *a, int *b);
int a = 4;
int b = 5;
cout << "Pre-change" << Endl;
cout << "A" is "<< &a <<" "<<" A has a value of "<< a << Endl;
cout << "B" is "<< &b <<" "<<" B with the value "<< b << Endl;
Swap (&a, &b);
cout << after "changed" << Endl;
cout << "A" is "<< &a <<" "<<" A has a value of "<< a << Endl;
cout << "B" is "<< &b <<" "<<" B with the value "<< b << Endl;
return 0;
}

The results are as follows:

When pointer A is actually a function argument, it is equivalent to generating a new pointer xxx, except that the pointer xxx points to the same address as the argument pointer A. So when the address of the variable C is assigned to pointer a in the function, it is actually changing the address of the pointer xxx, so the function A = &c; does not affect the real pointer a at all.

C + + Written test-1, basic theory

Related Article

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.