Ecere SDK/eC Forums http://ecere.com/forums/ Print view |
|
Can I define my own template functions? http://ecere.com/forums/viewtopic.php?f=1&t=249 |
Page 1 of 2 |
Author: | samsam598 [ Wed Aug 15, 2012 1:04 am ] |
Post subject: | Can I define my own template functions? |
Greetings! As subjected,if yes,any special concerns should I pay attention to? Thanks and best regards, Sam |
Author: | jerome [ Wed Aug 15, 2012 1:47 am ] |
Post subject: | Re: Can I define my own template functions? |
Hi Sam, At the moment, eC only have support for class generics. It is (to some extent) still an experimental feature, and mainly meant to power the type-aware containers (e.g. Array, List, AVLTree, Map...) and Iterator, which provide equivalent functionality to many of the C++ STL classes. Please read the Templates section in this original blog post for the new feature: http://www.ecere.com/blog/2008/09/03/ec ... hlighting/ There are 3 types of parameterization possible for classes, being: - A generic type - A generic identifier, meant to be a member of a class - A generic value You can find examples for the first one (generic type) in all of the ecere source tree's containers (sdk/ecere/src/com/containers). Be aware that it is still experimental and may suffer from some limitations. The latter 2 are used for the LinkList class (also in that folder). The LinkList class is a generic link list linking nodes which contain within themselves both a previous and next link member. It uses the generic identifier to define an arbitrary member of the class to serve as the previous and next links. The generic value is used to specialize the link list as either circular or not. These features are only used in the LinkList, and are thus particularly experimental/limited. As a quick overview, here's some things you can do with a generic class: Code: Select all
Best regards, Jerome |
Author: | samsam598 [ Wed Sep 05, 2012 12:26 am ] |
Post subject: | Re: Can I define my own template functions? |
As an exercise,I've tried to implement a generic data structure Stack.First I wrote a plain version for int,given below code: Code: Select all
the private field top should not be accessed outside,am I right?But in the Main() calling vec.top is fine,why? Now I am trying to implement the generic version as below: Code: Select all
Regards, Sam |
Author: | jerome [ Wed Sep 05, 2012 4:18 am ] |
Post subject: | Re: Can I define my own template functions? |
Hi Sam, As I mentioned earlier, these templates are still somewhat shaky grounds. I've found that if you modify your pop function to return like this: Code: Select all
Also be warned that T arr[maxStack] gets compiled to a fixed-sized (as if T took 8 bytes). On another note, all the predefined container classes inherit from Container, so you could make your Stack class inherit from it as well to test those features. Or it could inherit from Array Regarding top being accessible from App::Main(), please read the chapter on Encapsulation and access control in eC, starting on page 85 in the Tao. It differs significantly from the C++ ideas: - static means accessible only within the current .ec source file - private means accessible only within this shared library (.dll/.so) - public means accessible by all In no case will a member of any class defined within a .ec file not be accessible within the same .ec file -- I would find this a terrible annoyance. The idea is that classes should be organized in source files according to functionality, e.g. the implementation for a major class and other related classes could go together in one source file, thus making member access between them easy (In C++ terms, you could think of it as all classes within a source files being friends). Things that should not be accessible by other unrelated classes can be made static (though there are currently limitations to the usage of static), and things that should only be invisible to the outside world would be made private (and only be seen within the shared library module source files). Regards, Jerome |
Author: | samsam598 [ Wed Sep 05, 2012 8:01 pm ] |
Post subject: | Re: Can I define my own template functions? |
Hi Jerome, Thanks a lot.For the private scope,noted. For the generic Stack function,double and int are fine after tested.But not for char* (String): Code: Select all
This is the 9 line This is the 9 line This is the 9 line ... Don't know whether I missed something very obvious. Regards, Sam |
Author: | jerome [ Wed Sep 05, 2012 8:44 pm ] |
Post subject: | Re: Can I define my own template functions? |
Hi Sam, You missed 2 things here First, the 'String' class at the moment works pretty much like a typedef to 'char *' (so It's essentially a C string) This means that you need to think of it as such, and so here you're doing a sprintf into an uninitialized pointer. Additionally, when you push a String onto your class, no special code generates a copy for each added string, so you're essentially pushing the same string (And modifying it will modify all other strings in the stack, since they're the same -- that's why you get the same line printed 10 times). The following will work: Code: Select all
Regards, Jerome |
Author: | samsam598 [ Wed Sep 05, 2012 9:11 pm ] |
Post subject: | Re: Can I define my own template functions? |
Thanks a lot.Got the point(maybe not yet ^_^) Code: Select all
Regards, Sam |
Author: | jerome [ Wed Sep 05, 2012 9:39 pm ] |
Post subject: | Re: Can I define my own template functions? |
Hi Sam, You still need to have one allocated string per thing added to the stack... This would work: Code: Select all
|
Author: | jerome [ Wed Sep 05, 2012 9:42 pm ] |
Post subject: | Re: Can I define my own template functions? |
You have to realize the life of a char array is limited to its scope (the compound { } brackets around it), so as soon as you're outside of the for loop iteration, it's gone. |
Author: | samsam598 [ Thu Sep 06, 2012 12:34 am ] |
Post subject: | Re: Can I define my own template functions? |
Appreciated. Btw,does CopyString do the same as strdup? The more I study deep into eC,the more I love.Keeping its features within a deeply thoughtful range other than bloated as c++ makes eC more beautiful and powerful. |
All times are UTC-05:00 | Page 1 of 2 |
Powered by phpBB® Forum Software © phpBB Limited |