Templates and protected members [modified]
-
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
-
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
Perhaps it would be clearer if you provided an example of how you are accessing my_char.m_int from my_ints.
-
Perhaps it would be clearer if you provided an example of how you are accessing my_char.m_int from my_ints.
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
-
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
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
-
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
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.
-
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
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, soMyClass<int>
can't access protected members ofMyClass<char>
. The fact that they were both generated from theMyClass
template doesn't change things.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
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