Skip to main content

Posts

What are Smart, Unique and Weak Pointer and when to use them

  Unique pointers  provide exclusive ownership of an object, preventing copying but allowing movement. They automatically release memory when the pointer goes out of scope, making them ideal for single ownership scenarios and resources like file handles. Shared pointers  enable multiple owners of an object through reference counting. While flexible, they can introduce performance overhead and potential circular reference issues. Careful consideration is essential when using shared pointers, especially in complex object structures. Weak pointers  do not own the object they reference but instead observe its lifetime. Primarily used to break circular dependencies between shared pointers, weak pointers provide a way to check if a shared pointer still exists without affecting ownership.
Recent posts

Explain Polymorphism and Flavors of Polymorphism...

Polymorphism is the ability of different objects to react in an individual manner to the same message. This notion was imported from natural languages. For example, the verb "to close" means different things when applied to different objects. Closing a door, closing a bank account, or closing a program's window are all different actions; their exact meaning is determined by the object on which the action is performed. Most object-oriented languages implement polymorphism only in the form of virtual functions. But C++ has two more mechanisms of static (meaning: compile-time) polymorphism: Operator overloading. Applying the += operator to integers or string objects, for example, is interpreted by each of these objects in an individual manner. Obviously, the underlying implementation of += differs in every type. Yet, intuitively, we can predict what results are. Templates. A vector of integers, for example, reacts differently from a vector of string objects when it receives

Explain using static class member ..

A class member declared static is a single instance of that member shared by all instances of this class (that's why it is sometimes termed a class variable, as opposed to object variable). This feature has many uses: for instance, in order to create a file lock, one can use a static bool class member. An object trying to access this file has to check first whether the static (i.e., shared) flag is false. If it is, the object turns the flag on and processes the file safely, since other objects will now find that the flag is now on, and hence -- they cannot access the file. When the object processing the file is done, it has to turn off the flag, enabling another object to access it. How is a static member created? class fileProc { FILE *p; static bool isLocked; //only a declaration; see definition below... public: bool isLocked () const {return isLocked; } }; //somewhere outside the class definition: bool fileProc::isLocked; //definition; initialized to 'false' by defau

Explain keyword friend...

for A class can declare external functions or other classes as friends. Friendship grants full access to all of the grantor's members, even private and protected ones: void encrypt (string & rep) {/*..*/} //global function class spy { public: static void transmit(const string& rep) { /*..*/} //... }; class secret { friend class spy;//spy can access all members of 'secret' friend void encrypt(string & rep);//...and so can encrypt private: string report; public: void scramble() { ::encrypt(report); } void transmit() const { spy::transmit(report); } }; Notes about friendship: A friend declaration exposes implementations details of its class, so it should be used wisely. However, friendship has the advantage of allowing code re-use in a simple manner; in fact, many of the standard functions and overloaded operators are used in standard containers (like string<>) by means of friendship. Friendship is not inherited, so non-public members of a

Explain Mutable Object Member or data member

When a data member is declared mutable, then it is legal to assign a value to it from a const member function. The following example may demonstrate when it is needed: class Buffer { void * data; //raw data buffer to be transmitted on the net size_t size; //size of the data mutable int crc; //used to verify that no errors occurred during transmission; //a mutable variable is allowed to be modified from a const //member function. public: //... int GetCrc() const; void Transmit() const; //copmutation of crc_val should be done here }; void f() { Buffer buffer; //...fill buffer with data buffer.Transmit(); //crc can be modified here; other members may not } Class buffer uses a data buffer transmitted via communication link after it has been filled. The filling process is quite slow and repetitive, so there's no point in calculating the value of crc every time a few bytes are appended to data. On the other hand, the receiving size has to get the right crc of the data in order

Explain Calling an object's member function from its constructor

An object's member functions (both virtual and non-virtual) can be called from its constructor. The invoked virtual is guaranteed to be the one defined in the current object (or higher, if it has not been overridden in the current object). However, virtuals of objects derived from the one whose constructor is being executed are not called. class A { public: virtual void f() {} virtual void g() {} }; class B: public A { public: void f () {} //overriding A::f() B() { f(); //calls B::f() g(); //g() not overriden in B, therefore calling A::g() } }; Mind that if the object's member functions use object data members, it is the implementor's responsibility to initialize them first, preferably by a mem-initializer list: class C { int n; int getn() const { cout< public: C(int j) : n(j) { getn(); } //Fine: n initialized before getn() //is called; otherwise - n would //have an undefined value };

explain Static member function

A static member function in a class can access only other static members of a class or variables which are not members of its class. It can be invoked even without an object instance, unlike any other member functions: class stat{ int num; public: stat(int n = 0) {num=n;} static void print() {cout <<"static member function" < }; void main() { stat::print(); //no object instance required stat s(1); s.print(); //but still can be called from an object }//end main Static members are used when all other data members of an object are also static; when the function does not depend on any other object member (like print() above); or simply when a global function is undesirable so it is wrapped in a class.