Re: Novice - Advice required




"David++" <David@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:0BD14C22-7286-4D6C-BCCE-10F9E260E143@xxxxxxxxxxxxxxxx

> Due to its size I would like to write this function into a new
> class (?) in
> a seperate .cpp file (or header file?) and then whenever I want to
> use the
> function I would create an object of this class, initialize it
> with the
> value, do the computation, return the result and then destroy the
> object. Is
> this the best approach for this task?

"Best" is a value judgement which only you can make.

But in principle there is nothing wrong with classes whose purpose
is just to perform certain tasks.

> Further, would it be best define the class in a cpp file or in a
> header file
> and then 'include' the header file which defines the class?

Usually you define the class in a .h file and implement its member
functions in a .cpp file which includes the .h file.

So file "xyz.h" would contain

class XYZ
{
public
int doTask( int x );
};

and "xyz.cpp" would contain

#include "xyz.h"

int XYX::doTask( int x )
{

//.......................
}

The place where doTask() is called from also needs to include
xyz.h.

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm


.



Relevant Pages

  • Common Environment Object Model: getting the path to the cpp file a certain class is implemented in
    ... Say we have a CodeClass object and call AddFunction() on it. ... the call will end up with the function declaration ... in the header file for that class and the implementation in the cpp file. ...
    (microsoft.public.vsnet.general)
  • Re: C++ header file
    ... Why not just have 1 CPP file and include the CPP file instead of ... In Java, there is no header file, each object is .java file. ... Separation of declaration and definition is very practical thing. ... It is sufficient to see only function's declaration in order to successfully compile a code that calls it. ...
    (microsoft.public.vc.language)
  • Re: Importing a type library into unmanaged code with /clr switch - linker errors
    ... I made some progress by just enabling /clr on the module (cpp file) ... but when I add managed code into the header file like: ... I get the compiler error: ...
    (microsoft.public.dotnet.languages.vc)
  • Re: accessing variables from more than one .cpp file
    ... i want to declare some variables with in a header file. ... extern int i; ... Include the header file in every .cpp file where you need to use it. ... Understanding the difference between declarations and definitions is ...
    (comp.lang.cpp)
  • Re: call c++ function from c
    ... header file from the cpp file. ... In the header file the function is ... that lie to get a job, then dog it for as long as they can with a constant ... actually ever writing any type of useful software. ...
    (comp.lang.c)

Loading