Code for C++ delete singleton instance

Answer:To delete singleton instance or object in C++ programs, we need to follow a proper design to delete singleton object in the class . Especially, when multiple users are using object of singleton class in c++ program. One of the bad situations we can think that what if one user has deleted singleton object while…

Read the full article

Design Singleton class for multi-threaded environment in C++

Answer: For c++ thread safe singleton class design for multi threaded environment, we need to synchronize the function that creates and returns singleton object. We will see the singleton class design without synchronization and with synchronization applying on function that create singleton object in C++ For example, in the below design “GetInstance()”  function is not…

Read the full article

this pointer in C++ – how does it work internally?

Answer: The this pointer in C++ is a constant pointer that stores address of an object of a class. The type of this pointer is the type of object it is pointing to.  For example, if we have a class named ‘sample’, the type of this pointer is ‘sample* const this’. (What is type of…

Read the full article

Design Singleton class in C++ with example – Simple Steps

Answer: Singleton class in C++ is the class that is designed to provide only one unique instance across the system. So, whenever we create c++ singleton instance, it will return the same instance created earlier if any. About method and variables for Designing Singleton class in C++ Singleton class requires below methods and variables. Private…

Read the full article

Difference between new and malloc in C++ – Including Performance

Difference between new and malloc in C++  – The answer of  interview question C++ malloc vs new heap memory allocation includes advantages of new operator over malloc function in C++ programming language. Also, discussion about performance of new and malloc, free() function and delete operators are included. Note that in a technical interview, generally, it is…

Read the full article

What is size of Empty Class in C++ with virtual function?

Answers contains the size of empty class in C++ with virtual function (4 or 8 bytes) and without virtual function (1 byte not 0 ) with reason and example. Answer: Size of empty class in C++ Size of empty class in C++ is 1 byte and not 0 byte without virtual function, whereas, with virtual…

Read the full article

How do you prevent heap allocation in C++ for a Class?

C++ Interview Question Description: Describe the concept to prevent heap allocation in C++ for a class i.e. object creation on heap. Means, dynamic memory allocation using new operator. Note that we don’t want to restrict object creation in stack in C++ class. So, explain how to prevent object creation on heap with a program example.…

Read the full article

What is difference between constructor and member function | By Example

Answer: Here is the difference between constructor and member function in C++ programming. Constructor name must be same as class name but functions cannot have same name as class name. C++ Code Example : Constructor does not have return type whereas functions must have. C++ Code Example : Member function can be virtual, but, there…

Read the full article

What default functions provided by compiler in C++?

Below are default functions provided by compiler  in C++ language if not implemented  in a class by a software developer. Default constructor Copy constructor Assignment operator Destructor NOTES Default constructor example: If we don’t write any constructor in a class including c++ copy constructor then default constructor provided by compiler will be called when we…

Read the full article

Why does our code run faster if we use initialization lists rather than assignment in class constructor?

Hint to understand the question: Notice the below class A for using initializer list and assignment operator in C++. Class constructor using initialization list: Class constructor using assignment operator: Answer: Using initialization list in constructor is the one step process i.e. it initializes objects at the moment it’s declared. It calls copy constructor. Whereas using…

Read the full article