Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> C++ Template Coding Conundrum...
Jeigh
post Mar 21 2006, 03:02 PM
Post #1


Whitest Black Mage
Group Icon

Group: [MODERATOR]
Posts: 1,352
Joined: 20-May 05
From: NB, Canada
Member No.: 5,281



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.
Go to the top of the page
 
+Quote Post
Vyoma
post Mar 30 2006, 01:44 PM
Post #2


Cosmic Overlord
Group Icon

Group: Members
Posts: 550
Joined: 26-November 05
From: Chennai, India
Member No.: 9,811



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.
Go to the top of the page
 
+Quote Post
Jeigh
post Mar 30 2006, 01:53 PM
Post #3


Whitest Black Mage
Group Icon

Group: [MODERATOR]
Posts: 1,352
Joined: 20-May 05
From: NB, Canada
Member No.: 5,281



Forgot I had even posted this here haha ohmy.gif 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 biggrin.gif
Go to the top of the page
 
+Quote Post
Vyoma
post Apr 3 2006, 06:47 PM
Post #4


Cosmic Overlord
Group Icon

Group: Members
Posts: 550
Joined: 26-November 05
From: Chennai, India
Member No.: 9,811



QUOTE(Jeigh @ Mar 30 2006, 07:23 PM) *
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 biggrin.gif


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. blink.gif
Go to the top of the page
 
+Quote Post
Jeigh
post Apr 3 2006, 07:05 PM
Post #5


Whitest Black Mage
Group Icon

Group: [MODERATOR]
Posts: 1,352
Joined: 20-May 05
From: NB, Canada
Member No.: 5,281



It appears you'll need to wait for a bit sad.gif 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)
Go to the top of the page
 
+Quote Post
bluefish
post Dec 16 2006, 06:07 PM
Post #6


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 71
Joined: 16-December 06
Member No.: 18,419



I would have your template inherit from a base class, e.g.
CODE
class filetype_base { /*...*/ }
template <class type> class filetype : filetype_base { /*...*/ }


Then you could store them in a vector of pointers, e.g.:
CODE
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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. ColdFusion template(2)
  2. PHP template(13)
  3. MySQL & PHP coding(9)
  4. Invision board template??(5)
  5. Secure Php Coding(10)
  6. My New Template I Made(26)
  7. The Hacker's Google(29)
  8. How Difficult Is It To Edit A Bought Template ?(12)
  9. Need Help With Floating Layout!(11)
  10. Where Can I Seek Help On Flash Coding ?(5)
  11. Your Opinion On An Edited Joomla! Template(17)
  12. Youth-oriented Template(10)
  13. PHP Coding Help!(5)
  14. Travel Template(1)
  15. Template Review(12)
  1. Shop Template(4)
  2. Need Help With A Joomla Template(4)
  3. Scrutinize My Chess Game!(6)
  4. Coding Asp In Javascript(2)
  5. Coding Html Properly In The New Age(15)
  6. Can Anyone Help With Html/coding?(2)
  7. Need Help With Joomla Template(7)
  8. Game Coding(4)
  9. Simple Template System(0)
  10. Coding Layouts(4)
  11. Get The Current Running Machine Ip-c/c++ Coding(2)
  12. I Need A Template For A Website(9)
  13. Joomla Template Kit Extension For Nvu/composer(3)


 



- Lo-Fi Version Time is now: 14th October 2008 - 12:57 PM