RSS Feed for C/C++ Interview QuestionsCategory: C/C++ Interview Questions

How can I handle a constructor that fails? »

Throw an exception. Constructors don’t have a return type, so it’s not possible to use
return codes. The best way to signal constructor failure is therefore to throw an exception.

What is the difference between const char *myPointer and char *const myPointer? »

Const char *myPointer is a non constant pointer to constant data; while char *const
myPointer is a constant pointer to non constant data.

How are prefix and postfix versions of operator++() differentiated? »

The postfix version of operator++() has a dummy parameter of type int. The prefix version
does not have dummy parameter.

What is the difference between a pointer and a reference? »

A reference must always refer to some object and, therefore, must always be initialized;
pointers do not have such restrictions. A pointer can be reassigned to point to different
objects while a reference always refers to an object with which it was initialized.

What is name mangling in C++? »

The process of encoding the parameter types with the function/method name into a unique name is called name mangling. The inverse process is called demangling.
For example Foo::bar(int, long) const is mangled as `bar__C3Fooil’.
For a constructor, the method name is left out.
That is Foo::Foo(int, long) const is mangled as `__C3Fooil’.

How virtual functions are implemented C++? »

Virtual functions are implemented using a table of function pointers, called the vtable.
There is one entry in the table per virtual function in the class. This table is created by
the constructor of the class. When a derived class is constructed, its base class is
constructed first which creates the vtable. If the derived class overrides any [...]

What happens when you make call “delete this;” ?? »

The code has two built-in pitfalls. First, if it executes in a member function for an
extern, static, or automatic object, the program will probably crash as soon as the delete
statement executes. There is no portable way for an object to tell that it was instantiated
on the heap, so the class cannot assert that its object [...]

What is “this” pointer? »

The this pointer is a pointer accessible only within the member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer. When a nonstatic member function is called for an object, the address of the object [...]