Jump to content
  • Advertisement
Sign in to follow this  

Is it ok to use casting in this case?

This topic is 2165 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. Edited by Waterlimon

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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. Edited by Waterlimon

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Like dawoodoz I would use template metaprogramming in this case.
Make a generic Octree container against which you can issue spatial queries.
Then you can do whatever you want with the returned data.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!