摘要:
In C++, delete operator should only be used either for the pointers pointing to the memory allocated using new operator or for a NULL pointer, and free() should only be used either for the pointers pointing to the memory allocated using malloc() or for a NULL pointer. 1 #include 2 #include 3 int... 阅读全文
摘要:
Following are the differences between malloc() and operator new. (1)new calls constructors, while malloc() does not. In fact primitive data types (char, int, float.. etc) can also be initialized with new. For example, below program prints 10. 1 #include 2 3 using namespace std; 4 5 int main... 阅读全文
摘要:
Ideally delete operator should not be used for this pointer. However, if used, then following points must be considered. (1)delete operator works only for objects allocated using operator new (See http://geeksforgeeks.org/?p=8539). If the object is created using new, then we can do delete this,... 阅读全文
摘要:
In C++, this pointer is passed as a hidden argument to all non-static member function calls. The type of this depends upon function declaration. If the member function of a class X is declared const, the type of this is const X* (see code 1 below), if the member function is declared volatile, th... 阅读全文
摘要:
The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. 'this' pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available 阅读全文
摘要:
Predict the output of following C++ program: 1 #include 2 using namespace std; 3 4 class A 5 { 6 public: 7 A() { cout 2 using namespace std; 3 4 class A 5 { 6 int x; 7 public: 8 A() 9 { 10 cout 2 using namespace std; 3 4 class A 5 { 6 int x; 7 public: 8 A(... 阅读全文
摘要:
在C++中,类的静态成员函数有以下几点需要注意: (1)静态成员函数没有this指针 例如,下面的程序有编译错误"`this’ is unavailable for static member functions"。 1 #include 2 class Test 3 { 4 static Test * fun() 5 { 6 return this; // compiler error 7 } 8 }; 9 10 int main()11 {12 getchar();13 return 0;14 } (2)静态成... 阅读全文