Akamai Interview Question
Applications DevelopersCountry: India
Approach 2 is not safe to use. Even how do you define the pop()? You can't simply return void*.
Approach 3 will not work for primitive type.
Can you please how you define pop() in approach 1?
Approach 3 will not work for primitive types, thus you can use method 1 or 2. #1 is the preferred method in my opinion.
To pull a value out of a void*, you are required to know what is going to come out as it is being used, thus the danger or utilizing it.
To pop from method 1, you would use value = boost::any_cast<T>(list) to return a value from the desired container.
There are three ways to address this that I know of:
- masterjaso July 04, 20131. boost::any (and the subsequent API that goes with it)
2. void* (that can point to any type of object)
3. Create a generic base class that all of your different types inherit from, then you can create lists and arrays of the parent type for any of the children.
Be mindful with void* as it is not as type-safe as using boost::any.
The drawback to the parent class is that it can overlap and violate the paradigm of modular programming if the items are from items of vastly different types and hierarchies.
You will need to search for the boost api to get all of the details.