How to pass enum values

This is a discussion on How to pass enum values within the C++ Programming forums, part of the General Programming Boards category; Hi ! I have a problem in passing enum values. I have a class B.cpp as follow: [code] enum book{ ...

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

    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
    21,739
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09: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, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21