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. template method in c++

template method in c++

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
4 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.
  • M Offline
    M Offline
    mehmetned
    wrote on last edited by
    #1

    There are three list which include objects of my classes.(car,bus,lorry) All the objects have an unique id number. I want to find the object which has a id that has given as parameter. (as using template method of c++) For example my classes are; class car{ string colour1; int id; int weight1; } class bus{ string colour2; int id; int weight2; } class lorry{ string colour3; int id; int weight3; } Is it necessary to write same names of the variables (id) to apply template method to my application? Thanks.

    S 1 Reply Last reply
    0
    • M mehmetned

      There are three list which include objects of my classes.(car,bus,lorry) All the objects have an unique id number. I want to find the object which has a id that has given as parameter. (as using template method of c++) For example my classes are; class car{ string colour1; int id; int weight1; } class bus{ string colour2; int id; int weight2; } class lorry{ string colour3; int id; int weight3; } Is it necessary to write same names of the variables (id) to apply template method to my application? Thanks.

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      I assume you mean something like this:

      template <typename T>
      inline int GetID(const T &obj)
      {
      return obj.id;
      }

      There are a number of methods you could use to handle differently named variables. Given the general case above you could introduce an exception using template specialisation as follows for example:

      template <>
      inline int GetID<rocket>(const rocket &obj)
      {
      return obj.rocket_id;
      }

      Steve

      M 1 Reply Last reply
      0
      • S Stephen Hewitt

        I assume you mean something like this:

        template <typename T>
        inline int GetID(const T &obj)
        {
        return obj.id;
        }

        There are a number of methods you could use to handle differently named variables. Given the general case above you could introduce an exception using template specialisation as follows for example:

        template <>
        inline int GetID<rocket>(const rocket &obj)
        {
        return obj.rocket_id;
        }

        Steve

        M Offline
        M Offline
        mehmetned
        wrote on last edited by
        #3

        carList for car busList for bus lorryList for lorry I have a method; int find(type list, int id) //type can be three posibilities (carList,busList,lorryList) { for(i){ if(list[i].id == id){ return id; } } } Is this method right for my porpose as logical?

        S 1 Reply Last reply
        0
        • M mehmetned

          carList for car busList for bus lorryList for lorry I have a method; int find(type list, int id) //type can be three posibilities (carList,busList,lorryList) { for(i){ if(list[i].id == id){ return id; } } } Is this method right for my porpose as logical?

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          I can't tell. Is it a template? It doesn't look like one. What is type?

          Steve

          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