I have a class, MyClass which contains:

typedef struct MyStruct
{
MyType * pmyTypes[14];

MyStruct()
{
pmyTypes = NULL;
}
} MyStruct;

I want to at compile time create an array of the same number of elements.
I've tried:

sizeof(MyClass::MyStruct.pmyTypes) / sizeof(MyClass::MyStruct.pmyTypes[0])

but I get a compiler error:

s:\mytest.h(155) : error C2143: syntax error : missing ')' before '.'

and I notice that when typing the '.' I don't get the list of elements in the
structure, even though I have included the file with the class definition
before I do this. Any idea where I'm going wrong this time? :-) Thanks!

Re: number of elements by Carl

Carl
Wed May 11 10:12:54 CDT 2005

Larry Waibel wrote:
> I have a class, MyClass which contains:
>
> typedef struct MyStruct

This is 'C' style - it's unnecessary in C++. Just use

struct MyStruct
{
};

unless you need this definition to be valid C as well as C++.

> {
> MyType * pmyTypes[14];
>
> MyStruct()
> {
> pmyTypes = NULL;
> }
> } MyStruct;
>
> I want to at compile time create an array of the same number of
> elements. I've tried:
>
> sizeof(MyClass::MyStruct.pmyTypes) /
> sizeof(MyClass::MyStruct.pmyTypes[0])

sizeof(MyClass::MyStruct::pmyTypes) /
sizeof(MyClass::MyStruct::pmyTypes[0])

In C++ the '.' is only used to separate a variable name from a member name.
MyStruct is a class name, so you use ::.

-cd



Re: number of elements by Victor

Victor
Wed May 11 10:16:23 CDT 2005

Larry Waibel wrote:
> I have a class, MyClass which contains:
>
> typedef struct MyStruct
> {
> MyType * pmyTypes[14];
>
> MyStruct()
> {
> pmyTypes = NULL;
^^^^^^^^^^^^^^^
This does not compile. You cannot assign to an array.

> }
> } MyStruct;

And drop that C habit of typedefing and just define the damn struct:

struct MyStruct
{
MyType *pmyTypes[14];
MyStruct();
};

>
> I want to at compile time create an array of the same number of elements.
> I've tried:
>
> sizeof(MyClass::MyStruct.pmyTypes) / sizeof(MyClass::MyStruct.pmyTypes[0])

Shouldn't this simply be

14

?

I prefer a simple work-around: define the array size in a constant,
and use it everywhere:

struct MyStruct
{
enum { myTypesSize = 14 };
MyType *pmyTypes[myTypesSize];
...
};

... MyStruct::myTypesSize ...

> [...]

V

Re: number of elements by Larry

Larry
Wed May 11 10:42:28 CDT 2005

In article <e2kEpvjVFHA.2256@TK2MSFTNGP14.phx.gbl>, Carl Daniel [VC++ MVP] wrote:
> From: "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
> Subject: Re: number of elements
> Date: Wed, 11 May 2005 08:12:54 -0700
> Newsgroups: microsoft.public.vc.language
>
> unless you need this definition to be valid C as well as C++.
>

This definition is done by someone else and I think it does need to be valid in C
as well

>
> In C++ the '.' is only used to separate a variable name from a member name.
> MyStruct is a class name, so you use ::.
>

Ok, I see. I did that (and then I do see the elements drop down) but now I get:

mytest.h(156) : error C2597: illegal reference to non-static member
'MyClass::MyStruct::pMyTypes'



Re: number of elements by Larry

Larry
Wed May 11 10:42:33 CDT 2005

