Is it ok to use casting in this case?

Started by
16 comments, last by rip-off 11 years, 10 months ago
I have the following classes:

class OctreeBase
{
//Each derieving class has a function like this. The octreebase doesnt have this function!
virtual DoStuff(void* object)
{
Node* node=static_cast<Node*>(object)
derievedOctree.typeSpecificOperation(node.typeSpecificFunction(),derievedOctree.privateTypeSpecificFunction())
}
}

class Node //Each class derieved from octreebase has its own class to represent a node containing whatever members the octree needs
{

}

class OctreeIterator //only 1 of these, not templated or anything
{
OctreeBase* tree
void* node
void DoStuff()
{
tree->DoStuff(node)
}
}



OctreeIterator uses the same tree for its whole life cycle.

The user gets the octreeiterator by calling OctreeBase*->Root(), which sets the tree member.


However i dont like having void pointers and casting, is there any way i could do this in a prettier way? Currently i have the octreeiterator as a template but i need to have just a single type to use it the way i want.

o3o

Advertisement
I would try to make an abstract class that define the DoStuff method's interface. Each class inheriting from it will define the method with different content. The tree will have pointers defined as if they point to the abstract class but you assign objects of classes that inherited from the abstract class.
I dont understand.

I have a tree class, derieving from OctreeBase, and each tree class has a node class which carries data specific to the derieved octree class.

The OctreeIterator needs to be able to use any class derieved from OctreeBase, and hold a temporal reference/pointer to the current node its at (as the octree can find the child node faster if it gets the parent node)

I cant make the nodes inherit from the same base class, because:
1.It adds vtable i think, which takes memory
2.The operation needs derieved class specific data and functions, so i would need to pass the derieved octree class to the node through a virtual method, which gives me the same problem (would need to pass a void pointer and cast in the function)

o3o

If you need runtime polymorphism, use the vtable.
[size="1"]
If you mean making the node derieve from a base class, that just adds overhead and doesnt remove the problem. I still would need to pass a void*, but this time from the octree to the node.

Im wondering if i really should just use a void*, as i cant really see any other options.


The only option i see is to make the iterator polymorphic, so i would return a IteratorBase* instead of Iterator from Octree.Root(), but that forces me to use new to allocate it, so i dont want to do it when i already have a solution which doesnt require me to allocate stuff.

o3o

This template class might work for you but I haven't tried it.
http://nomis80.org/code/octree.html
No i dont have a problem with making the octree work for different types, i have a problem with making the iterator work with different octrees without templates, unnecessary waste of space and preferrably without void*, though that might be the real solution.

Technically there shouldnt be a problem if i derieved different iterators from the same base, as theyre all going to be the same size. But c++ is designed to assume that theyre not always the same size and thus i would need to return a pointer to the iterator and not the value if i used polymorphism with them (which requires new).

o3o

You can use polymorphism through references too, fwiw.
[size="1"]
If you mean that i should use polymorphism with the iterator, how can i make copies of the iterator properly, or return such an iterator, without using any allocation when the client will only use IteratorBase*/& to contain the iterators.

o3o

This topic is closed to new replies.

Advertisement