Thread: template constructor

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    template constructor

    heya,

    im a little confused about an issue with templates..

    i understand templates can be used to pass multiple objects (e.g related by inheritance) and one may call template<class T>'s methods etc..

    however, if we have templates with member functions, constructors and member variables, where do these come into play?

    i.e

    Code:
    template<class T>
    int
    whatever<T>::size(void) {
            return fileSize;
    }
    is this defining the actual size() variable for all objects passed into the template?

    similarly, if we have something like

    Code:
    whatever<T>::whatever(string)
    when will the template's actual constructor be called?

    tia

    -twans

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    is this defining the actual size() variable for all objects passed into the template?

    that should read size() method..

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    actually, they are not existing before its instantiation.

    if you write following code
    whatever<int> x; //instance the class template

    now, you get an object of template class whatever<int>, and now, its constructor is called.


    whatever<T>::whatever(string); this code is not to call the contructor, it is illegal and syntax error
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    so can a template have an actual constructor?

    what if we have the following scenario:

    string s;
    whatever<s> p;

    does this create an instance of the template called p of type s?

    im finding the concept a little hard to grasp.

    thanks for your reply

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    whatever<T>::whatever(string) {//implementation} actually works and compiles fine.

    so if one calles

    Code:
    whatever<int> s("hello");
    do we get an object of type whatever<int> ? and does it has the properties of both a normal class?.

    so in the example above, if whatever<class T> had instance varialbes x and y (both public) can we call s.x=0 and s.y=10 and its modifying the actual instance of the 'whatever' class?

    thanks again.

    -twan

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Code:
    string s;
    whatever<s> p;
    it's incorrect,it should be
    Code:
    whatever<string> p;
    in < >, it just can be a type parameter,not an variable
    so
    whatever<T> and whatever<U> have differenet constructor and all the things of their are different though they look like sibling
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    ahh, i think i understand..

    the actual whatever<int> is a differnt object to int itself, correct? so my whatever<string> s; might create an object with methods size(), operator<< etc which are defined in my whatever<class T> template, which differ from those in string itself?

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    do we get an object of type whatever<int> ? and does it has the properties of both a normal class?.

    yes,but i don't know what you meant it has the properties of both a normal class.


    whatever<int> x;

    you can see it as
    whatever_int x;

    whatever_int is a new class, compiler replaced the template parameter with int, in this way, it's a normal class actually
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    great!

    thanks for your help

    i was confusing template classes for simply adding additional , generic methods, but in fact they are -as you informed me- new classes in themselves.

    i appreciate the help.

    -twans

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    you r welcome, heihei~~~
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  11. #11
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    in < >, it just can be a type parameter,not an variable
    You're right in saying that cannot be a variable, but incorrect in saying it can only be a type. the parameters inside the < >'s can also be some sort of integral constant.
    Code:
    template <typename type, int size>
    class FancyArray
    {
        public:
         type  array[size];
    };

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    It doesn't have to be an integral constant.

    So far I've seen as a template parameter:
    A particular class, function pointer, some value, a pointer to an array of class objects

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    127

    yeah, and can also be address of a 'static' object which has external linkage

    Code:
    template<char *str>
    class X{};
    
    namespace
    {
        char buffer[]="hello"; 
    }
    
    
    X<buffer> x;
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    you realize that you just contradicted yourself?
    object which has external linkage
    Code:
    namespace
    {
        char buffer[]="hello"; 
    }
    The linkage of an unnamed namespace is.... thats right internal

  15. #15
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    The linkage of an unnamed namespace is.... thats right internal
    some books say it is internal, in fact, it is external, see TC++PL
    and you can take a test.

    the code i mentioned will be compiled, in other words, everything in the unnamed namespace has external linkage. on the another hand, everything in unnamed namespace is just visible in current translation unit.
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM