Access modifier without class?

Last Edited By Krjb Donovan
Last Updated: Mar 11, 2014 07:50 PM GMT

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

  1. ifndef _I_H_
  2. define _I_H_

public:

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

private:

   void TransferInfo( const char* sessionId );
  1. 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

Advertisement

©2024 eLuminary LLC. All rights reserved.