You are here:

C++/access modifier without class?

Advertisement


Question
Hello,

I saw the following code in one c++ header file, there are only 3 functions, without class, without including anything else. I wonder why there are access modifier, but no class? thanks

#ifndef _I_H_
#define _I_H_

public:
   boolean Transfer( const char* sessionId, long& traceNumber );
   boolean Update( const char* sessionId );

private:
   void TransferInfo( const char* sessionId );

#endif

Answer
Well firstly the C++ Boolean type is called bool rather than boolean, so assuming the code above should read something like so:

   #ifndef _I_H_
   #define _I_H_

   public:
       bool Transfer( const char* sessionId, long& traceNumber );
       bool Update( const char* sessionId );

   private:
       void TransferInfo( const char* sessionId );

   #endif

Or that boolean was some other type or type alias declared elsewhere.

I shall assume that the boolean type is declared in another header file called boolean.h, which might look like so:

boolean.h:
----------

   #ifndef SOMEPROJECT_BOOLEAN_H
   # define SOMEPROJECT_BOOLEAN_H

   typedef bool    boolean;

   #endif // SOMEPROJECT_BOOLEAN_H

Moving on...

Indeed on the face of it the contents of this header file are quite useless as it is illegal C++ if taken on its own.

Hence I suspect if you looked at where it is actually used (i.e. where it is used in a #include pre processor directive) you will find it is in the middle of a class declaration, quite likely in other header files.

Thus, assuming the file containing the code you are asking about is called i.h, then just including it in some other C++ source file as usual:

somesource.cpp:
---------------

   #include "boolean.h" // for definition of boolean type
   #include "i.h"

   // other code ...

will give compiler errors.

However, including i.h in the middle of a class specification thus:

someclass.h:
------------

   #ifndef SOMEPROJECT_SOMECLASS_H
   # define SOMEPROJECT_SOMECLASS_H
   # include "boolean.h" // for declaration of boolean type

   class SomeClass
   {
   // some class declaration code ...

   # include "i.h"

   // more class declaration code ...
   };

   #endif // SOMEPROJECT_SOMECLASS_H

Will cause the preprocessor to produce code for compilation something like:

   typedef bool    boolean;

   class SomeClass
   {
   // some class declaration code ...

   public:
       boolean Transfer( const char* sessionId, long& traceNumber );
       boolean Update( const char* sessionId );

   private:
       void TransferInfo( const char* sessionId );

   // more class declaration code ...
   };

Which would be legal C++, but might cause unexpected errors elsewhere if the code surrounding the including of i.h does not take note that it leaves the access at private, so maybe i.h should be included as the first or last part of a class declaration, rather than in the middle as my example implies.

As to why someone would want to do this - well I think we would require more context on the situation in which such techniques (I might go so far as to call them 'tricks') are used, and maybe some background on the reasoning behind such design decisions.

Hope this helps

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

©2012 About.com, a part of The New York Times Company. All rights reserved.