Click here to Skip to main content
15,908,115 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi everyone,

Last interview, i got a question what is the advantage pointer to data member over the pointer member, normal member function and pointer to function.
Posted
Comments
Stefan_Lang 15-Jul-15 2:36am    
Makes no sense without context: it all depends on the intended use.

Or more to the point, who would be using the pointer to data member and pointer to function? If it's someone outside the class, that would break encapsulation and is generally a bad idea without good reason. If used inside the class, what is the point of using a pointer?

P.S.: is this really, exactly, what you were asked? I have my doubts you've reproduced the question correctly
Richard MacCutchan 15-Jul-15 3:53am    
There is no advantage, they are just used in different circumstances.
Member 11417637 15-Jul-15 5:36am    
ok richard,
pls explain any one situation
Richard MacCutchan 15-Jul-15 7:47am    
You would only need a function pointer when you do not know the function name at compile time, for example:
1. The funtion to call will depend on some inputs in the program.
2. You are calling the function from a DLL that you load at run time.
Member 11417637 15-Jul-15 8:29am    
Thanks Richard MacCutchan

A general note: in C++ you should avoid using raw pointers (of any kind) as a matter of principle, except for the purpose of writing frameworks specifically designed to help other parts of the system avoiding pointers. What you should use are smart pointers.

Given that, I assume the question is intended as a low level evaluation of your understanding, and thus, the explanations would be as follows:

Pointer to data member:
This breaks encapsulation and should never be used. However, you can use a pointer to const data member as a means to pass a data reference to another function in an efficient way.

Data member pointer:

You can use a pointer as a data member rather than the data itself for similar reasons as you would in a local function, e. g.:
- If you need to pass the data to other functions occasionally, and the data type is expensive to copy, using a pointer is preferable
- If the data could have a NULL value, but the data type doesn't define one, using nullptr as NULL value is an option
- if the class that owns the data can (and is expected to) be copied (a lot), but the data element is not usually modified, it may be more sensible for the instances of the class to share the data element by using a pointer (however, it would be even better to use a smart pointer!)

Member function pointer:

Just forget about that unless you are a highly advanced system/framework programmer. Classes and (virtual) member functions make function pointers of any kind pretty much obsolete, except in compiler programming (in which case you would be the one responsible for making the use of function pointers obsolete)
 
Share this answer
 
They are different beasts and serve different purposes. So I don't see any 'advantage' in using one or the other.
Are you sure you reported the interview question exactly?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900