Recent learning algorithms and brush problems are basically written in C + + program, in this process, found that C + + and Java have a lot of grammatical similarities, but there are a lot of different points, and these differences for programmers already mastered Java, understand C + + code may be a little difficult or even difficulty, After stepping through one pit after another, I decided to record these differences, which may not cover the whole, so this article will be updated continuously.
0, directory 1. New Keyword 2. Several ways to instantiate objects in C + + 3. C + + initialization expression 4. Colon 5 after the C + + constructor. A semicolon that is difficult to omit in C + +; 6. In C + +:: 7. *, &,->8 in C + +. Const One, new keyword before or after a C + + function
// C++中:MyClass* a = new MyClass();delete a;// Java中:MyClass a = new MyClass();
- The new keyword in C + + returns a pointer to an object after it is instantiated, so you need to define a pointer, whereas in Java the object is returned as a pointer (reference), but it is not necessary to display a defined pointer in Java.
Different points
- Objects instantiated with the New keyword in C + + need to use the DELETE keyword to manually free memory after use, and in Java we do not need to manually free up memory because of the garbage collection mechanism.
Two ways to instantiate objects in C + +
// 方式一MyClass* a = new MyClass();delete a;// 方式二MyClass b();
- The new keyword in mode one does three things: first get the memory space on the heap memory (like the malloc function), then call the constructor of the class, and finally return a pointer to that memory space, because it is the memory space that is allocated dynamically in the heap memory, so we need to call the Delete function to free this part of memory space.
- Mode two directly in the stack memory allocation of storage space, similar to the definition of the basic type, so there is no need to manually free memory space, but when the object information is large, this approach will consume a lot of memory, affecting program efficiency.
Iii. C + + initialization expressions
int a(10); // 等效于 int a = 10;
Unlike Java, C + +, in addition to using assignment statements to initialize variables, can also initialize variables using the above-mentioned initialization expressions, which have the same effect.
Iv. colons after C + + constructors
// 初始化表达式class MyClass { public: const int a; int b; MyClass(int aVal, int bVal):a(aVal), b(bVal) {}};// 非初始化表达式,无法通过编译,编译时会报以下错误:// ‘const int MyClass::a‘ should be initializedclass MyClass { public: const int a; int b; MyClass(int aVal, int bVal) { a = aVal; b = bVal; }};
This syntax is actually the initialization expression in the 4th above, but here we see that the following method of assignment in the constructor will give an error, which is why? Because here our colon initialization and assignment statements do things differently: when the colon is initialized, only the initialization expression of the corresponding variable after the colon is initialized when allocating memory for the member variable, so constant a initializes the memory when it is allocated, and then does not change the value, so there is no error. , and when an assignment statement is used, the constructor has allocated memory for a when it executes to the body of the function but has not initialized it, so the should be initialized compilation error is reported.
I was surprised at the first time I saw the magic notation.
V. C + + is difficult to omit the semicolon;
In the example above we see that the C + + class is defined with a semicolon appended to it, and we know that there is no need to add a semicolon after the curly braces defined in the Java class.
After the data found that:
In C + +, a semicolon must be added to the end of a statement or snippet except for a function and a precompiled instruction. Where the precompiled directive refers to a statement that begins with #. Common ones are, #include, #define, #ifdef, #if, #elif, #else, #endif等.
Vi. in C + +::(-scoped characters)
C + + 's scope:: Usage three, described as follows
Global scope character
int a = 3;void test() { int a = ::a; cout << a << endl;}
This is where the value of global variable A is accessed through the global scope, and then its value is assigned to the local variable A in the test function.
Class-Scoped characters
class MyClass { public:void sayHello();};void MyClass::sayHello() { cout << "hello world!" << endl;}
Here is a bit like Java in the definition of interfaces and implementation of the interface, first defined in the class definition of a function sayhello, but there is no specific function body, and then in the definition of the class, through the scope of the function SayHello before adding a MyClass::, equivalent to tell the compiler, This function is the concrete implementation of the SayHello method of the MyClass class.
namespace-scoped Characters
std::cout << "hello world!" << endl;
Here Std is the namespace, such usage is equivalent to using namespace std;
VII. C + +, *, &,
class MyClass { public: int a; MyClass(int aVal):a(aVal){}};int main () { MyClass obj(5); MyClass* objPointer = new MyClass(3); // 常规操作 cout << obj.a << endl; // 取指针操作 cout << (*objPointer).a << endl; // ->操作 cout << objPointer->a << endl; // 取地址操作 cout << (&obj)->a << endl; // new出来的对象别忘了释放内存哦 delete objPointer; return 0;}
In C + +:
- * represents the variable that accesses pointer A;
- & indicates the address of the obtained variable;
- . Represents a member variable or function that accesses an object; (same as Java)
-A member variable or function that accesses the object pointed to by the pointer;
The pointer content is too many, here do not repeat, interested in children's shoes can refer to the Novice tutorial
The const before or after the C + + function name
Const before the function name
const int getVal() { const int a = 3; return a;}int main() { cout << getVal() << endl;}
is to define the return type of the function, which specifies that the return variable type must be const.
Const after function name (used in member functions of the Class)
class MyClass { public:int a;MyClass(int aVal):a(aVal){}void sayHello() const { cout << "a value is: " << a << endl;}// 编译报错:assignment of member ‘MyClass::a‘ in read-only objectvoid changeA() const { a = 5;}};
The const representation after the function name is that the function is a constant member function , and the function cannot modify any member variable within the object, only read operations can occur, and write operations cannot occur. This can protect member variables.
Differences between C + + and Java syntax