Thread: How to pass enum values

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing values from Enum
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 02-16-2009, 10:28 PM
  2. pass all the array as blob object in one field
    By Leite33 in forum Windows Programming
    Replies: 0
    Last Post: 11-12-2006, 01:27 PM
  3. extern with enum
    By penney in forum C Programming
    Replies: 4
    Last Post: 10-22-2003, 12:38 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM