Topic: If it isn't, it should be


Author: "Gillmer J. Derge" <spam@gillmerderge.com>
Date: 2000/07/26
Raw View
"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:8ljaas$33u$1@slb1.atl.mindspring.net...
> This works:
>
> char upr_text[sizeof(A().text)];

That works if, as is the case here, A is default constructible.  Here's
another alternative that I believe will work even if A has more complicated
constructors.  Since this is only an argument to a sizeof expression, it
shouldn't give you problems with dereferencing a null pointer.

  char upr_text[sizeof(static_cast<A*>(0)->text)];

-----
Gillmer J. Derge


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: wmm@fastdial.net
Date: 2000/07/26
Raw View
In article <8lcr17$1iqb$1@news.hal-pc.org>,
  "Greg Brewer" <nospam.greg@brewer.net> wrote:
> Given the following,
>
> class A
> {
> public:
>    char text[64];
> };
>
> shouldn't the following work?
>
> class AA : public A
> {
> public:
>    char upr_text[sizeof(text)];
> };

The rationale for the error is that you can't refer to a
non-static data member of a class without an object expression
except when forming a pointer to member.

That rationale doesn't make a lot of sense in the context of a
sizeof() expression, and there's an open issue on the Core
Language issues list (see
http://www.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#198)
that deals with similar questions in the context of local and
nested classes.  I'll make sure that inheritance, as in your
example, is also added to the discussion.

--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Mike Wahler" <mkwahler@mkwahler.net>
Date: 2000/07/26
Raw View
Greg Brewer <nospam.greg@brewer.net> wrote in message
news:8lcr17$1iqb$1@news.hal-pc.org...
> Given the following,
>
> class A
> {
> public:
>    char text[64];
> };
>
> shouldn't the following work?
>
> class AA : public A
> {
> public:
>    char upr_text[sizeof(text)];
> };
>
> My compiler gives me an error.

Me too. :-)

'AA::A::text' : member from enclosing class is not a type name, static, or
enumerator

The class hasn't been instantiated, so there's no 'text' to get the size of.
This works:

char upr_text[sizeof(A().text)];


-Mike




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Greg Brewer" <nospam.greg@brewer.net>
Date: 2000/07/25
Raw View
Given the following,

class A
{
public:
   char text[64];
};

shouldn't the following work?

class AA : public A
{
public:
   char upr_text[sizeof(text)];
};

My compiler gives me an error.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]