Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 12 Apr 2001 17:38:33 -0500
From:      Lucas Bergman <lucas@slb.to>
To:        Ying-Chieh Liao <ijliao@csie.nctu.edu.tw>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: OT: cast int to enum in C++
Message-ID:  <20010412173833.B7929@billygoat.slb.to>
In-Reply-To: <20010413015404.A30645@terry.dragon2.net>; from ijliao@csie.nctu.edu.tw on Fri, Apr 13, 2001 at 01:54:04AM %2B0800
References:  <20010413015404.A30645@terry.dragon2.net>

next in thread | previous in thread | raw e-mail | index | archive | help
Since your post is off-topic, please prefix the subject with "OT:".

> In C, compiling following code is all ok but in C++, there's some
> err msgs :
> 
> test.cpp:12: conversion from `int' to `enum test'
> 
> I've tried g++ -fenum-int-equiv test.cpp, but it said that this flag
> is no longer supported :< what can I do ? dont suggest me to change
> tmp(1) to tmp(TEST1) ...
> 
> typedef enum { TEST1=1, TEST2=2, TEST3=3 } test;
> void tmp(test argv) { return; }
> int main(void) { tmp(1); }

It's part of C++'s type safety.  "int" doesn't have to implicitly
convert to "enum anything" to help save the programmer from
him/herself.

First, since tmp() has declared its argument of type "test," what
you're doing by calling tmp(1) is (probably) annoying and
wrong-headed; it looks like it defeats the purpose of declaring an
enum in the first place.  However, if you insist in this course,
changing tmp(1) to tmp((test) 1) works.  (Oh, and make main() return
an "int" for heaven's sake.)

Lucas

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010412173833.B7929>