• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: In-class initialization of integral static const variables & Linker errors
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: In-class initialization of integral static const variables & Linker errors


  • Subject: Re: In-class initialization of integral static const variables & Linker errors
  • From: Michael Crawford <email@hidden>
  • Date: Wed, 15 Jul 2009 11:35:06 -0700

I think initializing const variables in the class declaration, if it
is allowed by the ISO standard at all, is not supported by all
compilers.  I read about that in I think one of Scott Meyers'
Effective C++ books, but it might have been a different one.

You can be sure it would work if you don't initialize it in the
header, but provide the initialization in a .cpp file.

Mike

On Wed, Jul 15, 2009 at 8:16 AM, Guillaume Billard<email@hidden> wrote:
> Hello,
> Here is a main.cpp file:
> class Foo
> {
> public:
> static int const foo = 0;
> };
> class Bar
> {
> public:
> static int const bar = 0;
> };
> int main(int argc, char**)
> {
> return (argc == 1) ? Foo::foo : Bar::bar;
> }
> Trying to build this with Xcode 3.1.3 on Mac OS X 10.5.7, I get the
> following linker errors:
> Undefined symbols:
>   "Bar::bar", referenced from:
>       __ZN3Bar3barE$non_lazy_ptr in main.o
>   "Foo::foo", referenced from:
>       __ZN3Foo3fooE$non_lazy_ptr in main.o
> ld: symbol(s) not found
> Can anyone tell me what is wrong here?
> From what I understand, in-class initialization of integral static const
> variables is allowed in C++.
> It also appears that one "can take the address of a static member if (and
> only if) it has an out-of-class definition."
> http://www.research.att.com/~bs/bs_faq2.html#in-class
> I don't know the insides of the ternary operator, but I reckon I'm not doing
> that here.
> Thanks.
>
>
>  _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list      (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>



--
Michael David Crawford
mdcrawford at gmail dot com

   GoingWare's Bag of Programming Tricks
      http://www.goingware.com/tips/
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: In-class initialization of integral static const variables & Linker errors
      • From: Ethan Tira-Thompson <email@hidden>
    • Re: In-class initialization of integral static const variables & Linker errors
      • From: Markian Hlynka <email@hidden>
References: 
 >In-class initialization of integral static const variables & Linker errors (From: Guillaume Billard <email@hidden>)

  • Prev by Date: Fortify & Xcode
  • Next by Date: Re: GDB problems and version
  • Previous by thread: In-class initialization of integral static const variables & Linker errors
  • Next by thread: Re: In-class initialization of integral static const variables & Linker errors
  • Index(es):
    • Date
    • Thread