stack

ARE we able to make stack of variables having different data types???
if yes then how?
By creating a stack of their base class.
Are we able to make a stack of enum type for this???
What are you asking? Can you elaborate more?
No, I don't think inheritance works for enum types.
You can of course cast the enums to int, but that requires you to know which element has which type to cast them back correctly.
This doesn't seem to make much sense anyway.

You should explain what you're trying to do and we can recommend you a more adequate solution.
Last edited on
means are we able to use ENUM data type to make a stack of different data types...
As for the answer of first post (ARE we able to make stack of variables having different data types???) some one told me " make a stack of type enum" and i m confused about it
I'm confused about what you are asking. Can you rephrase?
Can you say what are you doing and post an example of what you need?
If that's really what that person said, disregard it. It's nonsense.
You could store pairs of an enum type that specifies the type of the second member of the pair (which would be of type void*). That's a bad idea though, so it's better to forget about it right away.

Make a base class that has an interface which is common to all subclasses and make a stack of that base class.
If you ever need to upcast, you can do it with dynamic_cast.
ARE we able to make stack of variables having different data types???

I believe the answer is no. Even if you use a class to hold a vector which holds different primitive types the pointer type and vector type will be that of the class name which is a programmer defined type.
How about a stack of Boost.Variant1 or writing a container adapter for Boost.Any2?

Still, let's no forget the [lack of] usefulness of having a heterogeneous container. There's only so much you can do when you don't know the type. If you're going to design an interface that applies to all of the differently-typed elements, you're right back to a container of base class pointers.

Other possibly interesting topics may include the Composite and Null Object patterns. Also, recall the article discussion on type erasure3 (very interesting stuff).


1 http://www.boost.org/doc/libs/1_43_0/doc/html/variant.html
2 http://www.boost.org/doc/libs/1_43_0/doc/html/any.html
3 http://www.cplusplus.com/forum/articles/18756/
Last edited on
abbi wrote:
ARE we able to make stack of variables having different data types???


You can do it with a union:

1
2
3
4
5
6
7
8
9
10
11
12
#include <stack>

union part
{
	int i;
	double d;
};

int main()
{
	std::stack<part> parts;
}


But it sounds like a bit of a crazy endeavour.

You could also store subclasses of a specific base class. You would need to store their pointers for this.

1
2
3
4
5
6
7
8
class Base
{
};

class D1: public Base {}
class D2: public Base {}

std::stack<Base*> bases; // push any subclass of Base and rely on polymorphism 
The union approach doesn't quite work because on retrieval, the programmer won't know, for each element,
what type the value is. You need a discriminator, which means a struct:

1
2
3
4
5
6
7
struct part {
    int which;   // 0 == int, 1 == double, etc
    union {
        int i;
        double d;
    } u;
};


But unions in C++ have limited use until C++0x allows the "unionized" types to have user-defined constructors.

+1 moorecm
Topic archived. No new replies allowed.