Solved

Declaring external functions within a class

Posted on 2003-12-01
2
198 Views
Last Modified: 2010-04-01

I've created a Dynamic Link Library in C++ (Borland C++ Builder to be exact).
The DLL contains one class (in this case we'll call it class1) which has both public and private functions.

Is it possible to implement some of the "class1" functions outside of  the class1.cpp file?
i.e. implement functions func1 and func2 below in a separate .cpp file?

header file definition for class1:
~~~~~~~~~~~~~~~~~~~

    class  __EXPORT_TYPE class1 {
    public:
        char * func1 (char * aParam1);
        char * func2 (char * aParam2);
        char * func3 (char * aParam3);
    private:
        char * func3 (char * aParam4);
        char * func4 (char * aParam5);
   }

As there is around 100 functions to implement, it would nice to implement these in different .cpp files.
I've tried to declare them as "extern" in the class1 definition although the compiler does not seem to like this.

hope you can help.


Cheers,

weslondon

0
Comment
Question by:weslondon
2 Comments
 
LVL 11

Accepted Solution

by:
bcladd earned 400 total points
ID: 9850953
You have already separated the header from the implementation file, right? All you need to do is write the appropriate implementation files and tell the compiler they are all required for the object to be built. This is a dependency/linking problem rather than a C++ problem:

(1) There is no need to extern the functions because they are not external IF you link all of the .cpp implementation files into the DLL

(2) The declaration of the functions is already separate from the definitions so no client of class1 needs to care about how you structured your source code.

Hope this helps,
-bcl
0
 
LVL 86

Expert Comment

by:jkr
ID: 9851107
bcladd is right - no need to specify them as "extern". You basically would

------------->8-------------------------------

// header.h

   class  __EXPORT_TYPE class1 {
   public:
       char * func1 (char * aParam1);
       char * func2 (char * aParam2);
       char * func3 (char * aParam3);
   private:
       char * func4 (char * aParam4);
       char * func5 (char * aParam5);
  };


------------->8-------------------------------

// functions1.cpp
#include "header.h"

char * class1::func1 (char * aParam1) {
}

char * class1::func2 (char * aParam1) {
}

------------->8-------------------------------

// functions2.cpp
#include "header.h"

char * class1::func3 (char * aParam1) {
}

------------->8-------------------------------

// functions3.cpp
#include "header.h"

char * class1::func4 (char * aParam1) {
}

char * class1::func5 (char * aParam1) {
}


0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question