You are here:

C++/copying a function

Advertisement


Question
plzz help.
  how a can i write a program to copy function (as a whole) from one c++ file to another c++ file ,(appending to its end)
    the inputs of the program shold be source file name,function name and destination file name.
 its not homework.its part of a code automation programme

Answer
This seems easy in concept, but is in fact quite when you sit down and consider all the details.

The concept is easy:

- Open the source source-code file for reading

- Read it to locate the function in question.

- Open the target source-code file for write-appending

- Copy the whole function text from the source source-code file to the end of the target source-code file.

- Close both files.

Job done.

The complexity comes in reading and processing the contents of the source source-code file. First let us look at where you find functions. In C++ functions can be stand alone (in the  global namespace), part of a class (which includes structs) or part of a namespace. They may be templated - as function templates, member functions of class templates, or member function templates of a class or a class template. Functions may be overloaded - that is more than one function of the same name may exist in a given namespace (which in this context includes classes). Functions can have many declarations (e.g. function prototypes), but only a single definition. Often these occur in a separate header file. Do you also need to copy these to some other header file?

So you may have to specify the fully qualified name of the function (e.g. OuterNamespace::InnerNamespace::OuterClass::InnerClass::FunctionName), the parameters for the function: (e.g. int n, std::string const & text ) and for member functions whether the function is const. The return type may also be useful.

Now the definition of the above function could look be written in the code in several ways for example:

void OuterNamespace::InnerNamespace:: OuterClass::InnerClass::FunctionName
(int n, std::string const & text )
{
// ...
}

or like so:

namespace OuterNamespace
{
// ...
       namespace InnerNamespace
       {
       // ...
         void OuterClass::InnerClass::FunctionName
         (int n, std::string const & text )
         {
         // ...
         }
       // ...
       }
// ...
}

Or maybe :

namespace OuterNamespace
{
// ...
       namespace InnerNamespace
       {
       // ...
         class OuterClass
         {
         // ...
         class InnerClass
         {
         // ...
         void FunctionName
         (int n, std::string const & text )
         {
         // ...
         }
         // ...
         };
         // ...
         };

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

In which the // ... show places other source-text could be. You will notice that the definition can be contained within open namespace or classes definitions (in the latter case the member function would be inline) or can be specified by qualification - in which case a declaration would be expected in the open namespace or class:

namespace OuterNamespace
{
// ...
       namespace InnerNamespace
       {
       // ...
         class OuterClass
         {
         // ...
         class InnerClass
         {
         // ...
         void FunctionName
         (int n, std::string const & text );

         // ...
         };
         // ...
         };

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

Then there are cases where for example, the types are specified slightly differently - std::string const & might be specified as const std::string & for example, and if the author decided to be using std::string then maybe the std qualification might be missing.

You will have to decide how to handle such cases.  

I have purposely chosen some awkward cases to make you think on what you are doing - as you give no hint as to the scope of the problem.

If, for example, your intention is to add boilerplate code to classes from template code files then this complexity can be ignored by copying whole template code files - probably performing some form of substitution for things like the correct class name in the process. The mechanism is dumb - it just copies whatever is in the file and substitutes special tags in the template code for the particular circumstances in effect at the time (such as the target's class name). Note the use of template here is the more generic meaning and not the C++ meaning of class or function templates.

However, if it is your wish to locate a function in an arbitrary piece of source code and copy it to another source code file then the problem becomes much more complicated as I tried to indicate ablove!

Your mention of code automation makes me think you require something much more like the former than the latter. So consider if your code automation functionality could be performed using files containing code snippets with special tags in where values need to be substituted. A side effect of such an approach is that is not restricted to just functions or just C++. For example you might create a file containing a boilerplate event handler for a framework:

// OnLeftMouseButtonClick
// Mouse left button click handler for class $$CLASS$$
void $$CLASS$$::OnLeftMouseButtonClick
{
       $$$BASECLASS$$::OnLeftMouseButtonClick()

// TODO: Add your left mouse button click handling code here
}

Your application opens this file, reads it character by character and writing most of them straight to the target source code file (which you have opened for write-appending). However, on detecting the string $$ the application interprets the characters up to the closing $$ sequence as a keyword that need to be substituted - in which case the substituted text is written out instead.

Now all you need to do is to associate the various functions that ca be automatically added to your code with the files that contain the code template(s) and determine the values that vary from case to case - define then as substitutable values - with a keyword that is placed in code templates and a value for each case that is available for substitution.

Note that I suspect you will find you will not always wish to place the code at the end of a file. In some cases - member function declarations for example - the text will have to go in some sensible place within the source file.

I suspect you can refine this technique some what but is should be good enough to get you going - if this is the sort of thing you require. Sorry if I guessed wrong but you did not give me a lot to go on.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.