- Constructor is a special member function that is automatically called by compiler when object is created and destructor is also special member function that is also implicitly called by compiler when object goes out of scope. They are also called when dynamically allocated object is allocated and destroyed, new operator allocates storage and calls constructor, delete operator calls destructor and free the memory allocated by new.
- Constructors and destructors both can be called implicity and explicitly.
- Explicit call to destructor is only necessary when object is placed at particular location in memory by using placement new. Destructor should not be called explicitly when the object is dynamically allocated because delete operator automatically calls destructor.
class A
{
public:
A()
{
}
~A()
{
}
};
void Func()
{
char mem[sizeof ( A )];
void* p = mem;
A* f = new ( p ) A(); // f points to mem
f->~A(); // Explicitly call the destructor
}
4. To use auto_ptr<A> b(new A) , use <memory> header.
5. Copy Constructor and Assignment operator are provided default by compiler.
6. C++ has direct access to low-level functionality provided by the operating system. This is one of the reasons C++ can produce higher performance code.
7. Class variables are called static variables. There is only one occurrence of a class variable.8. Instance variables are non-static and there is one occurrence of an instance variable in each class instance (i.e. each object). Also known as a member variable or a field.
9. Idempotent methods are methods, which are written in such a way that repeated calls to the same method with the same arguments yield same results.
10. By default all the functions defined inside the class are implicitly or automatically considered as inline except virtual functions (Note that inline is a request to the compiler and its compilers choice to do inlining or not).
11.Static variables are initialized when complied. And compiler will allocate memory to all the global and static variables in BSS(Block Started by symbol) segment before the main function is executed. Main function is not the first thing we do when we execute a program.
12. Static object are initialized only once and live until the program terminates.
13. Local Objects are called stack based or automatic objects
14. Global static objects are called and initialized before main.
15. References in C++ cannot be used for implementing data structures like Linked List, Tree.
16. main is not a reserved word in C++.
17. A callback is a pointer to a function, so if you want a processing function to notify you about some event you pass a pointer to another function (the callback) to the processing function.The processing function then calls the callback when appropriate
18. Callbacks have two fundamental flaws: Firstly, they are not type-safe. We can never be certain that the processing function will call the callback with the correct arguments. Secondly, the callback is strongly coupled to the processing function since the processing function must know which callback to call.
19. Pre increment/decrement can be used a l- value but post increment/decrement cannot be used as l-value.
++b = 20; // works b++=200 // not works 20.When a function is declared as const, it can be called on any type of object. Non-const functions can only be called by non-const objects.21. Friends declarations may appear anywhere in the class.They are not members of class granting friendship and so they are not affected by access control section of class in which they are declared.
22. We need to use Initializer list whenver we have const or reference data members. The normal constructor we use do assignments and we cannot assign to const objects or objects of refernece types.We can only intialize which is done by intializer list.
23. To prevent copies, class must explicitly declare its copy constructor as private.However, members and friends can still make copies. If we want to prevent this we do so by declaring copy constructor as private but not defining it.
24. ++i is faster than i++ because i++ has to make a copy of the object and ++i does not.
25.By default, the first enum has the value 0.
26. If you mark the constructor as private, if you extend that class,..then you wont be able to instantiate the derived class as well.
27. Constructors that are used to allocate memory on the
heap
for the pointers inside a class are known as Dynamic
constructors.
28. Cannot
be invoked explicitly like a normal method call on the object
A
a1;
a1.A();
// Cannot invoke a constructor like a normal method
References:
- http://www.geeksforgeeks.org/static-objects-destroyed/
- http://www.geeksforgeeks.org/possible-call-constructor-destructor-explicitly/
No comments:
Post a Comment