templatized class inside templatized class

This is a discussion on templatized class inside templatized class within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by -EquinoX- I think the only way I can restraint it is for the template class vector to ...

  1. #46
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,883
    Quote Originally Posted by -EquinoX-
    I think the only way I can restraint it is for the template class vector to take an object type... type can be anything but then I can guarantee that type has all the overload operators
    If you really want to make sure that vector's element type can support all the operators, then the solution is to create a vector class and an element class. Note, classes, not class templates. Once you involve templates, it becomes possible to try to instantiate the template with a type that does not support the operations required.

    Quote Originally Posted by -EquinoX-
    well it's a requirement for both the vector and the type parameter... I am only confused on how can I make so that whatever type is the parameters then it would support those operators
    Assume that the type supports those operators and thus make use of them. The compiler will then enforce this requirement for you by spitting out an error if the user attempts to call a function that assumes that a particular operator is available when it is not.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #47
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by -EquinoX- View Post
    well it's a requirement for both the vector and the type parameter... I am only confused on how can I make so that whatever type is the parameters then it would support those operators
    this compiles
    Code:
    #include <valarray>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      std::valarray<std::string>thisMakesAtLeastSomeSense;
      thisMakesNoSense+="asdf";
    	return 0;
    }
    because string has a += operator

    this doesn't.
    Code:
    #include <valarray>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      std::valarray<std::string>thisMakesNoSense;
      thisMakesNoSense*="asdf";
    	return 0;
    }
    because string doesn't have a *= operator.

    what you still don't realize is that even if you create a template class that has the operator in question, you can't guarantee you can use it.

    you're transferring the inevitable compilation error from your vector class to your element class.

  3. #48
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by laserlight View Post
    If you really want to make sure that vector's element type can support all the operators, then the solution is to create a vector class and an element class. Note, classes, not class templates. Once you involve templates, it becomes possible to try to instantiate the template with a type that does not support the operations required.
    if he doesn't use templates, then he can't actually use the operators at all can he?
    Last edited by m37h0d; 11-18-2009 at 12:24 PM.

  4. #49
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by m37h0d View Post
    Code:
    I don't really think that the implementation must be inside the class
    d'oh of course. that is correct. you just can't put it in a separate file.
    Sure you can, although it wouldn't be normal. The definition just has to be available somehow, that's all.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #50
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay, I am now 100% sure after consulting here and there that I don't need to create an element class.. all I need is just one template vector class.. only two problem left..

    1. how can I make sure that the element (which will be stored inside the vector) supports all of
    this operators:

    =, ==, !=, +, +=, -, -=

    2. any idea why the element takes an int? I've been thinking all day and can't seem the reason

  6. #51
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by -EquinoX- View Post
    okay, I am now 100% sure after consulting here and there that I don't need to create an element class.. all I need is just one template vector class.. only two problem left..

    1. how can I make sure that the element (which will be stored inside the vector) supports all of
    this operators:

    =, ==, !=, +, +=, -, -=

    2. any idea why the element takes an int? I've been thinking all day and can't seem the reason
    i already showed you #1.

    re: #2, only thing that occurred to me was looping over the array of T's and initializing each one with its index in the array.

  7. #52
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    for #1, you only showed me that some types makes sense to use *= and some don't, but it doesn't really tell me that the element supports all of the operator, correct me if I am understanding you wrong

  8. #53
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    the compiler will tell you as soon as someone tries to use an operator that doesn't exist.

  9. #54
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,883
    Quote Originally Posted by m37h0d
    if he doesn't use templates, then he can't actually use the operators at all can he?
    Of course he can, since he explicitly provides them, i.e., creating one's own number class.

    Quote Originally Posted by m37h0d
    re: #2, only thing that occurred to me was looping over the array of T's and initializing each one with its index in the array.
    Yes, though strictly speaking that is assignment, not initialisation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #55
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    does all type takes an int as an argument in the constructor?

  11. #56
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,883
    Quote Originally Posted by -EquinoX-
    does all type takes an int as an argument in the constructor?
    All objects of built-in types can be initialised with an int or have an int assigned to them, but they do not actually have constructors. For class types, it depends on the class type. As I noted, some class types are deliberately designed as numeric types and thus have similiar behaviour in many respects, e.g., the std::complex class template.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #57
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by laserlight View Post
    Of course he can, since he explicitly provides them, i.e., creating one's own number class.
    aah ok, i see what you mean. no abstraction or genericity at all.

    Yes, though strictly speaking that is assignment, not initialisation.
    you're correct, i phrased myself poorly. i was thinking of the initialization on the right hand side...i.e.:
    Code:
    arr[i]=T(i);

    does all type takes an int as an argument in the constructor?
    not every class does, but if in your constructor, you assign each element in your array to an instance that has been constructed with one, then any type put into your vector must.

    e.g:
    Code:
    template<typename T,size_t N>foo
    {
      private:
        T  arr[N];
      public:
        foo()
        {
           for(size_t i=0;i<N;++i)
           {
              arr[i]=T(i);
           }
        }
    };
    Last edited by m37h0d; 11-18-2009 at 09:31 PM.

  13. #58
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so I came to a conclusion that there is no way to make sure that type has all the operators:

    =, ==, !=, +, +=, -, -=

    and every objects of built in type can take an int as a constructor

  14. #59
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    that's what i've been trying to tell you!

  15. #60
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay, now define what is an objects of built in type?

Page 4 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an object of a class inside another class - possible?
    By Programmer_P in forum C++ Programming
    Replies: 34
    Last Post: 08-15-2009, 11:21 PM
  2. Replies: 2
    Last Post: 11-28-2008, 04:15 PM
  3. Unresolved external on vector inside class constructor
    By Mario F. in forum C++ Programming
    Replies: 13
    Last Post: 06-20-2006, 12:44 PM
  4. Declaring an instance of a class inside a class
    By nickodonnell in forum C++ Programming
    Replies: 4
    Last Post: 10-01-2005, 11:46 PM
  5. how do i define a const inside a class under private
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2001, 05:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21