+91 90691 39140 | +1 253 214 3115 | info@hub4tech.com | hub4tech

C++ Pointers Functions Interview Questions and Answers

1
Explain the use of this pointer.

The this keyword is used to represent an object that invokes the member function. It points to the object for which this function was called. It is automatically passed to a member function when it is called.
e.g. when you call A.func(), this will be set to the address of A.

2
What is const pointer?

const pointer is a pointer which you don’t want to be pointed to a different value. That is, the location stored in the pointer can not change. We can not change where the pointer points. It is declared as:

type * const name
type is data type
name is name of the pointer
eg: char * const p

Since the location to which a const pointer points to can not be changed, the following code:

char ch1 = ‘A’;
char ch2 = ‘B’;
char * const p = &ch1;
p = &ch2;
will throw an error since address stored in p can not be changed.

3
Explain what happens when a pointer is deleted twice

A pointer if not nullified and deleted twice, leads to a trap. If set to null, it wont have much affect if deleted twice.

4
What is const reference?

const references allow you to specify that the data referred to won't be changed. A const reference is actually a reference to const. A reference is inherently const, so when we say const reference, it is not a reference that can not be changed, rather it’s a reference to const. Once a reference is bound to refer to an object, it can not be bound to refer to another object. For example:

int &ri = i;
binds ri to refer to i. Then assignment such as:
ri = j;
doesn’t bind ri to j. It assigns the value in j to the object referenced by ri, ie i;

This means, if we pass arguments to a function by const references; the function can not change the value stored in those references. This allows us to use const references as a simple and immediate way of improving performance for any function that currently takes objects by value without having to worry that your function might modify the data. The compiler will throw an error if the function tries to modify the value of a const reference.

5
What is a smart pointer?

Smart pointers are objects which store pointers to dynamically allocated (heap) objects. They are like built-in C++ pointers. However, they automatically delete the object pointed to at the appropriate time. They are useful as they ensure proper destruction of dynamically allocated objects (Exceptions). They can also be used to keep track of dynamically allocated objects shared by multiple owners.

They appear as owning the object pointed to and are responsible for deletion of the object when it is no longer needed.

The smart pointer library provides five smart pointer class templates:

scoped_ptr : Simple sole ownership of single objects. Noncopyable.
scoped_array: Simple sole ownership of arrays. Noncopyable.
shared_ptr: Object ownership shared among multiple pointers
shared_array: Array ownership shared among multiple pointers.
weak_ptr: Non-owning observers of an object owned by shared_ptr.
intrusive_ptr: Shared ownership of objects with an embedded reference count.

These templates are designed to complement the std::auto_ptr template.

Copyright ©2015 Hub4Tech.com, All Rights Reserved. Hub4Tech™ is registered trademark of Hub4tech Portal Services Pvt. Ltd.
All trademarks and logos appearing on this website are the property of their respective owners.
FOLLOW US