-
June 29th, 2009, 11:42 AM
#16
Re: protected member OOP question
We have macros that create the get and set functions for you. Here's an example of one of them.
Code:
// Purpose:
// HAS_A is used for quickly creating a private class member variable with corresponding
// public Get and Set functions.
//
// Usage:
// HAS_A( type , name )
// Example: HAS_A(int, Count)
// The above example expands to the following code:
//
// private:
// int m_Count;
// public:
// const int & GetCount () const { return m_Count; }
// void SetCount(const int & inValue) { m_Count = (( int &)inValue); }
//
// Notes:
// 1) HAS_A can change the scope of members and methods declared after it. In the following
// example the declaration of X is public, not private.
//
// private:
// HAS_A(int, Count)
// int X;
//
// For this reason HAS_A is usually used at the end of a class.
//
// 2) HAS_A is case sensitive. The following example creates Getcount and Setcount methods,
// not GetCount and SetCount.
//
// HAS_A(int, count)
#define HAS_A( inType, inName )\
private: \
inType m_ ## inName; \
public: \
virtual const inType & inName () const { return m_ ## inName; } \
virtual void inName(const inType & inValue) { m_ ## inName = (( inType &)inValue); }
-
June 29th, 2009, 04:17 PM
#17
Re: protected member OOP question
Hi GCDEF,
Reading your post I read the line "void inName(const inType & inValue) { m_ ## inName = (( inType &)inValue); }"
and I'm wondering why you used the "( inType &)" cast: given that a cast to a reference is always an l-value its effect should be to allow assignment operator with non const reference argument, am I right ? if yes, why do you feel it necessary?
-
June 29th, 2009, 08:26 PM
#18
Re: protected member OOP question
Originally Posted by GCDEF
We have macros that create the get and set functions for you. Here's an example of one of them.
Code:
// Purpose:
// HAS_A is used for quickly creating a private class member variable with corresponding
// public Get and Set functions.
//
// Usage:
// HAS_A( type , name )
// Example: HAS_A(int, Count)
// The above example expands to the following code:
//
// private:
// int m_Count;
// public:
// const int & GetCount () const { return m_Count; }
// void SetCount(const int & inValue) { m_Count = (( int &)inValue); }
//
// Notes:
// 1) HAS_A can change the scope of members and methods declared after it. In the following
// example the declaration of X is public, not private.
//
// private:
// HAS_A(int, Count)
// int X;
//
// For this reason HAS_A is usually used at the end of a class.
//
// 2) HAS_A is case sensitive. The following example creates Getcount and Setcount methods,
// not GetCount and SetCount.
//
// HAS_A(int, count)
#define HAS_A( inType, inName )\
private: \
inType m_ ## inName; \
public: \
virtual const inType & inName () const { return m_ ## inName; } \
virtual void inName(const inType & inValue) { m_ ## inName = (( inType &)inValue); }
I have a few questions about that:
- What about if you choose to make GetCount return a temporary?
- What if the Get/Set functions need to be virtual?
- Do they have to be inline?
- Also, back onto the subject of protected, do you have a macro for creating protected members that only derived classes can access with Get/Set functions? Which brings me to another question...
- Not all members will need read and write access, right?
Some of my questions are a bit silly, but I'm curious as to the point where you stop using the macro and start declaring odd Get/Set functions here and there that are virtual or exist under different access modifiers. I guess I like to be consistent in that I rarely create inline functions. The macro idea is a good one though, but I can't help but think that this is where some automated tool would come in handy (like VS Snippets which AFAIK don't exist for C++) so that you don't have code littered with macros but at the same time don't have to go around manually typing 2 function declarations and definitions for every member.
Last edited by Mybowlcut; June 29th, 2009 at 08:29 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|