Skip to main content

Posts

Showing posts from October, 2017

std::auto_ptr in c++

Automatic Pointer [deprecated] Note: This class template is deprecated as of C++11 and completely removed in c++ 17. unique_ptr is a new facility with a similar functionality, but with improved security (no fake copy assignments), added features (deleters) and support for arrays. auto_ptr objects have the uniqueness of taking ownership of the pointers allotted to them: An auto_ptr object that has ownership over one element is in charge of destroying the element it points to and to deallocate the memory allocated to it when itself is destroyed. The destructor does this by invoking operator delete automatically. Hence, no two auto_ptr objects should own the same element, since together would try to destruct them at some point. When an assignment operation takes place between two auto_ptr objects, ownership is transferred, which means that the object losing ownership is set to no longer point to the element (it is set to the null pointer). #include void memory_leak(...

Compiler Generated Default Constructor with default keyword in in c++ | C++ 11

Compiler Generated Default Constructor : In C++ 3.0 :  Compiler results an error if there any other constructor and missing default constructor and you try to invoke default constructor. class CAT {     CAT (int age) {} }; CAT  d1;  // Error: compiler will not generate the default constructor // C++ 11:  With the default keyword, it forced the compiler to generate default constructor even if the is any other constructor. class  CAT  {     CAT (int age);     CAT () = default;    // Force compiler to generate the default constructor };

final keyword in c++ | C++ 11

final keyword (for virtual function and for class) : Making class final make sure that the no class can derived from final class. class CAT final {     // no class can be derived from CAT    ... };     Making virtual function final make sure that the no class can override final virtual function. class Dog {    virtual void FUNC() final;  // No class can override FUNC()  };

override keyword in c++ | C++ 11

Override Keyword (for virtual function) : // C++ 03 class CAT {    virtual void A(int);    virtual void B() const; } class YellowCAT : public CAT {    virtual void A(float);  // Created a new function    virtual void B(); // Created a new function  } In C++, the intention of user was to override both function A and B, but since the function signature mismatch in function A and const keyword mismatch in function B. hence function A and B will not get override at run time. // C++ 11 class CAT {    virtual void A(int);    virtual void B() const;    void C(); } class YellowCAT : public CAT {    virtual void A(float) override;  / / Error: no function to override    virtual void B() override;        // Error: no function to override    void C() override;               // Error: not a virtual ...

Delegating Constructor in c++ | C++ 11

Delegating Constructor : In Java, we can one constructor  from another constructor, which is not possible in c++ class Cat  {    public:    Cat() { ... }    Cat(int a) { Cat(); doOtherThings(a); } }; // But in case of C++ 03, we achieve this through creating a new function, class Cat  {    init() { ... };    public:    Cat() { init(); }    Cat(int a) { init(); doOtherThings(); } }; // With C++ 11, we can call constructor through an initializer list, also we can perform initialization for class data members. class Cat {    int age = 9;    public:    Cat() { ... }    Cat(int a) : Cat() { doOtherThings(); } }; Limitation: Cat() has to be called first. i.e. The constructor can't be called in middle or at the last in the other constructor.

Enum class in c++ | C++ 11

C++ 3.0 : Unscoped Enum: // unscoped enum:   enum [identifier] [: type]   {enum-list};         Example:   //NonScoped enum : namespace NonScoped   {       enum Enum { num1, num2, num3, num4 };          void EnumFunction(Enum n_enum)       {           if (n_enum == num1)          { /*...*/           }       }   }   Casting Rules : 1. In Unscoped enum an int is never implicitly convertible to an enum value. int account_num = 135692;   Enum hand;   hand = account_num; // error C2440: '=' : cannot convert from 'int' to 'Enum'   2. A cast is required to convert an int to a scoped or unscoped enumerator. hand = static_cast (account_num); // OK, but probably a bug!!!  3. You can promote a unscoped enumerator to an integer value without a...

nullptr in c++ | C++ 11

nullptr :   In c++ NULL value is integer 0, hence leads to an ambiguity when we invoke below function. void foo(int i) { cout << "foo_int" << endl; } void foo(char* pc) { cout << "foo_char*" << endl; } int main() {    foo(NULL);    // Ambiguity    // C++ 11    foo(nullptr); // call foo(char*) }

Foreach in C++ | C++ 11

  C++ 03:     for (vector ::iterator itr = v.begin(); itr!=v.end(); ++ itr)       cout << (*itr); C++ 11:   for (auto i: v) { // works on any class that has begin() and end()       cout << i ;    // readonly access    }    for (auto& i: v) {       i++;                 // changes the values in v     }                       // and also avoids copy construction Auto Syntax:    auto x = begin(v);   // Same as: int x = v.begin();    int arr[4] = {3, 2, 4, 5};    auto y = begin(arr); // y == 3    auto z = end(arr);   // z == 5     // How this worked? Because begin() and end() are defined for array. // Adapt your code to third party library by defining begin() and end()  for their c...

Uniform Initialization | C++ 11

C++ 03 :  class Dog {     // Aggregate class or struct    public:       int age;       string name; }; Dog d1 = {5, "Henry"};   // Aggregate Initialization Uniform Initialization: Uniform Initialization expands on the Initializer List syntax, to provide a syntax that allows for fully uniform type initialization that works on any object – removing the distinction between initialization of aggregate + non-aggregate classes, arrays, STL/custom collection classes, and PODs. // C++ 11 extended the scope of curly brace initialization class Dog {    public:       Dog(int age, string name) {...}; }; Dog d1 = {5, "Henry"};  Uniform Initialization Search Order: 1. Initializer_list constructor 2. Regular constructor that takes the appropriate parameters. 3. Aggregate initializer. ==> Dog d1{3}; Example: class Dog {    public:    int age;   ...

Initializer list in C++

C++ 11 Initializer : // C++ 03 class Dog {     // Aggregate class or struct    public:       int age;       string name; }; Dog d1 = {5, "Henry"};   // Aggregate Initialization // C++ 11 Initializer extended the scope of curly brace initialization. class Dog  {    public:       Dog(int age, string name) {...}; }; Dog d1 = {5, "Henry"};  /* Uniform Initialization Search Order:  * 1. Initializer_list constructor  * 2. Regular constructor that takes the appropriate parameters.  * 3. Aggregate initializer.  */ Dog d1{3}; class Dog  {    public:    int age;                                // 3rd choice    Dog(int a)     {                            /...

Auto keyword | C++ 11 | C++ 14

The auto keyword was a way to explicitly specify that a variable should have automatic duration: int main() {     auto int foo(5); // explicitly specify that foo should have automatic duration     return 0; } Auto in C++  11:  In C++11, the meaning of the auto keyword has changed, consider the following statement: double d = 5.0; Starting with C++11, When initializing a variable, the auto keyword can be used in place of the variable type to tell the compiler to infer the variable’s type from the initializer’s type. This is called type inference (also sometimes called type deduction). For example: auto d = 5.0; // 5.0 is a double literal, so d will be type double auto i = 1 + 2; // 1 + 2 evaluates to an integer, so i will be type int This even works with the return values from functions: int add(int x, int y) {     return x + y; } int main() {     auto sum = add(5, 6); // add() returns an int, so sum ...