Out of curiosity, why would I ever want private elements of a struct? I get having constructors and function pointers is awesome but why would I ever want to hide it? Sorry if this comes off as inexperienced but I've never done anything in C++ like this before.
O_o
Consider:
Code:
class SMatrix
{
// ...
f32 m[4][4];
};
So, you have a "4x4" class representing matrices, and you need to use that class so "..." represents a lot of good mechanics.
Unfortunately, you can't, shouldn't try to, provide for every mechanic, but you eventually find you need to implement some mathematics for a client your interface doesn't support.
Code:
void DoSomething
(
SMatrix & f
)
{
// ...
f.m[1][1] = 0;
// ...
}
Everything in the universe is grand.
Except that the core routines are consuming most of the available compute--profiling--so you can legitimately to try and improve the implementation.
Code:
class SMatrix
{
// ...
__m128 m[4];
};
You take the time to rewrite the core methods supporting "SSE" or similar instruction set.
Now you have a problem: none of the client code, `DoSomething', works with the new version because a `__m128' is not a `f32[4][4]'. You could solve that with operator overloading by means of a second class overloading `operator []'.
Code:
class SVector
{
// ...
__m128 m;
};
class SMatrix
{
// ...
SVector m[4];
};
Now everything again works, but you become aware of an instruction set and algorithm which improves many operations by interleaving the data, and in implementing that, you are back at square one: the client code doesn't work.
Code:
class SMatrix
{
// ...
void getAll(f32 *);
void getColumn(size_t, f32 *);
void getElement(size_t, size_t, f32 *);
void getRow(size_t, f32 *);
// ...
void setAll(f32 *);
void setColumn(size_t, f32 *);
void setElement(size_t, size_t, f32 *);
void setRow(size_t, f32 *);
// ...
__m128 m[4];
};
void DoSomething
(
SMatrix & f
)
{
// ...
f.setElement(1, 1, 0);
// ...
}
This code works everywhere, and it would work everywhere regardless of `m' being `public', but by marking `m' as `public' you tell client code not to use `m' in the first place allowing you to make changes like these without potentially causing conflict in client code.
Soma