public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Static constant class members
@ 2004-02-28 18:41 Vova
  2004-02-28 21:23 ` Eljay Love-Jensen
  0 siblings, 1 reply; 5+ messages in thread
From: Vova @ 2004-02-28 18:41 UTC (permalink / raw)
  To: gcc-help

Hello all !
I have problem with this construction:

class TxtVideo {
  protected:
	static unsigned int* const VBASE = (unsigned int*) 0x0c00b8000L;

When compiling it I'm getting error message:
error: invalid in-class initialization of
   static data member of non-integral type `unsigned int* const'

I want to define this constant in header file becouse compiler must be able to 
insert it as imidiatly operand, like #define constants.
I can define it as integral type such as int, but thet I must always use 
reinterpret_cast<unsigned int*>. This is bad solution.

I discover, that this error appear only in g++ >= 3.0, in 2.9* it was all 
right.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Static constant class members
  2004-02-28 18:41 Static constant class members Vova
@ 2004-02-28 21:23 ` Eljay Love-Jensen
  2004-02-29 16:49   ` Vova
  0 siblings, 1 reply; 5+ messages in thread
From: Eljay Love-Jensen @ 2004-02-28 21:23 UTC (permalink / raw)
  To: Vova, gcc-help

Hi Vova,

You cannot initialize static data members in-class.  (You can in Java, not 
in C++.)

You need to do this:

--- TxtVideo.h ---
class TxtVideo {
   protected:
	static unsigned int* const VBASE;

--- TxtVideo.cpp ---
unsigned int* const TxtVideo::VBASE = (unsigned int*) 0x0c00b8000L;

HTH,
--Eljahy

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Static constant class members
  2004-02-28 21:23 ` Eljay Love-Jensen
@ 2004-02-29 16:49   ` Vova
  2004-02-29 17:13     ` Eljay Love-Jensen
  0 siblings, 1 reply; 5+ messages in thread
From: Vova @ 2004-02-29 16:49 UTC (permalink / raw)
  To: gcc-help

On Saturday 28 February 2004 22:13, Eljay Love-Jensen wrote:
> You cannot initialize static data members in-class.  (You can in Java, not
> in C++.)
This construction works fine in gcc 3.3

 class TxtVideo {
    protected:
      static const int test = 10;

>
> You need to do this:
>
> --- TxtVideo.h ---
> class TxtVideo {
>    protected:
> 	static unsigned int* const VBASE;
>
> --- TxtVideo.cpp ---
> unsigned int* const TxtVideo::VBASE = (unsigned int*) 0x0c00b8000L;
This is true, but actual value of the constant wouldn't be avaible in files 
which includes "TxtVideo.h", so compiler wouldn't insert it as imidiate 
operand, instead it will insert memory reference. Im I wrong ?

> HTH,
> --Eljahy

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Static constant class members
  2004-02-29 16:49   ` Vova
@ 2004-02-29 17:13     ` Eljay Love-Jensen
  2004-02-29 20:39       ` Vova
  0 siblings, 1 reply; 5+ messages in thread
From: Eljay Love-Jensen @ 2004-02-29 17:13 UTC (permalink / raw)
  To: Vova, gcc-help

Hi Vova,

 >This construction works fine in gcc 3.3 ...

A "static int const" is a special exemption.

A pointer (even a static const one)  is not exempted.

 >This is true, but actual value of the constant wouldn't be available in 
files which includes "TxtVideo.h", so compiler wouldn't insert it as 
immediate operand, instead it will insert memory reference. Am I wrong ?

You are correct.  If you really want it to be an immediate operand in C++, 
do this:

class TxtVideo
{
public:
   static unsigned int* const getVBase() { return (unsigned int* 
const)0x0C00B8000L; }
...

That will be inserted as an inline function (since it's body is defined in 
the class), and the inline will optimize down to an immediate.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Static constant class members
  2004-02-29 17:13     ` Eljay Love-Jensen
@ 2004-02-29 20:39       ` Vova
  0 siblings, 0 replies; 5+ messages in thread
From: Vova @ 2004-02-29 20:39 UTC (permalink / raw)
  To: gcc-help

Hello !

On Sunday 29 February 2004 19:49, Eljay Love-Jensen wrote:
> You are correct.  If you really want it to be an immediate operand in C++,
> do this:
>
> class TxtVideo
> {
> public:
>    static unsigned int* const getVBase() { return (unsigned int*
> const)0x0C00B8000L; }
> ...
>
> That will be inserted as an inline function (since it's body is defined in
> the class), and the inline will optimize down to an immediate.
>
Thanks ! I'll try it.

-- 
          Vova

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-02-29 18:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-28 18:41 Static constant class members Vova
2004-02-28 21:23 ` Eljay Love-Jensen
2004-02-29 16:49   ` Vova
2004-02-29 17:13     ` Eljay Love-Jensen
2004-02-29 20:39       ` Vova

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).