Discussion: Copying void *?
Afficher un message
Vieux 22/02/2008, 00h50   #6
Travis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Copying void *?

On Feb 21, 3:31 pm, Christopher <cp...@austin.rr.com> wrote:
> On Feb 21, 4:43 pm, Travis <travis.bow...@gmail.com> wrote:
>
> > So the constuctor is basically

>
> > myClass::myClass (const myClass &rhs)
> > {
> > string1 = rhs.string1;
> > string2 = rhs.string2;
> > voidstar = rhs.voidstar;

>
> > }

>
> > dangerous?

>
> > what I want is to have people be able to insert into the tree and
> > throw whatever type they want into the tree and i'll store it as a
> > void *.

>
> Very dangerous in my opinion:
> Who allocates what void * is pointing to?
> Who releases it?
> If every node may contain a different type, how is the user of the
> node going to know what type it contains, perform the proper cast and
> use it?
>
> I am still unclear if you want to make a tree that may contain nodes
> that contain any _one_ type
> i.e
> I can make a tree<int> that contains node<int> objects, which contain
> ints
> I can create another instance of the tree, tree<myclass>, that
> contains node<myclass> objects, which that contain myclass
> ...
> or
> ...
> If you are wanting to create a tree that may contain nodes that may
> contain any type
> i.e.
> I can make a tree, that contains one node, that contains an int
> The same instance of the tree may contain another node, which contains
> a myclass object
>
> The first can be easily done with templates and I can envision it
> easily.
> The second is harder, and I _think_ it can also be done with
> templates, however, I cannot easily envision its design.
>
> I think there is also a boost container type that supports elements of
> varying types, such that one element can contain an int while another
> element can contain a myclass, with both elements belonging to the
> same container.
>
> I think your use of the void * is a way for you to get around a design
> problem. But it if isn't a problem in your design, we still need to
> see the constructor that takes 3 arguments, allocation of the memory
> void * points to, where it is release, and where it is casted and
> used.


Yeah the second one. The tree is always going to have a node_type of
myClass (and the tree is templated). As part of that class, there is
one attribute that I would like to be any type therefore one of the
attributes is a void*
  Réponse avec citation
 
Page generated in 0,05281 seconds with 9 queries