views:

75

answers:

2

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;
}
+2  A: 

The Register::GetHeader() function returns a const Header &. Therefore you cannot call any non-const member functions through this reference.

To fix your problem, i expect you want to make the Header::Field_1() function const.

ie. declared as unsigned long Field_1(void) const;

Alex Deem
+2  A: 

Yes, when GetHeader() returns a const reference to the Header object, you can only call const methods on it. In this case if you declare Field_1 as a const method by writing:

unsigned long Header::Field_1(void) const

(note the const on the end)

The trouble is if you do this then within the const method all other members appear as if they are const too (unless you mark them as mutable).

As a result you will probably have trouble with the reinterpret_cast<char*>(&field1).

In which case the simplest solution is probably to have GetHeader() return a non-const reference. Alternatively you could mark field1 as mutable (but make sure you understand the semantics and dangers of mutable first if you go down that route. My feeling is that making GetHeader() return a non const reference is what you really want here)

Phil Nash