1

I wrote a simple C++ class program to read and/or to print out some BMP header information as shown below and it works just fine. However, I want to take the advantage of what a C++ contructor initializer can offer. So, I want to use the C++ contructor initializer to open the file as well as retrieve the data, i.e. mOffBits, mWidth, and mHeight. Is this possible?

Header file:

#include <fstream>
#include <iostream>
#include <string>

class BMP
{
  public:
        BMP (const std::string& inFileName);
        ~BMP ();
        const void printInfo () const;

  private:
        std::ifstream ifs;
        std::string mInFileName;
        std::uint32_t mOffBits;
        std::int32_t mWidth, mHeight;
        char str [54];
};

Program file:

#include "bmp.h"

BMP::BMP (const std::string& inFileName) : mInFileName (inFileName)
{
        ifs.open (mInFileName, std::ios::in | std::ios::binary);

        ifs.seekg (0, std::ios::beg);
        ifs.read (&str [0], 54);
        mOffBits = *reinterpret_cast <std::uint32_t*> (&str [0xA]);
        mWidth = *reinterpret_cast <std::int32_t*> (&str [0x12]);
        mHeight = *reinterpret_cast <std::int32_t*> (&str [0x16]);
}


BMP::~BMP ()
{
        ifs.close ();
}


const void BMP::printInfo () const
{
        std::cout << "\tInput filename:\t\t" << mInFileName << std::endl;
        std::cout << "\tBegin of IMG data:\t" << mOffBits << std::endl;
        std::cout << "\tHeight:\t\t\t" << mHeight << std::endl;
        std::cout << "\tWidth:\t\t\t" << mWidth << std::endl;
}

main.cpp:

#include "bmp.h"

auto main (int argc, char* argv []) -> int
{
        if (argc < 2)
        {
                std::cout << argv [0] << " image.bmp" << std::endl;
                return 1;
        }

        // Itialize bmp class;
        BMP bmp (argv [1]);

        // print out header information.
        bmp.printInfo ();

        return 0;
}
Habibie
  • 11
  • 2
  • Your use of `*reinterpret_cast` is undefined behaviour (strict aliasing violation); use `memcpy` instead , or preferably use portable unpacking code – M.M Oct 23 '17 at 21:45
  • I would not recommend doing the thing you are asking for ; it makes the code more complicated for no reason – M.M Oct 23 '17 at 21:46
  • Whether it will be more or less complicated, I have nothing to lose mainly because I sure would like to learn something new. – Habibie Oct 23 '17 at 21:52
  • Basically you will make a function that reads `str`, call it from the ctor-intiializer list using the comma operator in one of the initializers prior to `mOffBits`, and then the initializers for `mOffBits` etc. will be function calls that extract the int from `str`. – M.M Oct 23 '17 at 21:59
  • Yes and your suggestion should OK. Actually, a few weeks ago I already did it after reading this [post](https://stackoverflow.com/questions/4162021/is-it-ok-to-call-a-function-in-constructor-initializer-list). I am hoping and/or looking for a more direct approach that does not involve calling a method. – Habibie Oct 24 '17 at 16:27
  • You can't avoid having the member function calls on `ifs`. You *could* have a long series of comma-operator expressions but that would be ghastly to say the least – M.M Oct 24 '17 at 20:38
  • Yes and you are right to the point. Thank you. – Habibie Oct 25 '17 at 03:25

0 Answers0