|
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";
p_YC->m_pBuffer = new char[strlen(mystring)];
strcpy(p_YC->m_pBuffer, (char *) mystring);
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.
|
|
|
|
|
Readability is not the only benefit to encapsulation. The primary reason, in my opinion, is to divorce the audience of a member variable from the derivation of its value.
If you use accesor and mutator methods you can do things like changing the underlying storage or even add qualifications and calculations without impacting the audience of the value.
For example:
Say you have an inventory class that contains a Price variable. Then at a later date, after you've built your app, a buisiness decision is made to vary the price by warehouse location. If you used an accesor (GetPrice), you only need to modify it to accomodate the calculation, rather than modifying every reference to m_price in the entire app.
Just my opinion for what its worth,
Bill
|
|
|
|
|
well i dont plan on storing pointers as member variables, i plan to make classes to control the pointers. the only simple member variables would be maybe integers or such...
e.g.
class MyClass
{
public:
SmartPointer<char> pBuffer;
int nType;
MyClass();
};
i see the problem with this though, that there can be no validation but the pointer thing shouldnt be a problem as long as auto_ptr or another smart pointer template class is used
>>Roman<<
|
|
|
|
|
One very crucial aspect of doing Object oriented programming is the concept of data encapsulation. If your member variables are public they are not encapsulated. Making them public out of convenience starts a process of laziness which ends with every declared object made public to the entire application and every member of every object is therefore a global public variable. This defeats the entire purpose of OO design.
I have sadly been required to work on many applications which fell victem to this very process. Don't do it. Please.
Take the time to properly encapsulate and manage your data. The time you will ultimately save yourself and others (like me) will be well worth the extra effort upfront.
"There's a slew of slip 'twixt cup and lip"
|
|
|
|
|
I would like to know if there is a way to open picture files other than tga's, bitmaps or jpegs, and how to do it, and to open 3d scene files for truespace, as i do not know milkshape 3d, which seems to be so common.
==================================================
When Your Mind Wonders...Where Does It Go???
|
|
|
|
|
Does anyone know how to create one of those? I see them in many places besides the common file open dialog, so i assume it's common code from somewhere...
-c
Smaller Animals Software, Inc.
You're the icing - on the cake - on the table - at my wake. Modest Mouse
|
|
|
|
|
|
any other hints...?
this:
#include "Shlwapi.h"
...
SHAutoComplete(m_hWnd, SHACF_FILESYSTEM);
gives me an 'SHAutoComplete' : undeclared identifier . searching the windows headers for SHAutoComplete turns up 0 matches.
is it some kind of platform SDK thing?
-c
Smaller Animals Software, Inc.
You're the icing - on the cake - on the table - at my wake. Modest Mouse
|
|
|
|
|
|
Hi guys,
when I use GlobalAddAtom and RegisterHotKey in my app, must I call GlobalDeleteAtom and UnregisterHotKey when it exits, or is it done automatically ?
Thanks
|
|
|
|
|
From MSDN
Global atoms are not deleted automatically when the application terminates. For every call to the GlobalAddAtom function, there must be a corresponding call to the GlobalDeleteAtom function
Regards
Carlos Antollini.
Sonork ID 100.10529 cantollini
|
|
|
|
|
I've downloaded an MDI demo application and the global variables are stored in(whateverIcalledMyProject)view.cpp
I'm making alterations to the project and I need to use the globals in other cpp files. But when I try to do that, I get an error message that the variable is an undeclared identifier. I can't include another cpp file at the top of the file. So how do I make the file aware of global variables in other files?
|
|
|
|
|
extern int g_myGlobalVariable;
you need to add something similar to every .cpp that needs the variable.
or, you can put that in a .H file and #include that file whereever you need to use the variable. or, just put that in your stdafx.h.
and, yes, you can #include .cpp files, but that won't help you here.
-c
Smaller Animals Software, Inc.
You're the icing - on the cake - on the table - at my wake. Modest Mouse
|
|
|
|
|
extern
I vote pro drink
|
|
|
|
|
You need to declare your global variables in the app file. You can get the vaiable valuefrom any cpp using theApp.global_variable
Best Regards...
Carlos Antollini.
Sonork ID 100.10529 cantollini
|
|
|
|
|
The best way is to declare them extern in the stdafx.h, and declare them in stdafx.cpp.
Anyone who adds globals to a project without wrapping them in a namespace should move on the VB, in my opinion. That includes the writers of Microsoft sample code.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
|
|
|
|
|
Christian Graus wrote:
Anyone who adds globals to a project without wrapping them in a namespace should move on the VB
LOL, VB 6.0 you mean. Because, with VB.NET there are no global variables.
I vote pro drink
|
|
|
|
|
Nemanja Trifunovic wrote:
with VB.NET there are no global variables.
I knew that, actually, because I've been reading my C# book. One of the first differences to C++ that I thought was actually excellent.
I've been reading 'Design and Evolution of C++', as I've probably said, and it's interesting to me that Stroustrup does not like a lot of C++ constructs that remained for C compatibility. Reading through some of his statements I think there are many aspects of C# he would approve of, although that is all undermined by his public distaste for languages owned by a corporation.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
|
|
|
|
|
Christian Graus wrote:
I think there are many aspects of C# he would approve of
The one he would really like (I love it) is the use of ref and out in function calls. With C++, you must go to the function definition to learn whether the variable is passed by value or by reference.
I vote pro drink
|
|
|
|
|
I need to have a XOR'd full screen mouse cross hair style cursor tracker.
Something similar to DrawFocusRect would produce when clicking and dragging in a window, but "without" using OnMouseUp and OnMouseDown I only want to draw the lines when the mouse in moving.
XOR lines require you to draw them in the same location to remove them so how using OnMouseMove would I implement something like the above...??? I'm at a loss for ideas on how to do this...???
repainting the screen on each mousemove is unacceptable! It has to be XOR'ed.
Thanx a bunch!
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
use OnMouseMove.
if the mouse is in the rect you're interested in:
get a CClientDC
draw the x-hairs at the "last" pos
draw them at the current pos
set "last" = current
there's probably a lot of special case stuff that you'll have to deal with, but that's the basic way i do this kind of thing.
-c
Smaller Animals Software, Inc.
You're the icing - on the cake - on the table - at my wake. Modest Mouse
|
|
|
|
|
Thats almost what i've been doing, but still i'm getting the same problem. When the mouse first moves over the client area the first drawn rect stays put and the rest are removed properly.
CClientDC dc(this);
static int lastX=point.x, lastY=point.y;
dc.DrawFocusRect(CRect(-10, -10, lastX, lastY));
dc.DrawFocusRect(CRect(-10, -10, point.x, point.y));
lastX = point.x;
lastY = point.y;
Any ideas...?
Thanx again!
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
try initializing "last" to something offscreen. as it is now, you're drawing that first rect three times (twice here, and once again the next time you hit this function). you've got to be sure you're only drawing the same spot twice (or any multiple of 2, actually)
-c
Smaller Animals Software, Inc.
You're the icing - on the cake - on the table - at my wake. Modest Mouse
|
|
|
|
|
Hey thanx again i've managed to fixed the problem with the following:
static BOOL bFirst = TRUE;
static int lastX=point.x, lastY=point.y;
if(bFirst)
dc.DrawFocusRect(CRect(-10, -10, point.x, point.y));
else{
dc.DrawFocusRect(CRect(-10, -10, lastX, lastY));
dc.DrawFocusRect(CRect(-10, -10, point.x, point.y));
}
lastX = point.x;
lastY = point.y;
bFirst = FALSE;
Now the only problem is on moving or resizing the last drawn rect re-appears where it last left off. Almost there I am...
Thanx again!
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
hi, my question is actually about opengl. I have a 3D scene, in my window (opengl scene, off course), and I want to make it disappear when it translates beyond a certain depth (negative z direction) into the screen. How do I achieve this?
Also, is there any discussion thread (on code project or other sites) that is specific to just openGL?
|
|
|
|