In C++,
The size of an empty class is 1byte?** The size of a class with no member variables would be same as the size of a class having an one-byte member variable!! Hence, the following code
class Class1 { }; class Class2 { char ch; }; void main() { printf("\n Sizeof(Class1) = %d, Sizeof(Class2) = %d", sizeof(Class1), sizeof(Class2)); }
will produce the output "Sizeof(Class1) = 1, Sizeof(Class2) = 1". And did you know that the size of a class without any member variables but with member virtual functions is 4 bytes? The following code
class Class { virtual void Func()=0; }; void main() { printf("\n Sizeof(Class) = %d", sizeof(Class)); }produces the output "Sizeof(Class) = 4". (**Note: Size may vary based on the Word-Length of the Machine)
When using the run-time polymorphism you have to make the destructor of base class virtual? Without virtual destructor for base class, destructor of derived class cannot be invoked through base class pointer. For example, the code
class Base { public: Base() { printf("\nConstructor of Base"); } ~Base() { printf("\nDestructor of Base"); } }; class Derived: public Base { public: Derived() { printf("\nConstructor of Derived"); } ~Derived() { printf("\nDestructor of Derived"); } }; void main() { Base* pBase = new Derived(); delete pBase; }
will not invoke the destructor of the derived class (even though the object being deleted is a derived class object)---because the destructor of base class was not defined virtual .
Virtual functions of derived class cannot be invoked in constructor/destructor of base class? For example, the code
class Base { public: Base() { OverloadedFunction(); } // Invoke the virtual function virtual void OverloadedFunction() { printf("\nBase Function"); } }; class Derived: public Base { public: Derived(){} virtual void OverloadedFunction() { printf("\nDerived Function"); } }; void main() { Base* pBase = new Derived(); delete pBase; }will produce output �Base Function� instead of �Derived Function � even though constructor is invoked through derived object.
The difference between Reference and Pointer? References cannot be used without being initialized. To define a Reference you should have at least one object of the referred type declared already. Thus References cannot point to non-existing objects. It is possible for a pointer to have a NULL value---but a Reference can never have a NULL value.
Further, a Reference cannot be reassigned to refer to a different object, while a pointer can be. That is, a pointer can be made to point to a different object by reassigning its value. But once a Reference is assigned to an object, it continues to refer to the same object and cannot be modified to refer to a different object. (Any assignment to the Reference variable only modifies the "value of the reffered object" and would not change the actual reference relationship.)
Also, the arithmetic of pointers is different from that of References. For example, an operator such as ++ on a pointer makes it point to next object (in an array of objects, if valid), where as ++ on a Reference invokes the operator++() on the refered object.
The difference between new and malloc? Creating an object with new invokes the constructor of the object. However, creating the object with malloc will not invoke any constructors. Same goes with free and delete. The function free will not invoke any destructors, while delete properly calls the destructors before actually freeing the memory.
Further, new is more typesafe than malloc. malloc always returns a void* while new returns a pointer to the object of the requested type. Also, new operator can be overloaded by a class while malloc cannot be.
Member opeartor new and its peers (new[], delete, delete[] etc...) are static member functions? They do not have access to this pointer and other non-static members.
When overloading the new operator, you should also consider overloading its peer opeartors new[], delete, delete[] etc.? For example, the following code
class TestClass { private: void* operator new(size_t nSize); public: TestClass() { } }; int main() { TestClass* pPtr = new TestClass; // Error: Cannot Access Private Member Function TestClass* pCl = new TestClass[10]; // Perfectly Okay !! }
prohibits the creation of a single TestClass object with new, but allows the creation of array of TestClass objects with new[] .
The order of initialization for a derived class is as follows?
The following functions would be generated automatically by the compiler if you do not supply them for a class?
Friendship is not inherited? Friend of a base class will not be a friend for the derived class.
union cannot have the following as their members?
How to print numbers from 1 to any given n without using any loop constructs (such as for, while, goto etc.)?
void PrintRecursively(int nNumber, int nMax) { if(nNumber <= nMax) { printf("%d ", nNumber); PrintRecursively(nNumber+1, nMax); } }
Operator Name
. Member selection
.* Pointer-to-member selection
:: Scope resolution
?: Conditional
# Preprocessor convert to string
## Preprocessor concatenate
static member functions cannot be declared as virtual? For example, the following generates a compiler error.
class A { public: static virtual void StaticA() // Error: Virtual member functions cannot be static { printf("\nCalled A\n"); } };
And did you also know that modifiers such as const and volatile are not allowed on static member functions? For example, the following code generates compiler error.
class A { public: static void Func() const // Error: modifiers are not allowed on static member functions { printf("\n Func1 Called"); } };
mutable keyword can be used to assign values to non-static, non-constant data members from a const member function? For example, the following code compiles without errors.
class A { mutable int m_accessCount; public: int GetAccessCount() const { return m_accessCount++; // Valid because m_accessCount is declared mutable } };
When using functions such as strncpy() that take a size input parameter, you should use the size of the destination buffer and not the source buffer for correct functionality? For example, the following is incorrect:
strncpy(dest, src, sizeof(src)); //If sizeof(src) > sizeof(dest) this would give unexpected results;
The correct and safe usage should be,
memset(dest, 0, sizeof(dest)); //Fill the buffer with null characters strncpy(dest, src, sizeof(dest)-1); //sizeof(dest)-1 takes care of the space for the terminating null character
In MFC,
DDX_CBString uses a prefix string match and not exact string match? That is, when DDX_CBString is called to transfer data to a combo box control, the first item in the control that has the given string as a prefix gets selected. Instead, if you want to match the entire string and not just prefix, then you should use DDX_CBStringExact. Same is the case with DDX_LBString and DDX_LBStringExact for list box.
In Win32,
For a Tree control, TVN_GETDISPINFO is sent to request the item text only if LPSTR_TEXTCALLBACK value is used for the TVITEM.pszText while creating the item? Same is the case with I_IMAGECALLBACK for TVITEM.iImage and I_CHILDRENCALLBACK for TVITEM.cChildren. The notification would not be sent if actual values were used instead of these callback options.
We can prohibit a window resizing by removing the WS_THICKFRAME style? When a window is created without the WS_THICKFRAME style, it would have no border for ther user to grab and resize! Similary, we can prohibit the user from moving the window by removing the WS_CAPTION style. When a window is created without the WS_CAPTION style, it would have no title bar and hence cannot be moved !
The other ways of restricting a window from being resized and moved is to handle the WM_SIZING and WM_MOVING messages. For both of these messages the LPARAM contains a pointer to RECT containing the window screen coordinates - just change these values and you are done.
Another elegant way to make a window non-sizable, non-movable is to handle the WM_NCHITTEST. Return HTBORDER whenever the default procedure returns any of HTLEFT, HTRIGHT,..., HTSIZE, HTCAPTION;
In WTL,
// Store the view top and bottom items before deleting the items int nSelectedIndex = m_wndListView.GetSelectedIndex(); int nTopIndex = m_wndListView.GetTopIndex(); int nBottomIndex = nTopIndex + m_wndListView.GetCountPerPage() -1; ... // Delete the old items and add new items ListView_DeleteAllItems(m_wndListView.m_hWnd); Add_Items(...); ... // Restore the selected item visibility m_wndListView.EnsureVisible(nBottomIndex, true); m_wndListView.EnsureVisible(nTopIndex, false); m_wndListView.SelectItem(nSelectedIndex);
By
P.Gopalakrishna