a trick utterly !

This is a discussion on a trick utterly ! within the C++ Programming forums, part of the General Programming Boards category; please look at this line below carefully for half a minute: emun Animal{dog, cat, bird, fish, crocodile}; see it ? ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Exclamation a trick utterly !

    please look at this line below carefully for half a minute:

    emun Animal{dog, cat, bird, fish, crocodile};

    see it ? OK, when we need some varaibles of Animal we could declare it like this:
    PHP Code:
    Animal myPet;
    myPet=dog
    and the output of myPet should be a zero. what should it do Though it couldnt be output directly ? Maybe string or Array is much more fit for this occasion. any ideas ?
    Never end on learning~

  2. #2
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    Code:
    emun Animal{dog, cat, bird, fish, crocodile}; 
    char *AnimalStr[] = {"dog","cat","bird","fish","crocodile"};
    Animal myPet;
    myPet=dog;
    cout << AnimalStr[myPet];

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by quagsire
    Code:
    emun Animal{dog, cat, bird, fish, crocodile}; 
    char *AnimalStr[] = {"dog","cat","bird","fish","crocodile"};
    Animal myPet;
    myPet=dog;
    cout << AnimalStr[myPet];
    since that, why not use string directly ? it should be more convenient.
    Last edited by black; 06-30-2002 at 11:37 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Maybe not. Seems to me that comparing simple integers is faster than doing a strcmp every time you need the contents of myPet. Another pro for enumerations is that it's never invalid. When you work with strings you might accidentally type "Dig" instead of dog, the compiler won't warn you and your program won't be able to handle it. It's the same like that with config files that contain a keyset. The keys are all numbered, but a setup program will need to display humanly readable names, so you use an array of strings to go along with it.
    Last edited by Boksha; 07-01-2002 at 02:52 AM.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Boksha
    Maybe not. Seems to me that comparing simple integers is faster than doing a strcmp every time you need the contents of myPet. Another pro for enumerations is that it's never invalid. When you work with strings you might accidentally type "Dig" instead of dog, the compiler won't warn you and your program won't be able to handle it. It's the same like that with config files that contain a keyset. The keys are all numbered, but a setup program will need to display humanly readable names, so you use an array of strings to go along with it.
    array could also do, check it below please:


    PHP Code:
    #include <iostream>
    #include <string>

    void main()
    {
      
    int i;  // the number stand for different elements.
      
    string Animal[5]={"dog""cat""bird""fish""crocodile"}; 
      for(
    i=0;i<5;i++)
      {
        
    cout << Animal[i] << endl;
      }
      
    cin >> "what";

    Never end on learning~

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    but having a set of enums with it could be usefull in switch case statements


    instead of

    Code:
    switch(type)
    {
        case 0:
            dog
        case 1:
            cat
    }
    this

    Code:
    switch(type)
    {
        case Dog:
            dog
        case Cat:
            cat
    }
    or for random access

    Animal[Dog]

    is infinitly more explanitory that

    Animal[0]

    NOTE: this does not mean you must use the enums in loops and such

    it doesn't require the reader to be 100% familiar with every aspect of the code to read it.

    its just my opinion though...
    Last edited by no-one; 07-01-2002 at 02:04 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  7. #7
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Wink

    Originally posted by no-one
    but having a set of enums with it could be usefull in switch case statements...
    woh, that's it ! I know just a little about switch so ... It seems I should read something on it, thanx, guys~
    Never end on learning~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The reversed-comparison trick: an argument against
    By brewbuck in forum Tech Board
    Replies: 8
    Last Post: 02-24-2009, 08:25 PM
  2. Preprocessor trick for variable number of args
    By Jaken Veina in forum C Programming
    Replies: 3
    Last Post: 11-04-2008, 09:24 AM
  3. No! Another way to trick us men!!
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-18-2004, 09:47 PM
  4. I, Robot -- It's a trick, and I'm mad.
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 07-15-2004, 01:57 PM
  5. The Meaning of Life: A Trick Question?
    By chix/w/guns in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2004, 07:53 PM

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