virtual member?
-
Hi. I am not sure how to explain this but I want to declare a member in a base class. IN a class derived from this, I want to be able to be able to override the member with another class that is derived from that original member. Ex: class CBase { virtual CTest m_test; } class CBase1 : public CBase { virtual CTest1 m_test; } CTest1 is derived from CTest. I want to be able to almost use the same name in each class and just add more fuunctionality in the CBase1 class, but CBase will still be able to call fuctions from m_test. Then in CBase1, it can call additional functions available in CTest1... This is like a virtual function but I want to do it for a member variable or something. I am not sure if I explained it right. Anyone can give me an idea? Thanks. Stan the man
-
Hi. I am not sure how to explain this but I want to declare a member in a base class. IN a class derived from this, I want to be able to be able to override the member with another class that is derived from that original member. Ex: class CBase { virtual CTest m_test; } class CBase1 : public CBase { virtual CTest1 m_test; } CTest1 is derived from CTest. I want to be able to almost use the same name in each class and just add more fuunctionality in the CBase1 class, but CBase will still be able to call fuctions from m_test. Then in CBase1, it can call additional functions available in CTest1... This is like a virtual function but I want to do it for a member variable or something. I am not sure if I explained it right. Anyone can give me an idea? Thanks. Stan the man
there's no such thing as a virtual member. but, can you make CTest a base class of CTest1 ? then you can override members in CTest to add that additional functionality.
-
Hi. I am not sure how to explain this but I want to declare a member in a base class. IN a class derived from this, I want to be able to be able to override the member with another class that is derived from that original member. Ex: class CBase { virtual CTest m_test; } class CBase1 : public CBase { virtual CTest1 m_test; } CTest1 is derived from CTest. I want to be able to almost use the same name in each class and just add more fuunctionality in the CBase1 class, but CBase will still be able to call fuctions from m_test. Then in CBase1, it can call additional functions available in CTest1... This is like a virtual function but I want to do it for a member variable or something. I am not sure if I explained it right. Anyone can give me an idea? Thanks. Stan the man
:| I'm not certain I understand what you want to achieve, but my gut feeling is that you cannot do that. maybe if you create a base class for CTest and CTest1 and keep a pointer to that class instead of CTest and CTest1 ?
// not tested, and probably will not work as-is
class CTestBase {};
class CTest : public CTestBase {};
class CTest1 : public CTestBase {};// and have
class CBase
{
CBase() { m_pTest = new CTest;};
CTestBase* m_pTest;
};class Cbase1 : public CBase
{
CBase1() { m_pTest = new CTest1;};
CTestBase* m_pTest;
};but you will probably need to use something like CBase::m_pTest and CBase1::m_pTest to resolve potential name conflicts.
Maximilien Lincourt Your Head A Splode - Strong Bad
-
there's no such thing as a virtual member. but, can you make CTest a base class of CTest1 ? then you can override members in CTest to add that additional functionality.
Hi Chris. Yes, that is not a problem. The one thing is that if the (virtual) member/class is already declared in another base class. Then this base class I derive another class. But for this class, I need a slightly different functionality of that "virtual" member/class. But I do not want to disturb the function of the original (virtual) member/ class in that other base class. You know my meaning? I want to "replace" that class with another class which has identical functions but with some changes... Thanks. Stan the man
-
:| I'm not certain I understand what you want to achieve, but my gut feeling is that you cannot do that. maybe if you create a base class for CTest and CTest1 and keep a pointer to that class instead of CTest and CTest1 ?
// not tested, and probably will not work as-is
class CTestBase {};
class CTest : public CTestBase {};
class CTest1 : public CTestBase {};// and have
class CBase
{
CBase() { m_pTest = new CTest;};
CTestBase* m_pTest;
};class Cbase1 : public CBase
{
CBase1() { m_pTest = new CTest1;};
CTestBase* m_pTest;
};but you will probably need to use something like CBase::m_pTest and CBase1::m_pTest to resolve potential name conflicts.
Maximilien Lincourt Your Head A Splode - Strong Bad
HI Max. Thanks for the comment. I think I can use something like this. Don't know why I did not think of it. Thanks. Stan the man
-
Hi. I am not sure how to explain this but I want to declare a member in a base class. IN a class derived from this, I want to be able to be able to override the member with another class that is derived from that original member. Ex: class CBase { virtual CTest m_test; } class CBase1 : public CBase { virtual CTest1 m_test; } CTest1 is derived from CTest. I want to be able to almost use the same name in each class and just add more fuunctionality in the CBase1 class, but CBase will still be able to call fuctions from m_test. Then in CBase1, it can call additional functions available in CTest1... This is like a virtual function but I want to do it for a member variable or something. I am not sure if I explained it right. Anyone can give me an idea? Thanks. Stan the man
I think there's a bit of an OO design issue going on here. Perhaps you need some data hiding...
class CBase { public: virtual int AccessorFunctionThatDealsWithTest(); private: CTest m_test; }; class CDerived1 : public CBase { public: int AccessorFunctionThatDealsWithTest();//override of CBase virtual function private: CSomeOtherTest m_test; };
If you do this then it matters not if CTest and CSomeOtherTest are related, they can be but the 'user' of CBase:CDerived1 hierarchy doesn't have to know or care because the AccessorFunctionThatDealsWithTest() interface is the same even though its implementation might be entirely different. Now that makes me :-DNothing is exactly what it seems but everything with seems can be unpicked.