In article <ej7LmxjVFHA.628@tk2msftngp13.phx.gbl>, Victor Bazarov wrote:
> Date: Wed, 11 May 2005 11:16:23 -0400
> From: Victor Bazarov <v.Abazarov@comAcast.net>
> Subject: Re: number of elements
> Newsgroups: microsoft.public.vc.language
>
> Larry Waibel wrote:
> > I have a class, MyClass which contains:
> >
> > typedef struct MyStruct
> > {
> > MyType * pmyTypes[14];
> >
> > MyStruct()
> > {
> > pmyTypes = NULL;
> ^^^^^^^^^^^^^^^
> This does not compile. You cannot assign to an array.

Sorry, that was a typo; should have been '*pmyTypes =NULL;'

>
> > }
> > } MyStruct;
>
> And drop that C habit of typedefing and just define the damn struct:
>
> struct MyStruct
> {
> MyType *pmyTypes[14];
> MyStruct();
> };
>
> >
> > I want to at compile time create an array of the same number of elements.
> > I've tried:
> >
> > sizeof(MyClass::MyStruct.pmyTypes) / sizeof(MyClass::MyStruct.pmyTypes[0])
>
> Shouldn't this simply be
>
> 14
>
> ?
>
> I prefer a simple work-around: define the array size in a constant,
> and use it everywhere:
>
> struct MyStruct
> {
> enum { myTypesSize = 14 };
> MyType *pmyTypes[myTypesSize];
> ...
> };
>
> ... MyStruct::myTypesSize ...
>
> > [...]
>
> V
>

This definition is provided by someone else who used hard-code values and I can't
be sure they won't be changed in the future so that's why I want to determine it
dynamically.


Re: number of elements by Carl

Carl
Wed May 11 11:08:09 CDT 2005

Larry Waibel wrote:
> In article <e2kEpvjVFHA.2256@TK2MSFTNGP14.phx.gbl>, Carl Daniel [VC++
> MVP] wrote:
>> From: "Carl Daniel [VC++ MVP]"
>> <cpdaniel_remove_this_and_nospam@mvps.org.nospam> Subject: Re:
>> number of elements
>> Date: Wed, 11 May 2005 08:12:54 -0700
>> Newsgroups: microsoft.public.vc.language
>>
>> unless you need this definition to be valid C as well as C++.
>>
>
> This definition is done by someone else and I think it does need to
> be valid in C as well
>
>>
>> In C++ the '.' is only used to separate a variable name from a
>> member name. MyStruct is a class name, so you use ::.
>>
>
> Ok, I see. I did that (and then I do see the elements drop down) but
> now I get:
>
> mytest.h(156) : error C2597: illegal reference to non-static member
> 'MyClass::MyStruct::pMyTypes'

Sorry, yes - I was in too much of a hurry.

You have to write something like:

sizeof((*(MyClass::MyStruct*)0).pmyTypes) /
sizeof((*(MyClass::MyStruct*)0).pmyTypes[0])

As Victor pointed out, it's best to avoid such constructs, but if the
definition is given to you and you just want to match it...

-cd




Re: number of elements by Eugene

Eugene
Wed May 11 13:16:15 CDT 2005

