declaring and defining at the same time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • curious2007
    New Member
    • Jun 2007
    • 71

    declaring and defining at the same time

    How can I define and declare a class and its functions at the same time? Is this posssible?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You don't define a class. You declare it.

    The definition is the creation of an object of that class.

    The class methods ar typically declared by having the function prototypes inside the class declaraion. You can put the function definition there (the code) but that makes the methods inline and that may not be what you want.

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Originally posted by curious2007
      How can I define and declare a class and its functions at the same time? Is this posssible?
      Do you mean like this:?
      [code=cpp]
      class Test
      {
      public:
      void testrun() { cout << "BLAH BLAH" << endl; };
      }
      [/code]
      The functions that are defined in the class will be inline.

      Comment

      • curious2007
        New Member
        • Jun 2007
        • 71

        #4
        OK, thanks but what happens if a function is inline? Does this put restrictions on what I can do with it? Also, if I make this function declerations and definitions in one file, is this going to be an hpp or cpp?

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by curious2007
          OK, thanks but what happens if a function is inline? Does this put restrictions on what I can do with it? Also, if I make this function declerations and definitions in one file, is this going to be an hpp or cpp?
          An inline functions means whenever that function is called the compiler puts the code from that function in that specific spot. Regular functions (not inline) are called and the code executes the function. That might cause some overhead. That's why if a function is small and is called many times, it might be good to make it inline. But, it is dangerous because your file will grow in size depending on the size of the function. If you want to make you functions inline in a class just declare in the class declaration them in the .hpp file.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by curious2007
            OK, thanks but what happens if a function is inline? Does this put restrictions on what I can do with it? Also, if I make this function declerations and definitions in one file, is this going to be an hpp or cpp?
            An inline function is not called. Instead, the definition is inserted in line. That means there is no function. That means there can be no function address. That means no virtual functions. That means no object-oriented programming using inline functions.

            The compiler needs to see the function definition in oreder o insert it in line. That means the inline function definition goes in a header file.

            Comment

            Working...