struct with const member variable without explicit constructor

  • Follow


Hi.

Is the following code c++:

   typedef struct {
       int a;
       int const b;
   } C;

The Intel compiler gives the following warning message:
warning #411: class "C" defines no constructor to initialize the
following:

             const member "C::b"

But it is possible to initialize b with the old C-style:

C c = { 1, 2 };


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply chb 2/2/2011 7:39:18 AM

On 2011-02-02 14:39, chb wrote:
> 
> Is the following code c++:
> 
>    typedef struct {
>        int a;
>        int const b;
>    } C;

Yes, this is well-formed C++.

> The Intel compiler gives the following warning message:
> warning #411: class "C" defines no constructor to initialize the
> following:
> 
>              const member "C::b"
> 
> But it is possible to initialize b with the old C-style:
> 
> C c = { 1, 2 };

Your example is reasonable but note that compilers are free to
diagnose anything - even well-formed and well-defined code.

HTH & Greetings from Bremen,

Daniel Kr�gler


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply ISO 2/2/2011 4:12:20 PM


On 02/02/2011 13:39, chb wrote:
> 
> Hi.
> 
> Is the following code c++:
> 
>    typedef struct {
>        int a;
>        int const b;
>    } C;
> 
> The Intel compiler gives the following warning message:
> warning #411: class "C" defines no constructor to initialize the
> following:
> 
>              const member "C::b"
> 
> But it is possible to initialize b with the old C-style:
> 
> C c = { 1, 2 };
> 
> 
Which is why it is a warning and not an error.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Francis 2/2/2011 4:13:24 PM

2 Replies
705 Views

(page loaded in 0.082 seconds)

Similiar Articles:













7/23/2012 2:59:44 PM


Reply: