This is a tough thing to remember, because it has never been used. To sum up, I will not check it on C ++ primer later.
Const pointers are classified into three types:
1. pointer to the const object (1) definition form: const double * PT; // const
Const is a New Keyword introduced in C ++, which brings great convenience to C ++ programming. The pointer to the const object and the const pointer are two very similar concepts. They are very easy to confuse for beginners. 1. pointer to the const
1. pointer to the const objectWe can use a pointer to modify the value of the object it refers to. However, if the pointer points to a const object, it is not allowed to use a pointer to change the const value it refers. C ++ requires that the
If the pointer points to a const object, you cannot use the pointer to change the const value. To ensure this feature, the C ++ language requires that the pointer to the const object must also have the const feature.
Therefore, assigning the
Const object
If you want to define a class object that will never change, you can define a const object as follows:
Const cbox standard (10.0, 10.0, 10.0 );
The value of the member variable of the standard object will never change. To ensure that
Non-reference formal parameters//交换函数,传递的实参只是参数的副本。并不能起到交换作用void swap(intint b) { int m = a; a = b; b = m;}Pointer parameters//可以起到交换作用void swap(int*aint*b) { intm *a; *a*b; *bm;}Reference formal parameters//可以起到交换作用void
In C + +, the Const object can only invoke the const member function, for what reason. Let's look at the following examples and analyze them to understand why:
#include
#include
using namespace std;
Class myint{
private:
int i;
Public:
Const char * P1;/pointer to the const object Char const * P2; // same as above Char * const P3; // const pointer Differences: What is after const is limited, for example, char const * P is limited (* P), (* P) is the memory that P points to
This article illustrates the method of implementing a Const object dynamically by C + +. Share to everyone for your reference. The specific methods are analyzed as follows:
First, create
In C + +, the const object is allowed to be created
In the use of C + + language development, if we want an object to be defined without being incorrectly modified, we can declare the object as a const object so that the data members of the object cannot be changed.
1. Define a Const object
Const
C + + Primer Learning Notes _24_ class and Data Abstraction (--static) with singleton mode, auto_ptr and singleton mode, const member function, const object, mutable modifierObjective"Example" writes out the five basic principles of
1. pointer to the const objectConst int * P;This P is a pointer to an int-type const object. Const limits the type pointed to by the pointer P rather than P itself. That is to say, P itself is not a const. You do not need to initialize it during
12. C ++-constructor and destructor call sequence, const member function, const object, 12. cconst
The Calling sequence of the constructor when a single object is created
1.First, determine whether the class of the object has a parent class.First
Inline can be placed in. cpp, but only this CPP file can use it at this time.To make a public object, you must put it in. H. If you do not want to put it in. H, you must copy one copy of each CPP file.In fact, even if it is put in. H, it is also a
A variable in a const object cannot be modified, even if a function in a const object cannot modify the value of a variable in that object#include using namespacestd;//-----------------------------------------------classTest { Public: intx;
Let's first look at a "strange" error:
# Include
# Include
Using namespace STD;
Class {
PRIVATE:
Int sad;
Public:
Void F () const;
Void F1 ();
// A (INT );
// ();
};
Void A: F () const {
Cout
}
Void A: F1 (){
Cout
We all know that the const object is used to declare a variable as a constant. It cannot be modified unless the conversion is displayed in the program.
This article reminds you of an implicit error and a solution to this problem.
Consider the
/*** book: "thinkinginc++" * function: Declare the keyword mutable, specify a specific data member can be changed in a const object * Time: September 11, 2014 07:47:07* Author: cutter_point*/ Class z{ int i; mutable int j;public: Z ();
I recently learned C ++ and used the copy constructor to raise some questions.
1 Class Test { 2 Private : 3 Int A; 4 Public : 5 Test (); 6 Test ( Const Test &T ){ 7 A = T.; 8 }; 9 ~ Test (); 10 Int Getdate (); /
const and static variables, which can be placed in the header fileconst objects are static by default, not extern, so they are declared and defined in the header file. Multiple CPP references the same header file and is not aware of each other, so
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.