where to declare and where to define the function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rasmidas
    New Member
    • Jun 2007
    • 56

    where to declare and where to define the function?

    There are two different directories.

    sparse/src/stk/dmg_apiutil
    and
    sparse/src/stk/dmg_meta_doc

    Inside these two directories there are lot of C++ files.

    I have written a function:

    void cFreeMemory( char* entityName, void* ptrStruct )
    {
    // Check if the Pointers are NULL
    if( ptrStruct != NULL && sGetEntityByNam e( entityName ) != NULL )
    {
    sEntityFree( sGetEntityByNam e( entityName ),
    ( void** )&ptrStruct,
    sYES );
    }
    }

    How I could access this function inside every file in the two directories? I mean where and how I need to declare this function so that I can access from every file inside the mentioned two directories.

    Thanks in advance.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by rasmidas
    There are two different directories.

    sparse/src/stk/dmg_apiutil
    and
    sparse/src/stk/dmg_meta_doc

    Inside these two directories there are lot of C++ files.

    I have written a function:

    void cFreeMemory( char* entityName, void* ptrStruct )
    {
    // Check if the Pointers are NULL
    if( ptrStruct != NULL && sGetEntityByNam e( entityName ) != NULL )
    {
    sEntityFree( sGetEntityByNam e( entityName ),
    ( void** )&ptrStruct,
    sYES );
    }
    }

    How I could access this function inside every file in the two directories? I mean where and how I need to declare this function so that I can access from every file inside the mentioned two directories.

    Thanks in advance.
    Declare that function in a .h file and include that file in every .cpp file that needs
    that function. Define that function somewhere in a .cpp file; possibly its own
    .cpp file or an already existing one. Compile and link everything and voila.

    kind regards,

    Jos

    Comment

    • rasmidas
      New Member
      • Jun 2007
      • 56

      #3
      Should I need to declare that as static ?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by rasmidas
        Should I need to declare that as static ?
        If you want to use that function in other translation units (other .cpp file) you
        most certainly shouldn't declare (and define) that function as static. It needs
        external linkage, because other functions in other translation units need to
        be able to find it. Static functions can only be used in the translation unit
        where they are defined themselves.

        kind regards,

        Jos

        Comment

        • rasmidas
          New Member
          • Jun 2007
          • 56

          #5
          Thanks. Thanks a lot.

          Comment

          Working...