CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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); }

  2. #17
    Join Date
    Oct 2008
    Posts
    1,456

    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?

  3. #18
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: protected member OOP question

    Quote Originally Posted by GCDEF View Post
    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:

    1. What about if you choose to make GetCount return a temporary?
    2. What if the Get/Set functions need to be virtual?
    3. Do they have to be inline?
    4. 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...
    5. 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.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Page 2 of 2 FirstFirst 12

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
  •  





Click Here to Expand Forum to Full Width

Featured