Declaring external functions within a class

Posted on 2003-12-01
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 {
        char * func1 (char * aParam1);
        char * func2 (char * aParam2);
        char * func3 (char * aParam3);
        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.



Question by:weslondon
LVL 11

Accepted Solution

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,
LVL 86

Expert Comment

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


// header.h

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


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

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

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


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

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


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

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

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


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object modeling - load and save functions 5 59
Getting IP address 8 71
I could not build boost code, 10 78
Exception thrown at 0x00007FFD5BC81F28 7 38
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now