C++ Some general Questions
Q) we can overload assignment operator as a normal function.But we can not overload assignment operator as friend function why?
Ans:) If the operation modifies the state of the class object, it operates on, it must be a member function, not a friend fucntionThus all operator such as =, *=, +=, etc are naturally defined asmember functions not friend functionsConversely, if the operator does not modify any of its operands,but needs only a representation of the object, it does not have tobe a member function and often less confusing.This is the reason why binary operators are often implemented asfriend functions sunch as + , *, -, etc..
Q Can you explain the term "resource acquisition is initialization?"
Ans ) Resource Acquisition is Initialisation or shortly RAII is a term closely connected with exception handling and automatic garbage collection(speaking loosely). RAII is defined as the technique of performing all necessary resource initialisation work right within the body of the constructor itself, and correspondingly doing all the deallocation within the destructor. Since during exception handling destructors of all the classes whose constructors have been successfully invoked, are called therefore all the local objects previously allocated are automatically deallocated without explicitly calling their destructors.the following example explains the RAII technique:class X{ int *r; public: X(){cout<<"X is created"; r=new int[10]; } ~X(){cout<<"X is destroyed"; delete [] r; }};class Y{ public: Y(){ X x; throw 44; } ~Y(){cout<<"Y is destroyed";}};Now since Y throws an exception right in its constructor therefore its destructor won't get called becuse the constructor was not invoked successfully. But the destructor of the class X will definitely be called and therefore the array 'r' will be deallocated. So we didn't have to care about resource deallocation at the time exceptions are raised.
Ans:) If the operation modifies the state of the class object, it operates on, it must be a member function, not a friend fucntionThus all operator such as =, *=, +=, etc are naturally defined asmember functions not friend functionsConversely, if the operator does not modify any of its operands,but needs only a representation of the object, it does not have tobe a member function and often less confusing.This is the reason why binary operators are often implemented asfriend functions sunch as + , *, -, etc..
Q Can you explain the term "resource acquisition is initialization?"
Ans ) Resource Acquisition is Initialisation or shortly RAII is a term closely connected with exception handling and automatic garbage collection(speaking loosely). RAII is defined as the technique of performing all necessary resource initialisation work right within the body of the constructor itself, and correspondingly doing all the deallocation within the destructor. Since during exception handling destructors of all the classes whose constructors have been successfully invoked, are called therefore all the local objects previously allocated are automatically deallocated without explicitly calling their destructors.the following example explains the RAII technique:class X{ int *r; public: X(){cout<<"X is created"; r=new int[10]; } ~X(){cout<<"X is destroyed"; delete [] r; }};class Y{ public: Y(){ X x; throw 44; } ~Y(){cout<<"Y is destroyed";}};Now since Y throws an exception right in its constructor therefore its destructor won't get called becuse the constructor was not invoked successfully. But the destructor of the class X will definitely be called and therefore the array 'r' will be deallocated. So we didn't have to care about resource deallocation at the time exceptions are raised.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home