problem of creating objects from another classes

Pages: 123
hmmm... see your all classes are independent classes.. and so we cant do anything..key is of type long and it is storing only the address..

what you can do is you can take a variable in your class to know which type of object it is storing..
otherwise an experimental thing which you can do is this:
1. make all your classes virtual.
2. you are storing your address in key, instead store the pointer in void* type.
3. when you want to know which type of object it is, dynamic_cast it and thats it. :)

lets take an example:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class internal_container
{
//some code

public:
virtual int funct();

//some more code
};

class leaf_container
{
//some code

public:
virtual int funct();

//some more code
};

void main()
{
//some code;

internal_container *ic;

void *ptr = ic; //here we are storing ic in ptr

//some code

//here we want to know what type of object is stored in ptr
if(dynamic_cast<internal_container*>(ptr) != NULL)
{
//it is of type internal_container
}
elseif(dynamic_cast<leaf_container*>(ptr) != NULL)
{
//it is of type leaf_container
}
else
{
//object doesnt match any class
}

}//main ends  




hope you are getting what i am trying to explain..
i think this will be successful, try it.
Topic archived. No new replies allowed.
Pages: 123