Click here to Skip to main content
15,910,877 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionis this good practice? Pin
Roman Nurik26-Feb-02 10:17
Roman Nurik26-Feb-02 10:17 
AnswerRe: is this good practice? Pin
Nemanja Trifunovic26-Feb-02 10:27
Nemanja Trifunovic26-Feb-02 10:27 
GeneralRe: is this good practice? Pin
Roman Nurik26-Feb-02 10:24
Roman Nurik26-Feb-02 10:24 
GeneralRe: is this good practice? Pin
Christian Graus26-Feb-02 10:54
protectorChristian Graus26-Feb-02 10:54 
GeneralRe: is this good practice? Pin
Maximilien26-Feb-02 11:01
Maximilien26-Feb-02 11:01 
GeneralRe: is this good practice? Pin
Michael Dunn26-Feb-02 11:41
sitebuilderMichael Dunn26-Feb-02 11:41 
GeneralRe: is this good practice? Pin
Carlos Antollini26-Feb-02 10:31
Carlos Antollini26-Feb-02 10:31 
AnswerRe: is this good practice? Pin
Jon Hulatt27-Feb-02 1:04
Jon Hulatt27-Feb-02 1:04 
Everone shouts "NO!!" to your question. I'd agree with them, but thought it'd be useful to explain why...

Imagine you've written your class with public members, and for example, you had a char * pointer as a member. Your app is free to both read & write to this pointer as needed.

As your app grows, you want to make it more efficient. So you go multi threaded. You could now get yourself into a situation where one thread is in the middle of reading data from that pointer, but a totally different thread is trying to write!. At best, you get data corruption. At worse, you get a protection error.

If you'd religiously used accessors and mutators, it would be minimal work to prevent this happening. You'd make sure that your accessor and mutator member functions used mutexes (or some other sync object, as appropriate) to police access to the member variable.

The other main reason is readability. Imagine these two lines, which are functionally equivalent:

class CYourClass
{
   public:
     char *m_pBuffer;
}


....


CYourClass *p_YC = new CYourClass

char mystring[]="hello world";

// you want to copy mystring's "hello world" into the class:

// the shite way: confusing to look at-
p_YC->m_pBuffer = new char[strlen(mystring)];
strcpy(p_YC->m_pBuffer, (char *) mystring);

// the easy way:
p_YC->SetBuffer( (char *) mystring);


in that example, you could do clever stuff like having the SetBuffer() mutator handle things like memory allocation for m_pBuffer. It simplifies your code.




Sorry to dissapoint you all with my lack of a witty or poignant signature.
GeneralRe: is this good practice? Pin
Bill Wilson27-Feb-02 8:05
Bill Wilson27-Feb-02 8:05 
GeneralRe: is this good practice? Pin
Roman Nurik27-Feb-02 9:43
Roman Nurik27-Feb-02 9:43 
AnswerRe: is this good practice? Pin
Stan Shannon27-Feb-02 1:29
Stan Shannon27-Feb-02 1:29 
GeneralOpenGl opening files Pin
Nnamdi Onyeyiri26-Feb-02 10:08
Nnamdi Onyeyiri26-Feb-02 10:08 
GeneralAuto-complete file combo Pin
Chris Losinger26-Feb-02 8:52
professionalChris Losinger26-Feb-02 8:52 
GeneralRe: Auto-complete file combo Pin
Michael Dunn26-Feb-02 11:42
sitebuilderMichael Dunn26-Feb-02 11:42 
GeneralRe: Auto-complete file combo Pin
Chris Losinger26-Feb-02 12:24
professionalChris Losinger26-Feb-02 12:24 
Generalnevermind... Pin
Chris Losinger26-Feb-02 13:44
professionalChris Losinger26-Feb-02 13:44 
GeneralGlobalAddAtom and RegisterHotKey Pin
26-Feb-02 8:41
suss26-Feb-02 8:41 
GeneralRe: GlobalAddAtom and RegisterHotKey Pin
Carlos Antollini26-Feb-02 9:03
Carlos Antollini26-Feb-02 9:03 
GeneralGlobal Variables Pin
Rajveer26-Feb-02 8:35
Rajveer26-Feb-02 8:35 
GeneralRe: Global Variables Pin
Chris Losinger26-Feb-02 8:34
professionalChris Losinger26-Feb-02 8:34 
GeneralRe: Global Variables Pin
Nemanja Trifunovic26-Feb-02 8:39
Nemanja Trifunovic26-Feb-02 8:39 
GeneralRe: Global Variables Pin
Carlos Antollini26-Feb-02 8:40
Carlos Antollini26-Feb-02 8:40 
GeneralRe: Global Variables Pin
Christian Graus26-Feb-02 10:56
protectorChristian Graus26-Feb-02 10:56 
GeneralRe: Global Variables Pin
Nemanja Trifunovic26-Feb-02 11:06
Nemanja Trifunovic26-Feb-02 11:06 
GeneralRe: Global Variables Pin
Christian Graus26-Feb-02 11:08
protectorChristian Graus26-Feb-02 11:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.