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;
}