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. Templates and protected members [modified]

Templates and protected members [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
wpf
7 Posts 4 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.
  • J Offline
    J Offline
    JKallen
    wrote on last edited by
    #1

    I have been programming for years and have managed to not have to get to involved with implementing my own templates classes, so the following probably has a simple solution. If I have declared and defined the following template class.... template class MyClass{ public: //.....................public members and functions protected: int m_int; T m_data }; MyClass my_ints; MyClass my_chars; The my_ints instantiation cannot access the my_char.m_int protected member variable even though that member variable is of the same type regardless of the underlying type the template encapsulates. This should have a simple solution. Thanks. JK

    J M 2 Replies Last reply
    0
    • J JKallen

      I have been programming for years and have managed to not have to get to involved with implementing my own templates classes, so the following probably has a simple solution. If I have declared and defined the following template class.... template class MyClass{ public: //.....................public members and functions protected: int m_int; T m_data }; MyClass my_ints; MyClass my_chars; The my_ints instantiation cannot access the my_char.m_int protected member variable even though that member variable is of the same type regardless of the underlying type the template encapsulates. This should have a simple solution. Thanks. JK

      J Offline
      J Offline
      Justin Tay
      wrote on last edited by
      #2

      Perhaps it would be clearer if you provided an example of how you are accessing my_char.m_int from my_ints.

      J 1 Reply Last reply
      0
      • J Justin Tay

        Perhaps it would be clearer if you provided an example of how you are accessing my_char.m_int from my_ints.

        J Offline
        J Offline
        JKallen
        wrote on last edited by
        #3

        OK Here goes.... Here is a useless example in real life, but this is a simplified example of where I am getting a compiler error. template class MyClass{ public: //.....................public members and functions virtual bool copy_whatever_I_can(const MyClass& object); protected: int m_int; T m_data }; template virtual bool MyClass::copy_whatever_I_can(const MyClass& the_char){ m_int = the_char.m_int; //all objects of MyClass have the m_int variable return true; } MyClass my_ints; MyClass my_chars; my_ints.copy_whatever_I_can(my_chars); //<----this causes the cannot access protected member error

        J J 2 Replies Last reply
        0
        • J JKallen

          OK Here goes.... Here is a useless example in real life, but this is a simplified example of where I am getting a compiler error. template class MyClass{ public: //.....................public members and functions virtual bool copy_whatever_I_can(const MyClass& object); protected: int m_int; T m_data }; template virtual bool MyClass::copy_whatever_I_can(const MyClass& the_char){ m_int = the_char.m_int; //all objects of MyClass have the m_int variable return true; } MyClass my_ints; MyClass my_chars; my_ints.copy_whatever_I_can(my_chars); //<----this causes the cannot access protected member error

          J Offline
          J Offline
          Justin Tay
          wrote on last edited by
          #4

          MyClass and MyClass are unrelated types. But i'm actually not sure how you can reach that protected error, since by right, the MyClass class only has a function that looks like copy_whatever_I_can(const MyClass& the_char) and this is not a compatible type with MyClass. Is there something I'm missing? (Perhaps you are using a template function instead?) -- modified at 14:41 Sunday 20th August, 2006

          J 1 Reply Last reply
          0
          • J Justin Tay

            MyClass and MyClass are unrelated types. But i'm actually not sure how you can reach that protected error, since by right, the MyClass class only has a function that looks like copy_whatever_I_can(const MyClass& the_char) and this is not a compatible type with MyClass. Is there something I'm missing? (Perhaps you are using a template function instead?) -- modified at 14:41 Sunday 20th August, 2006

            J Offline
            J Offline
            JKallen
            wrote on last edited by
            #5

            Actually you are correct. This was a bad example. I am trying to not copy and paste all the code becasue it is complex. The long and short of it is, forgetting the parameter mistype in the function declaration and defintion, the question is "can templates with different basic underlying types access each others protected members assuming the protected member type is independant of the underlying base type?" A simple analgoy is a list object. you have the following protected members; T* m_data; int m_length; All lists have the m_length variable to manage the m_data variable. I would think there is a way to get an object of type List and List to access each other's m_length variable directly. I recognize that a public function can return this but lets say for arguments sake I dont want to create the function to return length.

            1 Reply Last reply
            0
            • J JKallen

              I have been programming for years and have managed to not have to get to involved with implementing my own templates classes, so the following probably has a simple solution. If I have declared and defined the following template class.... template class MyClass{ public: //.....................public members and functions protected: int m_int; T m_data }; MyClass my_ints; MyClass my_chars; The my_ints instantiation cannot access the my_char.m_int protected member variable even though that member variable is of the same type regardless of the underlying type the template encapsulates. This should have a simple solution. Thanks. JK

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              I think you're misunderstanding templates and member access. A template is not a class, it's a template. One specialization of MyClass<> doesn't have any special access to the members of a different specialization. Each specialization is a different type, so MyClass<int> can't access protected members of MyClass<char>. The fact that they were both generated from the MyClass template doesn't change things.

              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

              1 Reply Last reply
              0
              • J JKallen

                OK Here goes.... Here is a useless example in real life, but this is a simplified example of where I am getting a compiler error. template class MyClass{ public: //.....................public members and functions virtual bool copy_whatever_I_can(const MyClass& object); protected: int m_int; T m_data }; template virtual bool MyClass::copy_whatever_I_can(const MyClass& the_char){ m_int = the_char.m_int; //all objects of MyClass have the m_int variable return true; } MyClass my_ints; MyClass my_chars; my_ints.copy_whatever_I_can(my_chars); //<----this causes the cannot access protected member error

                J Offline
                J Offline
                jk chan
                wrote on last edited by
                #7

                try this if you are free template class ONE { public: template void CopyThis(ONE &s) { _int = s._int; } int _int; T ss; }; ONE ls; ONE dir; dir._int = 100; ls.CopyThis(dir);

                If u can Dream... U can do it

                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