Preprocessor Solution

  • + 9 comments

    inline makes defined function as a copy-paste code, i.e. it suggest to compiler that "You can replace this code, instead of calling it as a function.". Let me explain with an example. We code this program:

    #include <iostream>
    
    inline void print(string name) {
        std::cout << name << endl;
    }
    
    int main() {
       string myName = "Meysam";
       
       print(myName);
       
       return 0;
    }
    

    When you compile this code, your code suggest to your compiler that "It's a good idea if you replace print function with it's code, huum?" and based on situation, if compiler accepts this suggestion, code will converts to this:

    #include <iostream>
    
    inline void print(string name) {
        std::cout << name << endl;
    }
    
    int main() {
       string myName = "Meysam";
       
       std::cout << myName << endl;
       
       return 0;
    }
    

    and then compiled.

    This optimision feature of C++ in many situations is a good solution instead of using macros. It will reduce overhead of program, but this also has some disadvantures that you can find them with search on google :D.

    If any explaination is required, I'm here ;).

    • + 3 comments

      God Damn. You are the hero and teacher we need but don't deserve.

      • + 0 comments

        lol stop being emotional :p

      • + 0 comments

        :-D

      • + 0 comments

        i know right.

    • + 0 comments

      Dude! You are awesome! I leraned a lot from your comments! Thank you!!!! xD

    • + 0 comments

      Thanks a lot ,friend

    • + 0 comments

      Best explanation..Thanks a lot Meysampg

    • + 0 comments

      Thanks dude. You are awesome!

    • + 0 comments

      thanx.respect from india

    • + 0 comments

      thanks a lot :)

    • + 0 comments

      This made my day.. Thank you so much

    • + 0 comments

      Thank you so much Sir!!