Skip to main content
Tweeted twitter.com/StackSoftEng/status/945447066314133505
added public/private to clarify that I was not making a comment about the bool's visibility.
Source Link
sdg
  • 1.9k
  • 10
  • 16

(Figured this was too subjective for SO, so posting here...)

I have some behavior that I can implement in various ways. At least two methods are shows in the code snippet below. Presume that the m_well data member is correctly set somehow at object construction time.

struct Behavior { virtual bool behavesWell() { return true; } private:  bool m_well; public: bool behavesMemberWell() { return m_well; } }; struct OtherBehavior : public Behavior { virtual bool behavesWell() { return false; } }; 

Obviously the one relies upon virtual dispatch, and the other merely the return of a data member.

A third, non-shown method would likely have the non-virtual public member function not return a fixed data member, but instead call a virtual - lets leave that aside for the purpose of this please.

What would lead you to one or other other of these two methods of implementing functionality, such that a user of this class can interrogate an object's behavior?

(Figured this was too subjective for SO, so posting here...)

I have some behavior that I can implement in various ways. At least two methods are shows in the code snippet below. Presume that the m_well data member is correctly set somehow at object construction time.

struct Behavior { virtual bool behavesWell() { return true; } bool m_well; bool behavesMemberWell() { return m_well; } }; struct OtherBehavior : public Behavior { virtual bool behavesWell() { return false; } }; 

Obviously the one relies upon virtual dispatch, and the other merely the return of a data member.

A third, non-shown method would likely have the non-virtual public member function not return a fixed data member, but instead call a virtual - lets leave that aside for the purpose of this please.

What would lead you to one or other other of these two methods of implementing functionality, such that a user of this class can interrogate an object's behavior?

(Figured this was too subjective for SO, so posting here...)

I have some behavior that I can implement in various ways. At least two methods are shows in the code snippet below. Presume that the m_well data member is correctly set somehow at object construction time.

struct Behavior { virtual bool behavesWell() { return true; } private:  bool m_well; public: bool behavesMemberWell() { return m_well; } }; struct OtherBehavior : public Behavior { virtual bool behavesWell() { return false; } }; 

Obviously the one relies upon virtual dispatch, and the other merely the return of a data member.

A third, non-shown method would likely have the non-virtual public member function not return a fixed data member, but instead call a virtual - lets leave that aside for the purpose of this please.

What would lead you to one or other other of these two methods of implementing functionality, such that a user of this class can interrogate an object's behavior?

Source Link
sdg
  • 1.9k
  • 10
  • 16

What are your decision critera in C++ to make something a data member or virtual method?

(Figured this was too subjective for SO, so posting here...)

I have some behavior that I can implement in various ways. At least two methods are shows in the code snippet below. Presume that the m_well data member is correctly set somehow at object construction time.

struct Behavior { virtual bool behavesWell() { return true; } bool m_well; bool behavesMemberWell() { return m_well; } }; struct OtherBehavior : public Behavior { virtual bool behavesWell() { return false; } }; 

Obviously the one relies upon virtual dispatch, and the other merely the return of a data member.

A third, non-shown method would likely have the non-virtual public member function not return a fixed data member, but instead call a virtual - lets leave that aside for the purpose of this please.

What would lead you to one or other other of these two methods of implementing functionality, such that a user of this class can interrogate an object's behavior?