I ran into some difficult problem in some C ++ code, which is easiest to describe using code. I have classes that are something like this:
class MyVarBase { } class MyVar : public MyVarBase { int Foo(); } class MyBase { public: MyBase(MyVarBase* v) : m_var(v) {} virtual MyVarBase* GetVar() { return m_var; } private: MyVarBase* m_var; }
I also have a subclass of MyBase, which must have a member of type MyVar, because it needs to call Foo. Moving the Foo function to MyVarBase is not an option. Does it make sense to do this:
class MyClass : public MyBase { public: MyClass(MyVar* v) : MyBase(v), m_var(v) {} MyVar* GetVar() { return m_var; } private: MyVar* m_var; }
This seems to work, but it looks very bad, and I'm not sure if this will cause a memory leak or break the copy constructor. My other options might be to name the MyVar variable in MyClass something else, but have it equal to the m_var pointer in the database or templatise MyBase in the MyVar type.
All of these options do not seem ideal, so I wanted to know if anyone else had a situation like this, and if there is a good way to make it work.
c ++ oop
Bonnici
source share