Design Question mem vars

Posted on 2006-05-26
Last Modified: 2013-11-20
I have two very basic quesions which I hope someone could help me out with:

Say I have a class

with member function PrintAccountName()
with private member variable m_dbAccountNumber.

Is it good practice to access the member variable directly in its own class or should I have a GetAccount() and SetAccount public member functions to access the variablei.e.


Question by:XPUSR

    Author Comment

    Is there any good book/site that deals with such basic class design issues?
    LVL 43

    Expert Comment

    What is probably the *better* way would be

    LPCTSTR CAccount::GetAccountNumber()
                 return m_dbAccountNumber;

    (assuming the account number is a string)

    Reason - if you wanted to check validity then it is much easier to do it in one place rather than all the places you use the account number.

    Also should you need to make a change then you only need to change the code in one place - the class.
    LVL 43

    Expert Comment

    Similar code for setting the account number.

    Author Comment

    Thanks AndyAinscow,

    But that doesn't really answer my question. If I access m_dbAccountNumber directly I still only need to change the code once. My question is is there a neet to use a get method?
    LVL 43

    Expert Comment

    There is no need to use functions to Get/Set the variables however there are advantages (as I said initially).
    LVL 8

    Accepted Solution

    I think you don't really give enough details to answer this fully for this specific case. But my first thoughts are:

    - if the variable is beeing used only in the class, you should at least make SetAccount/GetAccount private
    - if you are going to use the account number many places I agree with Andy, that a GetAccount function gives you the possibility for things like raising an exception or returning "INVALID" for invalid numbers etc. in a consistent way.
    - I would more often have only a Set-function to ensure only legal values are ever set
    - I would still postulate that the "normal" member variable should be accessed directly in the class' own functions


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now