[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Design Question mem vars

Posted on 2006-05-26
6
Medium Priority
?
165 Views
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

CAccount
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.

 CAccount::PrintAccountNumber()
{
             cout<<m_dbAccountNumber;
}



2.
0
Comment
Question by:XPUSR
  • 3
  • 2
6 Comments
 

Author Comment

by:XPUSR
ID: 16768409
Is there any good book/site that deals with such basic class design issues?
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16768498
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.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16768509
Similar code for setting the account number.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:XPUSR
ID: 16770732
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?
0
 
LVL 45

Expert Comment

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

Accepted Solution

by:
Knut Hunstad earned 200 total points
ID: 16772462
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

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Loops Section Overview
Suggested Courses
Course of the Month18 days, 13 hours left to enroll

834 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