Operator class?

This is a discussion on Operator class? within the C++ Programming forums, part of the General Programming Boards category; Is it possible to make a class for operators? IE: Code: Operator A = +; int B, C; B = ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Operator class?

    Is it possible to make a class for operators? IE:
    Code:
    Operator A = +;
    int B, C;
    
    B = (B A C);
    The only reason I want to do this, is to avoid having long if statements that do the same thing only a slight change to operators
    Code:
    Operator A = +;
    int B, C;
    
    if(B > 0)
     a = -;
    
     B = (B A C);
    As opposed to:
    Code:
    int B, C:
    
    if(B > 0)
     B = B - C;
    if(B <= 0)
     B = B + C;
    Now, thats pritty simple code that I wouldent mind doing. They are both 5 lines, the same amount of work. But, when I want something like:

    Code:
    int A, B, C;
    
    if(A < 0 && B > 0) {
     A += 1;
     A = (B + C) / A;
     B += 1;
     B = (C - A) / B;
     C = (C << 2) / A; 
    }
    
    if(A > C && B > C) {
     A += 1;
     A = (B - C) / A;
     B += 1;
     B = (C - A) / B;
     C = (C << 2) / A;
    }
    Instead of making a whole new IF and make the same code twice, it would be ten times easier to do:

    Code:
    Operator O = -;
    int A, B, C;
    
    if(A < 0 && B > 0)
     O = +;
    
     A += 1;
     A = (B O C) / A;
     B += 1;
     B = (C - A) / B;
     C = (C << 2) / A;
    Basicaly, I'm just trying to cut down on uncessisary lines of code. Though, I realize (B O C) would be very ugly. (Would seem more like you wanted to multiply the three) But, is there some way to do this? Even if its some hackish define? (#define M (A, B) (A < 0) + (A > 0) -) I know thats not a real define (Never made a define macro)

    Well, if its possible by some means, it'd be great.

    Thanks!
    Last edited by Blackroot; 09-14-2006 at 11:37 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    No, that's not possible. I really see little logic in it other than maybe saving you a few lines of code overall... is that really worth making rediculous looking code? ... and really... can't you write better examples than that? If you're so interested in saving lines of code, there is no reason that this:
    Code:
    int A, B, C;  // Assume some initialization.
    
    if(A < 0 && B > 0) {
     A += 1;
     A = (B + C) / A;
     B += 1;
     B = (C - A) / B;
     C = (C << 2) / A; 
    }
    
    else {  /* I made this else because that's really what you alternative says. Not another if */
     A += 1;
     A = (B - C) / A;
     B += 1;
     B = (C - A) / B;
     C = (C << 2) / A;
    }
    Can't be this:
    Code:
    int A, B, C; // Assume some initialization.
    
    A += 1;
    
    A = ((A < 0 && B > 0) ? (B + C) : (B - C)) / A; 
    /* A = (B [+/-] C) / A   --   [depending on the condition] */
    
    B += 1;
    
    B = (C - A) / B;
    C = (C << 2) / A;
    Last edited by SlyMaelstrom; 09-15-2006 at 12:05 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,162
    Assuming all you're worried about is adding in some cases and subtracting in others, the code
    Code:
    int B, C:
    
    if(B > 0)
     B = B - C;
    if(B <= 0)
     B = B + C;
    could be simplified to;
    Code:
    int B, C;
    int multiplier = (B > 0) ? -1: 1;
    
    B += multiplier*C;
    I saw someone do something like you suggest with some macro hackery a few years back. He was actually trying to come up with an entry for an obfuscated C contest IIRC. I certainly wouldn't care to maintain such code.

  4. #4
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Hmm, that integer is pritty much what I needed. I'm wondering if this would work.

    Code:
    #define GZeroAdd (A) ( (A > 0) ? -1 : 1 )
    #define GZeroMult(A) ((A > 0) ? A : 1)
    
    //...
    
    int A, B;
    
    B-=(GZeroAdd(A)A);
    B*=(GZeroMult(B));
    Its very ugly, but its not an unholy solution (Though quite annoying, yes).

    Sly, your option is good normaly, but it wouldent work. You'd be deviding by zero when you initialized A. Unless its possible to do?:?
    Code:
    int A = -1;
    int B = 1;
    A = ((A < 0 && B > 0) ? ((B + C) / -A) : ((B - C) / -A));
    Still, both solutions are not pritty. The reason I said several if's is becuase in my actual program that occures. (Happens to be collision detection, I have wayyy to many if's doing exactly the same thing with one or two differences in the multipliers. IE [+/-] height then [+/-]width, [+/-] MovementIncrement six if's that could be done in a single one or two. (Zero with either solution.))

    Well, does anyone have some other solution, or recommend either of theyse? -,-.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote Originally Posted by Blackroot
    Sly, your option is good normaly, but it wouldent work. You'd be deviding by zero when you initialized A.
    There is no check against dividing by zero in your example... I assumed you understood it would have to be there and left it out as I did.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

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