随笔分类 - C++(Geeks For Geeks)
摘要:A class declared inside a function becomes local to that function and is called Local Class in C++. For example, in the following program, Test is a local class in fun(). 1 #include 2 using namespace std; 3 4 void fun() 5 { 6 class Test // local to fun 7 { 8 /* members of Te...
阅读全文
摘要:Ever wondered how can you design a class in C++ which can’t be inherited. Java and C# programming languages have this feature built-in. You can use final keyword in java, sealed in C# to make a class non-extendable. Below is a mechanism using which we can achieve the same behavior in C++. It make...
阅读全文
摘要:C allows a void* pointer to be assigned to any pointer type without a cast, whereas C++ does not; this idiom appears often in C code using malloc memory allocation. For example, the following is valid in C but not C++:void* ptr;int *i = ptr; /* Implicit conversion from void* to int* */ or similar...
阅读全文
摘要:In C, it might not be possible to have function names on left side of an expression, but it’s possible in C++. 1 #include 2 using namespace std; 3 4 /* such a function will not be safe if x is non static variable of it */ 5 int &fun() 6 { 7 static int x; 8 return x; 9 } 10 11 int main...
阅读全文
摘要:In C, we cannot access a global variable if we have a local variable with same name, but it is possible in C++ using scope resolution operator (::). 1 #include 2 using namespace std; 3 4 int x; // Global x 5 6 int main() 7 { 8 int x = 10; // Local x 9 cout<<"Value of global x is "<
阅读全文
摘要:Calling an undeclared function is poor style in C (See this) and illegal in C++. So is passing arguments to a function using a declaration that doesn’t list argument types: If we save the below program in a .c file and compile it, it works without any error. But, if we save the same in a .cpp fil...
阅读全文
摘要:说实话,学习C++以来,第一次听说"Metaprogramming"这个名词。 Predict the output of following C++ program. 1 #include 2 using namespace std; 3 4 template struct funStruct 5 { 6 enum { val = 2*funStruct::val }; 7 }; 8 9 template struct funStruct10 {11 enum { val = 1 };12 };13 14 int main()15 {16 cout ::v...
阅读全文
摘要:Default parameters for templates in C++: Like function default arguments, templates can also have default arguments. For example, in the following program, the second parameter U has the default value as char. 1 #include 2 using namespace std; 3 4 template class A 5 { 6 public: 7 T x; 8 ...
阅读全文
摘要:Function templates and static variables: Each instantiation of function template has its own copy of local static variables. For example, in the following program there are two instances: void fun(int ) and void fun(double ). So two copies of static variable i exist. 1 #include 2 using names...
阅读全文
摘要:We have discussed assignment operator overloading for dynamically allocated resources here . This is a an extension of the previous post. In the previous post, we discussed that when we don’t write our own assignment operator, compiler created assignment operator does shallow copy and that cause p..
阅读全文
摘要:In C++, the programmer abstracts real world objects using classes as concrete types. Sometimes it is required to convert one concrete type to another concrete type or primitive type implicitly. Conversion operators play smart role in such situations. For example consider the following class 1 ...
阅读全文
摘要:Predict the output of following C++ program. 1 #include 2 3 using namespace std; 4 5 class Complex 6 { 7 private: 8 double real; 9 double imag;10 11 public:12 // Default constructor13 Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) 14 {15 }16 17 ...
阅读全文
摘要:The answer is same as Copy Constructor. If a class doesn’t contain pointers, then there is no need to write assignment operator and copy constructor. The compiler creates a default copy constructor and assignment operators for every class. The compiler created copy constructor and assignment oper...
阅读全文
摘要:Difficulty Level: Rookie Consider the following C++ program. 1 #include 2 #include 3 4 using namespace std; 5 6 class Test 7 { 8 public: 9 Test() 10 {11 }12 Test(const Test &t)13 {14 cout<<"Copy constructor called "<<endl;15 }16 Test& operator = (const Test &...
阅读全文
摘要:In C++, RTTI (Run-time type information) is available only for the classes which have at least one virtual function. For example, dynamic_cast uses RTTI and following program fails with error “cannot dynamic_cast `b’ (of type `class B*’) to type `class D*’ (source type is not polymorphic) ” becau...
阅读全文
摘要:这个不懂,等看会了再写。。。
阅读全文
摘要:Deleting a derived class object using a pointer to a base class that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor. Source: https://www.securecoding.cert.org/confluence/display/cplusplus/OOP34-CPP....
阅读全文
摘要:Can we make a class constructor virtual in C++ to create polymorphic objects? No. C++ being static typed (the purpose of RTTI is different) language, it is meaningless to the C++ compiler to create an object polymorphically. The compiler must be aware of the class type to create the object. In...
阅读全文
摘要:We have discussed a similar topic in Java here. Unlike Java, C++ allows to give more restrictive access to derived class methods. For example the following program compiles fine. 1 #include 2 using namespace std; 3 4 class Base 5 { 6 public: 7 virtual int fun(int i) 8 { 9 }10 };1...
阅读全文
摘要:In C++, a static member function of a class cannot be virtual. For example, below program gives compilation error. 1 #include 2 using namespace std; 3 4 class Test 5 { 6 public: 7 // Error: Virtual member functions cannot be static 8 virtual static void fun() 9 { 10 ...
阅读全文
浙公网安备 33010602011771号