Enumerators vs Magic no.s

This is a discussion on Enumerators vs Magic no.s within the C++ Programming forums, part of the General Programming Boards category; I understand that it is not is good idea to use magic no.s ..but just take a look at the ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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..!
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Sorry, I can't think of a good way to implement the struct...
    Could you show a little example..?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    ..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?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    ...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?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    ...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..!
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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, 04:18 PM

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