public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* what about enum class ?
@ 2013-12-04 14:52 Graziano Servizi
  2013-12-04 16:18 ` Andrew Bell
  2013-12-06 15:51 ` Jonathan Wakely
  0 siblings, 2 replies; 3+ messages in thread
From: Graziano Servizi @ 2013-12-04 14:52 UTC (permalink / raw)
  To: gcc-help

Dear Jonathan, Dietmar or anybody else,

I'm doing some exercises and I tried enum class as a type embedded in a 
class scope which is the ancestor of a two-level hierarchy.

My aim was to use bool as underlying type, and this was accepted by the 
compiler...

Nevertheless, transmitting a value for a member variable of the enum 
class type through the chain of constructors, I found it finally changed
without any evident reason, even if it had the value I expected during 
its whole history.

There should be something wrong in my code I can't find, and your help 
would be highly appreciated.


Here follows the code: it is, I hope, short enough to be easily 
debugged. As you can see main declares an object of class Third and
thereafter ask for the value returned by its giveMe public method.

The Third's ctor sends a value up to the constructor of the ancestor
class, which uses it to initialize a member variable, which, in my
opinion should be inherited by Third.

But the value written is the opposite I expected...Why?

///////// code
# include <iostream>
using namespace std;

class First
  {public:
   enum class Alpha : bool {A=false, B=true};
   protected:
   Alpha alpha;
   First(Alpha b)
    {alpha = b;
     clog << "First's ctor set " << static_cast<bool>(alpha) << endl;}
   };

class Second : First
  {
   protected:
   Alpha First :: alpha; // to reset from private
   public:
   First :: Alpha; // to reset Alpha type as public
   Second(Alpha b) : First(b)
     {
      clog << "Second's ctor handles " << static_cast<bool>(b) << endl;
      }
   };

class Third : Second
  {
   public:
   Alpha giveMe() // Alpha is recognized without scope resolution (by 
inheritance...)
    {// alpha should be inherithed from First, through Second...or NOT?
     clog << "Second :: giveMe returns " << static_cast<bool>(alpha) << 
endl;
     return alpha;}
   Third(Alpha b = Alpha :: A) : Second(b)
    {
     clog << "Third's ctor handles " << static_cast<bool>(b) << endl;
     }
   };

int main( )
  {
   Third aaa;
   clog << "in main one finds " << static_cast<bool>(aaa.giveMe()) << '\n';
   }
////////// end code


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

* Re: what about enum class ?
  2013-12-04 14:52 what about enum class ? Graziano Servizi
@ 2013-12-04 16:18 ` Andrew Bell
  2013-12-06 15:51 ` Jonathan Wakely
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Bell @ 2013-12-04 16:18 UTC (permalink / raw)
  To: gcc-help

This isn't a gcc question.  It's a c++ question.

Your code can be fixed with a using declaration to change the
protection of the enum "Alpha" and member "alpha".

On Wed, Dec 4, 2013 at 8:51 AM, Graziano Servizi
<Graziano.Servizi@bo.infn.it> wrote:
> Dear Jonathan, Dietmar or anybody else,
>
> I'm doing some exercises and I tried enum class as a type embedded in a
> class scope which is the ancestor of a two-level hierarchy.
>
> My aim was to use bool as underlying type, and this was accepted by the
> compiler...
>
> Nevertheless, transmitting a value for a member variable of the enum class
> type through the chain of constructors, I found it finally changed
> without any evident reason, even if it had the value I expected during its
> whole history.
>
> There should be something wrong in my code I can't find, and your help would
> be highly appreciated.
>
>
> Here follows the code: it is, I hope, short enough to be easily debugged. As
> you can see main declares an object of class Third and
> thereafter ask for the value returned by its giveMe public method.
>
> The Third's ctor sends a value up to the constructor of the ancestor
> class, which uses it to initialize a member variable, which, in my
> opinion should be inherited by Third.
>
> But the value written is the opposite I expected...Why?
>
> ///////// code
> # include <iostream>
> using namespace std;
>
> class First
>  {public:
>   enum class Alpha : bool {A=false, B=true};
>   protected:
>   Alpha alpha;
>   First(Alpha b)
>    {alpha = b;
>     clog << "First's ctor set " << static_cast<bool>(alpha) << endl;}
>   };
>
> class Second : First
>  {
>   protected:
>   Alpha First :: alpha; // to reset from private
>   public:
>   First :: Alpha; // to reset Alpha type as public
>   Second(Alpha b) : First(b)
>     {
>      clog << "Second's ctor handles " << static_cast<bool>(b) << endl;
>      }
>   };
>
> class Third : Second
>  {
>   public:
>   Alpha giveMe() // Alpha is recognized without scope resolution (by
> inheritance...)
>    {// alpha should be inherithed from First, through Second...or NOT?
>     clog << "Second :: giveMe returns " << static_cast<bool>(alpha) << endl;
>     return alpha;}
>   Third(Alpha b = Alpha :: A) : Second(b)
>    {
>     clog << "Third's ctor handles " << static_cast<bool>(b) << endl;
>     }
>   };
>
> int main( )
>  {
>   Third aaa;
>   clog << "in main one finds " << static_cast<bool>(aaa.giveMe()) << '\n';
>   }
> ////////// end code
>
>



-- 
Andrew Bell
andrew.bell.ia@gmail.com

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

* Re: what about enum class ?
  2013-12-04 14:52 what about enum class ? Graziano Servizi
  2013-12-04 16:18 ` Andrew Bell
@ 2013-12-06 15:51 ` Jonathan Wakely
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2013-12-06 15:51 UTC (permalink / raw)
  To: Graziano Servizi; +Cc: gcc-help

On 4 December 2013 14:51, Graziano Servizi wrote:
>
> ///////// code
> # include <iostream>
> using namespace std;
>
> class First
> {public:
> enum class Alpha : bool {A=false, B=true};
> protected:
> Alpha alpha;
> First(Alpha b)
> {alpha = b;
> clog << "First's ctor set " << static_cast<bool>(alpha) << endl;}
> };
>
> class Second : First
> {
> protected:
> Alpha First :: alpha; // to reset from private

(In the class above First::alpha is public, not private.)

This syntax is no longer part of C++, you should do it like this instead:

using First::alpha;

> public:
> First :: Alpha; // to reset Alpha type as public

And like this instead:

using First::Alpha;

> Second(Alpha b) : First(b)
> {
> clog << "Second's ctor handles " << static_cast<bool>(b) << endl;
> }
> };
>
> class Third : Second
> {
> public:
> Alpha giveMe() // Alpha is recognized without scope resolution (by
> inheritance...)
> {// alpha should be inherithed from First, through Second...or NOT?
> clog << "Second :: giveMe returns " << static_cast<bool>(alpha) << endl;
> return alpha;}
> Third(Alpha b = Alpha :: A) : Second(b)
> {
> clog << "Third's ctor handles " << static_cast<bool>(b) << endl;
> }
> };
>
> int main( )
> {
> Third aaa;
> clog << "in main one finds " << static_cast<bool>(aaa.giveMe()) << '\n';
> }
> ////////// end code
>

I think what happens is G++ incorrectly creates a new member
Second::alpha, which is not the same as First::alpha, so giveMe
returns an uninitialized variable.

You should stop using access declarations and your code will work.

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

end of thread, other threads:[~2013-12-06 15:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-04 14:52 what about enum class ? Graziano Servizi
2013-12-04 16:18 ` Andrew Bell
2013-12-06 15:51 ` Jonathan Wakely

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).