From: Ben Voigt [C++ MVP] on


"David Webber" <dave(a)musical-dot-demon-dot-co.uk> wrote in message
news:#4AL4$pNKHA.4816(a)TK2MSFTNGP04.phx.gbl...
>
> "Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message
> news:%23BGF7ilNKHA.5488(a)TK2MSFTNGP02.phx.gbl...
>
>> David Webber <dave(a)musical-dot-demon-dot-co.uk> wrote:
>
>>> In circumstances like that I tend to declare structures with all
>>> members "static".
>>
>> No such thing exists in C.
>
> Ah sorry - my memory of what subset of C++ exists in C is obviously
> getting rather vague :-(
>
>>> Then to access the data, you simply use
>>>
>>> MyStruct.element;
>>
>> Surely you mean MyStruct::element
>
> I'm definitely having a bad day expressing myself too. I had in mind
>
> MYSTRUCT MyStruct; // (as it would be in C++)
> Mystruct.element;

It's considered very poor practice to use an object instance to refer to a
static member because anyone reading that will automatically think element
is an instance member variable.

(The exceptions would be when the data type isn't known, is very hard to
name, or the member may be either static or per-instance depending on
template parameters.)

From: David Webber on

"Ben Voigt [C++ MVP]" <bvoigt(a)newsgroup.nospam> wrote in message
news:C132E666-226D-4008-8BD7-FBC9AD4FDE37(a)microsoft.com...

>> MYSTRUCT MyStruct; // (as it would be in C++)
>> Mystruct.element;
>
> It's considered very poor practice to use an object instance to refer to a
> static member because anyone reading that will automatically think element
> is an instance member variable...

I was trying to keep somewhere in line with C - in fact I don't think I ever
do this. What I *do* do is use instances to call static member functions
(and hang the purists <g>.).

In the example I gave, MyClass stores the HINSTANCE of a language resource
DLL statically.

MyClass.DoACertainDialogueBox( ... );

is not static, but

MyClass.LoadString( ... );

is, and if I have an instance, I have no qualms about using it with the
latter.

In fact even the first of these doesn't actually use any non-static data.
But it does require that the constructor has been called, because the
constructor calls AfxSetResourceHandle() and stores the old one, which is
restored by the destructor.

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm