Skip to main content

Disallow the use of compiler generated functions

/* C++ 11 Update: Delete keyword */
class dog {
   public:
   dog(const dog& ) = delete; // Prevent copy constructor from being used.
                              // Useful when dog holds unsharable resource.
}



/* For C++ 03:


Solution:
 Declare them as private functions, but not define them.
 -- Members and friends get link-time error, others get compile-time error.
*/
class dog {
   private:
   dog& operator=(const dog& rhs);   
};


// Note: the same techniques can used to disallow any function from being used.





Question: Can we disallow destructor from being used? 


/* Solution */
class dog {
  public:
     void destroyMe() { delete this; }
     void dog(string message) { cout<
  private:
     ~dog() { cout<< m_name.m_str << " is destroyed.\n" << endl; }
};

int main ()
{
  dog dog1("Dog Barks");
  dog1->destroyMe();  
}  

// Error : Though we have invoke function to delete object through delete, but still when function goes out of scope destructor gets called and results in runtime error.



Solution: Create pointer on heap


int main ()
{
  dog* dog1 = new dog("Dog Barks");
  dog1->destroyMe();
}

Comments

Popular posts from this blog

const used with functions

const used with functions : class Dog {    int age;    string name; public:    Dog() { age = 3; name = "dummy"; }         // const parameters and these are overloaded functions    void setAge(const int& a) { age = a; }    void setAge(int& a) { age = a; }         // Const return value    const string& getName() {return name;}         // const function and these are overloaded functions    void printDogName() const { cout << name << "const" << endl; }  // value of name can't be modified    void printDogName() { cout << getName() << " non-const" << endl; } }; int main() {    Dog d;    d.printDogName();        const Dog d2;    d2.printDogName();     }

Structured Bindings

Returning multiple Values from function C++ 11 vs C++ 17 Returning compound objects Iterating over a compound collection Direct initialization Returning multiple Values from function C++ 11 vs C++ 17 :  C++ 11 (std::tie): std::tuple mytuple() {     char a = 'a';     int i = 123;     bool b = true;     return std::make_tuple(a, i, b);  // packing variable into tuple } To access return value using C++ 11, we would need something like: char a; int i; bool b; std::tie(a, i, b) = mytuple();  // unpacking tuple into variables Where the variables have to be defined before use and the types known in advance. C++ 17 : auto [a, i, b] = mytuple(); Returning compound objects :  This is the easy way to assign the individual parts of a compound type (such as a struct, pair etc) to different variables all in one go – and have the correct types automatically assigned. So let’s have a look at an ...

STL :: std::array<> in c++

Example:          int a[5] = {10,20,30,40,50}; array b; std:copy(std::begin(a),std::end(a),std::begin(b)); for(int x : b) { cout< } cout<<"begin functional iterator : "< for(auto val = b.begin(); val cout<<*val< cout<<"R begin functional iterator : "< for(auto val = b.rbegin(); val cout<<*val< cout<<"AT Function : "< cout<<"at(2) : "< cout<<"front Function : "< cout<<"front() : "< cout<<"back Function : "< cout<<"back() : "< cout<<"[] operator : "< cout<<"b[2] : "<