Well, I think my problem originates in a lake of knowledge of basic C++ concepts. The problem is, in my code (below) I have the classes Header and Register. For both, I pass a reference to a ifstrem file already opened. The Header reads some bytes from it. Register has a method to return a reference of Header (which is passed in Register constructor).
The problem is, when I declare the reference to Header as const (in my Register class), I've got an error message saying:
error C2662: 'Header::Field_1' : cannot convert 'this' pointer from 'const Header' to 'Header &'
The problem, I guess, is:
Field_1 method changes the file cursor through seekg. therefore this member object cannot be const (its internal structure is being changed). Being fstream _stream declared inside Header class, and having a const reference for this class does it make all its internal members const as well?
PS: I'm using VS C++ express. The code below doesn't do anything useful, is just an example.
// ********** HEADER.H ***********
#ifndef __HEADER_H__
#define __HEADER_H__
#include <fstream>
class Header
{
private:
std::ifstream* _stream;
unsigned long field1;
public:
Header(std::ifstream* stream);
~Header() { }
unsigned long Field_1(void);
};
class Register
{
private:
const Header& _header;
std::ifstream* _stream;
public:
Register(const Header& header, std::ifstream* stream);
~Register(){ }
const Header& GetHeader(void) { return _header; }
};
#endif /*__HEADER_H__*/
//********* PROGRAM.CPP *************
#include <iostream>
#include "header.h"
using namespace std;
Header::Header(ifstream* stream) : _stream(stream)
{
}
unsigned long Header::Field_1(void)
{
_stream->seekg(0x00, fstream::beg);
_stream->read(reinterpret_cast<char*>(&field1), sizeof(field1));
return field1;
}
Register::Register(const Header& header, std::ifstream* stream): _header(header), _stream(stream)
{
}
int main(void)
{
ifstream file("test.dat", ios::binary | ios::in);
Header h(&file);
Register reg(h, &file);
cout << "Field 1 " << reg.GetHeader().Field_1() << endl;
file.close();
return 0;
}