Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to use overloaded operators with template types?

How to use overloaded operators with template types?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Arris74
    wrote on last edited by
    #1

    Hello, I use a template class where I would like to overload the operator [ ]. My intention is that not only the instances of the class will call the operator but also the types of the instances. I have tried the following code. It works but never the operator [] is called. Please could tell me if is there a solution to use my own overloaded operator with any type like int, float, long etc.. Thanks for your kind answers. //ClassTest.h template class ClassTest { public: ClassTest(){}; virtual ~ClassTest(){}; type* Date; inline type& operator[](UINT i_index){return (at(i_index));}; }; main() { // test 1 ClassTest theInt; theInt.Date = new int[10]; theInt.Date[0] =5; //Does not call type& operator[] //test2 int array[] = {1996,1997,1998} ClassTest figure; figure.Date = array; int xx = figure.Date[1]; //Does not call type& operator[] }

    M 1 Reply Last reply
    0
    • A Arris74

      Hello, I use a template class where I would like to overload the operator [ ]. My intention is that not only the instances of the class will call the operator but also the types of the instances. I have tried the following code. It works but never the operator [] is called. Please could tell me if is there a solution to use my own overloaded operator with any type like int, float, long etc.. Thanks for your kind answers. //ClassTest.h template class ClassTest { public: ClassTest(){}; virtual ~ClassTest(){}; type* Date; inline type& operator[](UINT i_index){return (at(i_index));}; }; main() { // test 1 ClassTest theInt; theInt.Date = new int[10]; theInt.Date[0] =5; //Does not call type& operator[] //test2 int array[] = {1996,1997,1998} ClassTest figure; figure.Date = array; int xx = figure.Date[1]; //Does not call type& operator[] }

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      You haven't called the [] operator for the template class in either of your examples. Instead of theInt.Date[0] =5; // This is using default [] operator to index into the "Date" member. it should be theInt[0] = 5; This will call your operator but it won't compile since you have no "at()" method defined. Mark

      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups