Thread: Enumerators vs Magic no.s

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Enumerators vs Magic no.s

    I understand that it is not is good idea to use magic no.s ..but just take a look at the situation..
    I need to describe a token..
    A
    Code:
    int type[5]
    would be sufficient for containing all the data..whether it is a number...or an operator....what type of operator...no of operands...etc etc....but it would scatter seemingly meaningless
    Code:
    if((x[0]==3)&&(x[2]!=0)) {;}
    ^ like statements through the whole code..
    So, is it a better Idea to use 4 or 5 different enumerators?..
    && Also ..is there a hidden difference in performance..?..although I know that enums are basically numbers...still there could be something..!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The solution is to use a struct instead of an array.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Sorry, I can't think of a good way to implement the struct...
    Could you show a little example..?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Do you not know what a struct is? It's a record type. You would learn the syntax for using struct and then make all these things into fields:

    a number...or an operator....what type of operator...no of operands
    Cprogramming.com Tutorial: Structures

    Further, that struct may or may not need to be part of a larger class type in your C++ program, so read

    Cprogramming.com Tutorial: Classes

    also.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ..I know very well what classes and structures are..but what difference does using them here make?
    Placing an object of
    Code:
    class Type
    {
         int n;
         int n_o;
         int pre;
         //&& some more ints..
    };
    inside the token class..
    brings the same problem with magic no.s...as with arrays....only somewhat better in specifying where to get what data...but the data is still in numbers...
    Is there a better way to use them here?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No, now the data is in names. Instead of talking about foo[2], which means who knows what, now you have foo.number_of_operands and everybody is a lot happier to see that sort of thing in code.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ...consider that each mathematical function would still have a corresponding number(say 'foo.f_no') ...Is there a way to put names to those...?..and call the functions directly from the names?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why would each mathematical function have a corresponding number? Why wouldn't it have a name instead? (EDIT: And if we're saying "call the functions directly from the names", then maybe we're storing function objects.)
    Last edited by tabstop; 05-24-2011 at 12:08 AM.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ...I didn't think of that... I could easily put a std::string inside the Type class to denote the name of the functions...and '==' them when required..!

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    we're storing function objects
    Please elaborate...I tried to implement it in that way(some months ago)..and after that 'pair classes'...but was totally pwned by the complexity .

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, I had thought that there was an overarching class above unary_function and binary_function in the <functional> header, but I guess not. Still you can run with those, especially if you have a majority of one or the other.

    Either way, you can always roll your own with something like
    Code:
    struct Functor {
        int no_of_arguments;
    };
    and then you inherit dozens of subclasses off of this one, each one with its own operator().

    If you're still doing arithmetic from the other thread, then one advantage of the <functional> struct is that plus, multiply, etc., are already there for the taking.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For dynamic function objects, you can use std::function instead of having to derive from your own class.
    Another option is to simply use a template to hold an arbitrary type which can accept the () syntax, which basically means it's a function, member function or a class/struct with an overloaded () operator. But this is trickier.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enumerators as strings
    By Mario F. in forum C++ Programming
    Replies: 14
    Last Post: 07-19-2006, 01:26 PM
  2. How to use enumerators to store input
    By Rubiks14 in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2005, 09:43 PM
  3. the magic of MAGIC numbers
    By borko_b in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-27-2002, 02:36 AM
  4. darn enumerators...please help
    By Waldo2k2 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-08-2002, 07:11 PM
  5. needs lots of help with arrays,pointers, and enumerators
    By ssjnamek in forum C++ Programming
    Replies: 13
    Last Post: 11-30-2001, 05:18 PM