Jump to content
xisto Community
Sign in to follow this  
Jeigh1405241495

C++ Template Coding Conundrum...

Recommended Posts

I have no clue if this is even possible, but it seems like it should be so I figured I'd check if any of you guys knew a way to do it. I'm somewhat new to c++ so I'm kinda learning templates as I go for this job I'm at currently and my supervisor wants me to look into if this can be done this way.Alright, so basically currently I'm using templates to handle file handling for a bunch of custom file types we have in a piece of software (basically just different data layouts that need to be handled accordingly). Now currently I have it set up so the template is given a filetype class which allows the template to call the appropriate classes handling methods.That all works perfectly fine, the problem arises when I want to have a unified container for all these templates. Even something like an array would be fine I just want to be able to access all the files in a single container rather then having to pass pointers all over the program to these various files. Normal containers don't seem to be viable, however, since you need to predefine the class the template would use when declaring the array (Ex. a container of file<type1> will only be a container of type1 , not type2, type3, etc. I would need to be able to store like file<> and have it able store all types).It feels like there should hypothetically be a fairly simple way to do this, I don't know if I'm just unaware of a certain way of doing it since I don't know alot of the less common things about c++/templates or if I'm just missing something really obvious heh. Well, any help you can provide would be greatly appreciated, thanks.

Share this post


Link to post
Share on other sites

Wow Jeigh! Now, do not tell me you are new to C++ anymore. You seem to be quite well versed that I am with respect to templates.Have you considered using some class in Standard Component Library like a queue or stack? I am exactly not sure of how to do this, or what the exact component would be required. I am not even sure if this will be of help. Or you may need to use some of the Standard C++ Library classes if you are using Standards that are later to 2000. I am not sure if this was any help, but felt like noting it.

Share this post


Link to post
Share on other sites

Forgot I had even posted this here haha :lol: But since I posted that, I tried looking into whether or not there was a way to use the STl's containers for this, and it appeared not. So I looked into generic container options and found out about Boost.Boost.org is awesome. It's basically this group (some members of which are from the standards group that contorl the STL apparently) that have "almost" production ready STL libraries, but aren't quite there yet for whatever reason. So I installed these as boost has a "Boost::any" container that lets you generically store any datatype and recast as needed.Now, this works great for a vector where one element is an int, another a string, another a float, another a char, another a class made by me... but some huge problems show up whenever I try use a class I made that has a filestream in it, OR if I try to store a template initiated class in it. Lalas it spits out such a huge error report it just confuses the hell out of me and I can't even begin to figure out if its solveable. Any help on the web I found implied it can't handle these types of things. Ah well.So now me and my supervisor have basically decided to figure out a different approach to the problem, he claims to have some "idea" haha, just not sure how he'll want to implement it yet. Going to meet about it later probably actually, if we come up with a useable workaround for this problem I'll post it up just for curiosities sake.Thanks for the suggestion though :lol:

Share this post


Link to post
Share on other sites

So now me and my supervisor have basically decided to figure out a different approach to the problem, he claims to have some "idea" haha, just not sure how he'll want to implement it yet. Going to meet about it later probably actually, if we come up with a useable workaround for this problem I'll post it up just for curiosities sake.
Thanks for the suggestion though :lol:


If you do find something out, please do post it. I, for least, would be very much interested in how you would tackle this problem. Heck! I have already lost a couple of days of sleep trying to figure it out 'logically', but ended up getting my brain fried up or something like that. :lol:

Share this post


Link to post
Share on other sites

It appears you'll need to wait for a bit :lol: My supervisors supervisor is returning from a long leave tomorrow or the next day and everything is getting shuffled up. Basically we aren't sure when exactly we'll get to continue work on this aprticular project, but we're hoping we'll just continue with it immediately so if so I'll let you know what we come up with for sure.I KNOW its possible using some pointer trick, I just suck with pointers and don't have a clue how you would go about pulling it off. I tried a few little things that would logically work but the implementation was beyond my abilities or simply not within the actual functionality of pointers (thats my nice way of saying I thought up a broken idea haha)

Share this post


Link to post
Share on other sites

I would have your template inherit from a base class, e.g.

class filetype_base { /*...*/ }template <class type> class filetype : filetype_base { /*...*/ }

Then you could store them in a vector of pointers, e.g.:
class mytype;std::vector<filetype_base*> fv;fv.push_back(new filetype<mytype>("hello.xxx"));std::cout<<fv[0]->name;

Clearly you would need to tinker with the inherited and base classes, and experiment with virtual functions. This is the simplest way I would see of doing it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.