Domanda

I'm deriving a new class from the VCL TStream class:

// A stream based on a temporary file, deleted when the stream is closed

class TTempFileStream : public TStream
{
...
public:
  using TStream::Seek;

   __int64 __fastcall Seek(const __int64 Offset, TSeekOrigin Origin)
  {
    return 0; // for simplicity!
  }

  ... 
} ;

TStream declares the following two variants of Seek:-

virtual int __fastcall Seek(int Offset, System::Word Origin)/* overload */;
virtual __int64 __fastcall Seek(const __int64 Offset, TSeekOrigin Origin)/* overload */;

But I get the following W8022 warning when compiling my class:-

[BCC32 Warning]_utils.h(166): W8022 
'_fastcall TTempFileStream::Seek(const __int64,TSeekOrigin)' hides virtual function '_fastcall TStream::Seek(int,unsigned short)'

Surely the Using declaration should fix that?

To drag this question back on track, I'm aware of the way that the two versions of TStream::seek interrelate, and I'm just trying to get inherited Seek(int,int) method exposed by the derived class. Why isn't my using declaration doing that?

È stato utile?

Soluzione

Roddy, your code is very much correct.

The code works as expected (tested) when adding using TStream::Seek; otherwise as the warning states, will hide the base class method. (this part of C++ language, Remy have to disagree for the first time with you).

The warning is a false positive, a very old and not yet correcred BUG in C++ Builder present at least from version 2006 to XE4.

Altri suggerimenti

You do not need the using statement at all, so get rid of it. You are overriding the 64-bit Seek() method. That is all you need to do. You get the 32-bit Seek() method for free since it is a public method of TStream and you are using public inheritance on your derived class. You do not have to declare anything to get or use the 32-bit Seek() method. As long as you do not override it, it will internally call your 64-bit overriden Seek() if called.

class TTempFileStream : public TStream
{
...
public:
    ...
    virtual __int64 __fastcall Seek(const __int64 Offset, TSeekOrigin Origin)
    {
        return 0; // for simplicity!
    }
    ... 
};

FYI, if all you need is to delete the temp file when the stream is closed, you don't need a derived class at all. You can use the RTL's THandleStream as-is instead, passing it a HANDLE from the Win32 API CreateFile() function, where you specify the FILE_FLAG_DELETE_ON_CLOSE flag to CreateFile().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top