-
How to pass enum values
Hi !
I have a problem in passing enum values.
I have a class B.cpp as follow:
[code]
enum book{
novel,
digest,
magzine
};
// And a function which receives this values:
void details(int price,enumtype);
}
[code]
This function is called from another A.cpp as follow:
Code:
B *b;
b->details(123,enumType);
What should I write in "enumtype" so that It correctly pass and called function I can check values directly as :
[code]
if(novel)
{
//Do Something
}
if(digest)
{
//Do Something
}
etc
[code]
Thanks
-
I would suggest something along the lines of:
Code:
enum BookType {
novel,
digest,
magazine
};
// ...
void B::details(int price, BookType bookType)
{
switch (bookType)
{
case novel:
// do something
break;
case digest:
// do something
break;
case magazine:
// do something
break;
}
}
Then you would use it as:
Code:
B *b;
b->details(123, novel);
However, it may be the case that instead of using an enum, you might want a class hierarchy instead, in which case the B object would have a Book (smart) pointer, and forward the work of its details() member function to the appropriate virtual function of the Novel, Digest, Magazine, etc, child class of Book. This design would be more extensible then using a switch based on an enum, but may also be overkill.