But really we're just arguing semantics at this point. |
It's always contentious when someone says "Never do ...", so I guess I should expect to have to explain myself. But I really thought it was self evident.
kbw: How are you defining the term "object"? Because it sounds to be like you're defining an object as something that has no public members. In which case you're right... an object should never have public members because if it does, that (by definition) makes it not an object. |
I probably meant object oriented class and certainly not C++ class. But object, being an instance of class, is a shortcut to what I meant. So, to reword it, neither the object classification or an instance of that classification should have public data. But that sounds awful and doesn't really answer the OPs question.
My definition is very simple. An object is an instance of a class. I find that's what most people mean when they say "object". |
That's a tricky one. Do you mean most C++ people? C++ has a constrained object model and the keyword class implements a variety of concepts (interface, object, abstract data type, type, record).
In this case, an instance of my example Vector class would be an object, but would have public members. |
In my thinking, a vector isn't an object. It's an abstract data type. The difference is that an abstract data type is just encapsulation, but objects can be specialised. BTW, I don't recall seeing public data on vector.
For example, in the C standard library, a file is represented by a FILE* data member and fopen, close, fread, fwrite, fseek, ftell, fprintf, fscanf and so on. That's an abstract data type. In C++, that could be implemented as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
class CFILE
{
FILE* imp_;
publc:
CFILE(const char* name, const char* mode)
{
imp_ = fopen(name, mode);
if (!imp_)
{
throw std::runtime_error("fopen failed"); // something more elaborate
}
}
~CFILE() { fclose(imp_); }
read(void* buf, size_t size) { fread(buf, size, 1, imp_) }
// ... and so on
|
I'm calling that an abstract data type and not a class. I don't think public data is fine, otherwise you can't treat it as a black box.
Here's another one,
1 2 3 4 5 6 7 8 9
|
class CRect
{
int x, y, dx, dy;
public:
CRect() { x = y = dx = dy = 0; }
void origin(int top, int left) { x = top; y = left; }
void width(int w) { dx = w; }
// and so on
|
I'm calling that a record. Public data is fine, it just a grouping of units.
How about this one:
1 2 3 4 5
|
class CWnd : public CObject
{
HWND m_hWnd;
public:
// ... and so on
|
This is a class in the object oriented sense, and it should
never have public data as it does in this example.