473,382 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Is it possibleto configure enumator constants in C++? (hard problem)

Suppose to declare a enum type:

enum ImageType
{
Binary,
ByteGray,
ShortGray,
...
}

Is it possible to get the enum constant name like "Binary" from a
configuration file so that
I can change it later?

Or can we solve it using other way instead of enum?

Thanks.

Weiming
Jul 19 '05 #1
3 1638
Weiming Liu wrote:
Suppose to declare a enum type:

enum ImageType
{
Binary,
ByteGray,
ShortGray,
...
}

Is it possible to get the enum constant name like "Binary" from a
configuration file so that I can change it later?
Nope
Or can we solve it using other way instead of enum?


Yep. A class with member functions returning the values. But please
remember: those values will not be compile time constants anymore, so they
cannot be case labels etc.

--
WW aka Attila
Jul 19 '05 #2
In article <bk***********@acs2.byu.edu>, wl**@email.byu.edu says...
Suppose to declare a enum type:

enum ImageType
{
Binary,
ByteGray,
ShortGray,
...
}

Is it possible to get the enum constant name like "Binary" from a
configuration file so that
I can change it later?
Yes and no -- it's trivial to invent a file format something like:

Binary = 1
ByteGray = 2
ShortGray = 3

and write a bit of code that can read this and know that "Binary" has
the value 1 afterwards.

The problem is with what you can do with it afterwards -- or more
accurately, with what you CAN'T do with it. You can't use it as a
constant afterwards, so code like:

switch(image_type) {
case Binary:

Because as far as the program cares, Binary is a string that was read in
from a file, and the compiler doesn't know anything about the fact that
you're trying to use it as a constant with the value 1.

OTOH, there are alternatives to that -- for example, you can use an
std::map to associate the value with something like a pointer to an
object or a function, and when you read that value, you can look it up
in the map and find (and execute) what you've associated with the value.
Or can we solve it using other way instead of enum?


The question is what exact problem you're trying to solve.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #3
"Weiming Liu" <wl**@email.byu.edu> wrote in message news:<bk***********@acs2.byu.edu>...
Suppose to declare a enum type:

enum ImageType
{
Binary,
ByteGray,
ShortGray,
...
}

Is it possible to get the enum constant name like "Binary" from a
configuration file so that
I can change it later?

Or can we solve it using other way instead of enum?

Thanks.

Weiming


If i understood what you wanting to do, i would:

<code>
#include <iostream>
#include <string>

enum ImageType
{
Unknown,
Binary,
ByteGray,
ShortGray
};

typedef struct {
const char *image_type_text;
ImageType image_type;
} ImageTypeTable_t;

static ImageTypeTable_t ImageTypeTable[] = {
{ "Binary", Binary },
{ "ByteGray", ByteGray },
{ "ShortGray", ShortGray },
{ 0, Unknown },
};

ImageType ImageTypeFromText(std::string image_type_text) {
int i;

for (i=0; ImageTypeTable[i].image_type_text != 0; i++) {
if (ImageTypeTable[i].image_type_text == image_type_text)
return ImageTypeTable[i].image_type;
}

return Unknown;
}

int main(void)
{
std::cout << ImageTypeFromText ("Binary") << std::endl;
std::cout << ImageTypeFromText ("ByteGray") << std::endl;
std::cout << ImageTypeFromText ("ShortGray") << std::endl;
std::cout << ImageTypeFromText ("XyzXyz123") << std::endl;

return 0;
}
</code>

Diego Giagio
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Chris Gonnerman | last post by:
A while back (a long while actually) I was a participant in a long argument over decimal and rational numbers, and their inclusion into the core of Python. Last night in a dream I came up with...
34
by: E. Robert Tisdale | last post by:
Please find attached the physical constants header file physical.h It defines conversion factors to mks units. It might be used like this: > cat main.cc #include<iostream>...
1
by: Carl Johansen | last post by:
I'm calling a SQL Server statement and I know that it might sometimes return a "violation of unique key constraint" error (SQL Server error 2627). I'd like to catch this and handle it - something...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
6
by: kobu.selva | last post by:
I was recently part of a little debate on the issue of whether constants and string literals are considered "data objects" in C. I'm more confused now than before. I was always under the...
1
by: 2obvious | last post by:
I want to declare some constants on the application level in global.asax to use throughout my application, e.g.: Sub Application_OnStart() Const NUM As Integer = 5 End Sub Problem is, when I...
7
by: Hal Vaughan | last post by:
I have a problem with port forwarding and I have been working on it for over 2 weeks with no luck. I have found C programs that almost work and Java programs that almost work, but nothing that...
27
by: Matt | last post by:
Does the language have header files containing definitions of numbers such as pi and e? I believe some implementations used to have those, but I can't find them in Stroustrup. Thanks.
2
by: Leslie Sanford | last post by:
I want to define a set of floating point constants using templates insteand of macros. I'd like to determine whether these constants are floats or doubles at compile time. In my header file, I have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.