delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/24/02:46:49.1

Message-ID: <395455CD.8BBDF39@pacbell.net>
From: Wesel <nospam AT pacbell DOT net>
X-Mailer: Mozilla 4.73 [en] (Win98; I)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: External Classes
References: <8j0pv6$5h9$1 AT news DOT netvision DOT net DOT il>
Lines: 88
Date: Fri, 23 Jun 2000 23:31:41 -0700
NNTP-Posting-Host: 63.197.122.98
X-Complaints-To: abuse AT pacbell DOT net
X-Trace: news.pacbell.net 961827953 63.197.122.98 (Fri, 23 Jun 2000 23:25:53 PDT)
NNTP-Posting-Date: Fri, 23 Jun 2000 23:25:53 PDT
Organization: SBC Internet Services
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Avi Berkovich wrote:
> 
> Hello Everybody,
> 
> I wish to know if it's possible and if it is, how can I use external
> classes.
> 
> For example, if I have a class compiled in an object file, how can I use it
> from my main program?
> I have tryed many ways and was unsuccessful.

Humm....  If you don't have the header file for that class, there's not
much I know to do.  It's almost impossible even to call a regular global
function in an object file without the function's prototype declared.

If you *do* have the header file for that object file, then you can
solve your problem as easily as you would use 'cout'.  Just #include
that header file before you use any of the classes or functions
established in the object file.

For example, myclass.o.  You would need a .h file something like this...

myclass.h
----------------
#ifndef myclassheader
#define myclassheader

class someclass
{
    //public: / protected: / private:
    //data members
	int classdata;

    //member function prototypes
	void someFunction(/*arguments*/);
};

//non-member functions.
void someNonMemberFunction(/*arguments*/);

//global data initiailized in myclass.o
extern int globaldata;

#endif
--------------

All myclass.o would do is allow you to use all the member functions, and
global functions, and global variables without having to compile them
every time.  It's nothing more than a convenience, but it's a
convenience that can make or break a programmer.

Of course, the source (.cpp) file for your object file would have to
look like this otherwise nothing I said would work.

myclass.cpp
-----------------
#include "myclass.h"

someclass::someFunction(/*arguments*/) 
{
    //define function here
}

someNonMemberFunction(/*arguments*/)
{
   //define function here
}

//and also,

int globaldata = 13;

-------------

You do not need the source file.  You do need the header file.  The
names in the header must be the same as the names in the source file,
whether you got the header file from someone else (i.e. <iostream.h>) or
you built the code yourself (i.e. "myclass.h").  As long as you have the
header file, (.o) files can share classes, functions, and global
variables.  In short, it allows you to build your program in steps,
instead of all at once.

*remembers waiting 30 min. for MSVC to compile some non-modular code...*

Wheoo.  Having 16 meg of RAM was not fun...


Wesel

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019