Larry Waibel wrote:
> In article <ej7LmxjVFHA.628@tk2msftngp13.phx.gbl>, Victor Bazarov
> wrote:
>> Larry Waibel wrote:
>>> I have a class, MyClass which contains:
>>>
>>> typedef struct MyStruct
>>> {
>>> MyType * pmyTypes[14];
>>>
>>> MyStruct()
>>> {
>>> pmyTypes = NULL;
>> ^^^^^^^^^^^^^^^
>> This does not compile. You cannot assign to an array.
>
> Sorry, that was a typo; should have been '*pmyTypes =NULL;'

Still doesn't work. Perhaps you meant to declare pmyTypes as

MyType (* pmyTypes)[14];

--
Eugene
http://www.gershnik.com





Re: number of elements by Victor

Victor
Wed May 11 13:42:52 CDT 2005

Eugene Gershnik wrote:
> Larry Waibel wrote:
>
>>In article <ej7LmxjVFHA.628@tk2msftngp13.phx.gbl>, Victor Bazarov
>>wrote:
>>
>>>Larry Waibel wrote:
>>>
>>>>I have a class, MyClass which contains:
>>>>
>>>> typedef struct MyStruct
>>>> {
>>>> MyType * pmyTypes[14];
>>>>
>>>> MyStruct()
>>>> {
>>>> pmyTypes = NULL;
>>>
>>> ^^^^^^^^^^^^^^^
>>>This does not compile. You cannot assign to an array.
>>
>>Sorry, that was a typo; should have been '*pmyTypes =NULL;'
>
>
> Still doesn't work.

REALLY?

struct Foo
{
};

struct Bar
{
Foo *foos[14];
Bar() { *foos = 0; }
// ^^^^^^^^^^ Compiles just fine. Sets foos[0] to 0.
};

int main()
{
Bar bar;
}

V

Re: number of elements by Eugene

Eugene
Wed May 11 14:58:45 CDT 2005

Victor Bazarov wrote:
>
> struct Foo
> {
> };
>
> struct Bar
> {
> Foo *foos[14];
> Bar() { *foos = 0; }
> // ^^^^^^^^^^ Compiles just fine. Sets foos[0] to 0.
> };

Sure thing but I am 99% certain that this is not what OP wanted.


--
Eugene
http://www.gershnik.com






Re: number of elements by Victor

Victor
Wed May 11 15:05:10 CDT 2005

Eugene Gershnik wrote:
> Victor Bazarov wrote:
>
>>struct Foo
>>{
>>};
>>
>>struct Bar
>>{
>> Foo *foos[14];
>> Bar() { *foos = 0; }
>>// ^^^^^^^^^^ Compiles just fine. Sets foos[0] to 0.
>>};
>
>
> Sure thing but I am 99% certain that this is not what OP wanted.

Assumption is the mother of all f***-ups.

V

Re: number of elements by Eugene

Eugene
Wed May 11 15:21:07 CDT 2005

Victor Bazarov wrote:
>
> Assumption is the mother of all f***-ups.

Yup, such as your assumptions about the language OP's headers are written in
and his control over them.


--
Eugene
http://www.gershnik.com



Re: number of elements by Victor

Victor
Wed May 11 15:38:00 CDT 2005

Eugene Gershnik wrote:
> Victor Bazarov wrote:
>
>>Assumption is the mother of all f***-ups.
>
>
> Yup, such as your assumptions about the language OP's headers are written in
> and his control over them.

WTF are you talking about?

Re: number of elements by Eugene

Eugene
Wed May 11 16:24:18 CDT 2005

Victor Bazarov wrote:
>
> WTF are you talking about?

[It seems that the f word is something you just cannot avoid]

I am talikng about advice like

> And drop that C habit of typedefing and just define the damn struct:

Which assumes that the header is not shared with C and that OP controls it.

--
Eugene
http://www.gershnik.com





Re: number of elements by Victor

Victor
Wed May 11 16:35:47 CDT 2005

Eugene Gershnik wrote:
> Victor Bazarov wrote:
>
>>WTF are you talking about?
>
>
> [It seems that the f word is something you just cannot avoid]
>
> I am talikng about advice like
>
>
>>And drop that C habit of typedefing and just define the damn struct:
>
>
> Which assumes that the header is not shared with C and that OP controls it.

It doesn't assume. The header cannot be shared with C. If you just
paid attention to the post instead of my habits of using whatever words,
you'd see that the struct had a constructor declared in it, which AFAIK
is not a C feature (yet).

Re: number of elements by Eugene

Eugene
Wed May 11 16:44:41 CDT 2005

Victor Bazarov wrote:
>>> And drop that C habit of typedefing and just define the damn struct:
>>
>>
>> Which assumes that the header is not shared with C and that OP
>> controls it.
>
> It doesn't assume. The header cannot be shared with C. If you just
> paid attention to the post instead of my habits of using whatever
> words, you'd see that the struct had a constructor declared in it,
> which AFAIK is not a C feature (yet).

So? A single #ifdef converts it to a C friendly header plus you still assume
that OP controls it.

--
Eugene
http://www.gershnik.com



Re: number of elements by Victor

Victor
Wed May 11 17:06:42 CDT 2005

Eugene Gershnik wrote:
> Victor Bazarov wrote:
>
>>>>And drop that C habit of typedefing and just define the damn struct:
>>>
>>>
>>>Which assumes that the header is not shared with C and that OP
>>>controls it.
>>
>>It doesn't assume. The header cannot be shared with C. If you just
>>paid attention to the post instead of my habits of using whatever
>>words, you'd see that the struct had a constructor declared in it,
>>which AFAIK is not a C feature (yet).
>
>
> So? A single #ifdef converts it to a C friendly header plus you still assume
> that OP controls it.

You know, I do not see any point in continuing. You may consider yourself
the winner of this "argument". I have more important things to do than
play your games here.

Re: number of elements by Eugene

Eugene
Wed May 11 17:47:29 CDT 2005

Victor Bazarov wrote:
>
> You know, I do not see any point in continuing. You may consider
> yourself the winner of this "argument". I have more important things
> to do than play your games here.

If there is somebody who plays games here it is you. I commented on
something that is, in all probability, a bug in OPs code. You felt obliged
to exercise you language lawer's skills and when I explained my intention in
the most polite manner possible you found nothing better to do than to
insult me over my assumptions. Of course when it comes to your assumtions,
not to mention being rude to those who ask questions here there is no
questioning them.

So may I ask you something? Next time you decide to throw your insults
around just continue doing whatever important things you have to do and shut
up.

--
Eugene
http://www.gershnik.com



Re: number of elements by Jerry

Jerry
Thu May 12 01:30:47 CDT 2005

In article <VA.00000288.0033a60f@no-spam-cox.net>, lwaibel@no-spam-
cox.net says...
> I have a class, MyClass which contains:
>
> typedef struct MyStruct
> {
> MyType * pmyTypes[14];
>
> MyStruct()
> {
> pmyTypes = NULL;
> }
> } MyStruct;
>
> I want to at compile time create an array of the same number of elements.

If you insist on an array, here's one possibility:

template <typename T, std::size_t N>
char (&dummy(T(&)[N]))[N];

#define elements(a) (sizeof(dummy(a)))

int main() {
MyStruct s;

int items[elements(s.pmyTypes)];

This has both good and bad points. On one hand, it won't even compile
if you attempt to apply it to a pointer instead of an array. Applying
the sizeof trick to a pointer will typically compile but produce
incorrect results (this is particularly easy to do if you use it in a
function that's been passed an array that has decayed to a pointer).

OTOH, virtually nobody can understand that template, and you do need
to use s.pmyTypes above, NOT MyStruct::pmyTypes, even though it's
only looking at the type, not doing anything with the object. If you
do accidentally try to use the class::member syntax, the error
message probably won't be very helpful either. Finally, this will
break almost any older compiler (e.g. VC++ 6).

Personally I'd use a vector, in which case you can use something like
this instead:

template<class T, std::size_t N>
std::size_t elements(T (&)[N]) { return N; }

This can't be used as a compile-time constant, but otherwise it's
somewhat cleaner, at least IMO.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Re: number of elements by keepcomc

keepcomc
Thu May 12 01:47:00 CDT 2005

pmyTypes is defined as array of pointer not pointer.
Array name is used as const variable which can't be assigned by any value
including NULL.

if ...

MyTYpe (*pmyTypes)[14];

pmyTypes is pointer for array sized 14.
then u can assign NULL to it.

u confused between pointer for array and array pointer.



"Larry Waibel" <lwaibel@no-spam-cox.net> wrote in message
news:VA.00000288.0033a60f@no-spam-cox.net...
> I have a class, MyClass which contains:
>
> typedef struct MyStruct
> {
> MyType * pmyTypes[14];
>
> MyStruct()
> {
> pmyTypes = NULL;
> }
> } MyStruct;
>
> I want to at compile time create an array of the same number of elements.
> I've tried:
>
> sizeof(MyClass::MyStruct.pmyTypes) /
sizeof(MyClass::MyStruct.pmyTypes[0])
>
> but I get a compiler error:
>
> s:\mytest.h(155) : error C2143: syntax error : missing ')' before '.'
>
> and I notice that when typing the '.' I don't get the list of elements in
the
> structure, even though I have included the file with the class definition
> before I do this. Any idea where I'm going wrong this time? :-) Thanks!
>



Re: number of elements by Larry

Larry
Thu May 12 12:00:08 CDT 2005

Yep, that does it; thanks! Don't know if I'd have ever figured it out
without your help, I sure do appreciate it!

In article <uENIhOkVFHA.2984@tk2msftngp13.phx.gbl>, Carl Daniel [VC++ MVP]
wrote:
> Sorry, yes - I was in too much of a hurry.
>
> You have to write something like:
>
> sizeof((*(MyClass::MyStruct*)0).pmyTypes) /
> sizeof((*(MyClass::MyStruct*)0).pmyTypes[0])
>