Thread: including outside functions into classes

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    including outside functions into classes

    Hi,

    I have question. Let say I have a hpp file with a list of inline finctions like this:

    Code:
    inline int check() {
      return 1;
    }
    
    inline int check_1() {
      return 1;
    }
    ...
    What I would like to do is to include them into several unrelated classes. How can I do this. Can I just add the hpp inline functions in headers of my class containing files or not. I mean if they are not defined as class functions how can they be called. I don't understan the logic.

    b

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by baxy
    What I would like to do is to include them into several unrelated classes. How can I do this. Can I just add the hpp inline functions in headers of my class containing files or not. I mean if they are not defined as class functions how can they be called. I don't understan the logic.
    Inline functions can be called like any other function. That they are declared inline just means that you hint to the compiler to inline them, and whether or not they are actually inlined, special steps are taken such that can appear in multiple translation units without breaking the one definition rule.

    So, if you have functions defined in the header, e.g., a member function defined inline in the class definition, that calls one of these inline functions, you should include the header containing these inline function definitions in that header. If one of these inline functions is called a member function defined in a source file instead, then you should include the header in that source file.

    In this sense, you are not including them into several classes, but rather including them into translation units in which they are called.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    hm ... thnx all clear now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes and functions
    By cpudaman in forum C++ Programming
    Replies: 20
    Last Post: 12-18-2007, 02:45 AM
  2. Class files including other classes
    By combatdave in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2006, 12:37 AM
  3. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 11:19 AM
  4. Including functions at run-time
    By Pea in forum Tech Board
    Replies: 4
    Last Post: 10-14-2004, 12:42 PM
  5. Including C++ headers (and functions) to a C-file
    By torbjorn in forum C Programming
    Replies: 4
    Last Post: 10-24-2002, 12:07 AM