Thread: #define question

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    #define question

    I wonder if I have something like this where Replace is the same as/"contains":
    Number == 5.

    My question is if it is possible to put this: Number == 5 to a std::string in any way through Replace ?


    Code:
     
    #define Replace Number == 5

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So you want to do something like
    Code:
    if (Replace) dosomething;
    and you actually want that to be
    Code:
    if(Number == 5) dosomething;
    Yes, you can do that - I wouldn't exactly recommend it, and it may cause all sorts of interesting problems when you mix it with other things if you're not careful.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes. I beleive I am trying to put to a std::string so I tried something like this.
    I think ´Word´ is containing "Number == 5" as a std::string now ?
    Thanks...
    Code:
    #define ReplaceS "Number == 5"
    
    std::string Word;
    Word = ReplaceS;

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't do this. It's equal to creating your own "language within the language" or "language syntax". People who read the code will be confused.
    This is clearly abusing define.
    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.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    "abusing define"? Sounds dramatic, like it might end all life as we know it.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I am trying out to do it through define because I cant see any other solution for what I have to do. I am doing it with causion and a lot of testing and no one except me will read the code so that will be no problem anyway.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just because you can get away with it doesn't mean it's right. I dare say 100% of things can be done without macros of this type. I have never, ever needed such a macro during my programming career.
    I suggest you explain what you're trying to do and show some code and people can suggest alternatives.
    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.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What is it that you actually want to achieve?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I agree that it's not a particularly good thing to do. What are you ACTUALLY trying to do, perhaps if you explain the bigger picture we can come up with something that works better?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Thanks, I will try to explain what I am doing.

    I have a Form application where I wrote this:

    Code:
    #include "Item1.h"
    int Number = 5;
    
    
    if( Criteria )
    { 
        int i = 1;
    }
    Then I have created a new Item to my project named Item1.h
    Here I have written this:

    Code:
    #define Criteria Number == 5
    I cant think of any other solution of how to do this. This is ofcourse
    the only way to do it as I dont know any other way though.
    The thing is that I have to in any way write this code in Item1.h.
    Last edited by Coding; 03-12-2008 at 02:31 PM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The obvious question is: why don't you write
    if( Number == 5 )
    From the beginning instead? Why do you have to make a define?
    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.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Now I know that you dont like that I am doing an "own" language within the language but this is working if you are very careful.
    This is what I am doing and have to do. So I will use an emty .h file where other people can code in this "own" language without interacting inside the C++ code where all thousands of lines are.
    They will just concentrate to write things in Item.h.
    This is the whole reason why. These people dont know C++.

    I also agree that it feels wrong to use define.
    So I dont know if it could be possible in any way to do what I am trying to do ?
    Last edited by Coding; 03-12-2008 at 02:40 PM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If people don't know C++, then they shouldn't be coding at all. Come on, Coding.
    If you want to make a scripting language for people, then write a parser or interpreter.
    Just because someone doesn't know C++ doesn't mean you should try to abstract away the syntax. They should learn C++, you should not try to make them get away with it.

    If everyone used different syntax for creating C++, no one would understand someone else's code here. A total mess (which is essentially what you're trying to invent).
    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.

  15. #15
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by Coding View Post
    Now I know that you dont like that I am doing an "own" language within the language but this is working if you are very careful.
    This is what I am doing and have to do. So I will use an emty .h file where other people can code in this "own" language without interacting inside the C++ code where all thousands of lines are.
    They will just concentrate to write things in Item.h.
    This is the whole reason why. These people dont know C++.

    I also agree that it feels wrong to use define.
    So I dont know if it could be possible in any way to do what I am trying to do ?
    What you could do then is either write an interactive program and prompt your co-workers for the "criteria" or just allow them to follow some standardized form and write it into a text file. I think if they're not coders this is probably better.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  2. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  3. macro (#define) question
    By Darrok in forum C++ Programming
    Replies: 30
    Last Post: 12-20-2001, 05:01 PM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM