SQLite Forum

need help!! invalid class typecast
Login

need help!! invalid class typecast

(1) By ianything on 2021-01-06 03:29:14 [link] [source]

i use cbuilder read blob data,such as:

       TStream *MainStream=new TMemoryStream();
       Query->FieldByName("MainData")->SetFieldType(ftBlob);
       TBlobField *mainField = (TBlobField*)Query->FieldByName("MainData");
       mainField->SaveToStream( MainStream);

pop error invalid class typecast so read this item classname as : ShowMessage(Query->FieldByName("MainData")->ClassName()); it as TBytesField ,not a TBlobField , may anyone tell me how to deal it?

(2) By Larry Brasfield (LarryBrasfield) on 2021-01-06 16:31:39 in reply to 1 [source]

The TBlobField class represents a variably sized binary field. This is distinctly different from a blob in the database which, once created, has a fixed size. Having fixed size data is one attribute of the TBytesField class. So that's one conceptual problem you need to deal with.

Dealing with it will involve using the CBuilder/Delphi/Lazarus infrastructure already existing to deal with blobs in databases rather than crudely trying to alter the object type in-place.

Another tip: You would do well to learn to use static_cast (or dynamic_cast) to perform compatible type conversions. This is to be contrasted with reinterpret_cast, which is what your C-style cast becomes when there is no meaningful static_cast for the object whose type you want change. It is very rare that a reinterpret_cast is appropriate for pointers to class instances. Hence, your C-style cast is the moral equivalent of wearing a "Kick me!" sign. This is particularly true where you rely on compile-time type checking for program correctness rather than careful study and understanding of library docs. The C-style cast is shorthand for "I know exactly what I'm doing here, so just do what I say." It is a way to generate runtime problems when you do not know exactly what you are doing. The static_cast leaves the compiler an opportunity to respond, "Sorry, that is not sensible."