Hi,
Suppose we have
class CAbstractFile {
public:
virtual bool Open()=0;
virtual bool Read(long num_bytes,void *bufferp)=0;
virtual bool Write(long num_bytes,void *bufferp)=0;
virtual bool Close()=0;
};
class CDiskFile : public CAbstractFile {
private:
CString Path;
HANDLE Handle;
public:
// all implemented in CDiskFile.cpp
void SetPath(CString path);
virtual bool Open();
virtual bool Read(long num_bytes,void *bufferp);
virtual bool Write(long num_bytes,void *bufferp);
virtual bool Close();
};
class CRamFile : public CAbstractFile {
private:
void *BufferStart;
long BufferLength;
long Position;
public:
// all implemented in CRamFile.cpp
void SetRamBuffer(void *startp,long length);
virtual bool Open();
virtual bool Read(long num_bytes,void *bufferp);
virtual bool Write(long num_bytes,void *bufferp);
virtual bool Close();
};
Now i want to derive a new class CJpegFile that implements reading &
writing of Jpeg files.
I want that CJpegFile object can be a CDiskFile or a CRamFile,
so that we can read/write a jpeg file as well via a diskfile as well
via a ramfile.
How should this be done in good C++?
I can't find the answer, and i'm sorry if this is a stupid question.
Cheers,
Vrish 7 2126 vr*****@yahoo.com wrote: Suppose we have
class CAbstractFile {
public:
virtual bool Open()=0; virtual bool Read(long num_bytes,void *bufferp)=0; virtual bool Write(long num_bytes,void *bufferp)=0; virtual bool Close()=0; };
class CDiskFile : public CAbstractFile {
private:
CString Path; HANDLE Handle;
public:
// all implemented in CDiskFile.cpp void SetPath(CString path); virtual bool Open(); virtual bool Read(long num_bytes,void *bufferp); virtual bool Write(long num_bytes,void *bufferp); virtual bool Close(); };
class CRamFile : public CAbstractFile {
private:
void *BufferStart; long BufferLength; long Position;
public:
// all implemented in CRamFile.cpp void SetRamBuffer(void *startp,long length); virtual bool Open(); virtual bool Read(long num_bytes,void *bufferp); virtual bool Write(long num_bytes,void *bufferp); virtual bool Close(); };
Now i want to derive a new class CJpegFile that implements reading & writing of Jpeg files.
I want that CJpegFile object can be a CDiskFile or a CRamFile, so that we can read/write a jpeg file as well via a diskfile as well via a ramfile.
How should this be done in good C++?
Something like
class CJpegFile : public CRamFile, public CDiskFile {
...
void SetIsDisk();
void SetIsRam(); // those two should probably throw if not closed
bool Open() {
if (isDisk)
CDiskFile::Open();
else
CRamFile::Open();
}
// others similarly
};
However, if you don't need the same CJpegFile to be able to write to
either disk or RAM at will, you'd be much better off with two classes
CJpegDisk and CJpegRam instead of one.
V
Hi Victor,
Victor Bazarov wrote: Something like
class CJpegFile : public CRamFile, public CDiskFile { ... void SetIsDisk(); void SetIsRam(); // those two should probably throw if not closed bool Open() { if (isDisk) CDiskFile::Open(); else CRamFile::Open(); } // others similarly };
What a pity that we can't use the C++ polymorphism here.
I thought that's what virtual functions are for, to automatically
switch between different implementations of a (abstract) base class.
However, if you don't need the same CJpegFile to be able to write to either disk or RAM at will, you'd be much better off with two classes CJpegDisk and CJpegRam instead of one.
Would that mean we have 2 times the same jpeg specific code, once using
the CDiskFile functions, and once using the CRamFile functions?
Cheers,
Vrish vr*****@yahoo.com wrote: Hi Victor,
Victor Bazarov wrote:
Something like
class CJpegFile : public CRamFile, public CDiskFile { ... void SetIsDisk(); void SetIsRam(); // those two should probably throw if not closed bool Open() { if (isDisk) CDiskFile::Open(); else CRamFile::Open(); } // others similarly };
What a pity that we can't use the C++ polymorphism here.
I thought that's what virtual functions are for, to automatically switch between different implementations of a (abstract) base class.
However, if you don't need the same CJpegFile to be able to write to either disk or RAM at will, you'd be much better off with two classes CJpegDisk and CJpegRam instead of one.
Would that mean we have 2 times the same jpeg specific code, once using the CDiskFile functions, and once using the CRamFile functions?
Definitely not. What you need to do is abstract the jpeg specific code
and take it out of 'CJpegDisk' and 'CJpegRam' into 'CJpegGeneric'. Then
your 'CJpegDisk' would be something like
class CJpegDisk : public CDiskFile, public CJpegGeneric {
and all things from here.
Of course, you'd be much better off if you do all this with "policies",
see "Modern C++ Design" by A. Alexandrescu.
V vr*****@yahoo.com wrote: Suppose we have
class CAbstractFile { ... class CDiskFile : public CAbstractFile { ... class CRamFile : public CAbstractFile { ...
Now i want to derive a new class CJpegFile that implements reading & writing of Jpeg files.
I want that CJpegFile object can be a CDiskFile or a CRamFile, so that we can read/write a jpeg file as well via a diskfile as well via a ramfile.
There are several ways of doing this. You need to think about how you
want to use the files.
I don't think that the methods of JpegFile include Read and Write.
Probably what you want to do with a JpegFile is things like Open,
Display, Print, ConvertToPng etc. So JpegFile is not a subclass of
AbstractFile. But it is *implemented* using an AbstractFile. So you
could have
class JpegFile {
private:
AbstractFile* file;
public:
JpegFile(char* fn): file(new DiskFile(fn) {...}
// Add a constructor to DiskFile that takes a filename
JpegFile(void* address): file(new RamFile(address) {...}
// Add a constructor to RamFile
~JpegFile() {delete file;}
// AbstractFile needs a virtual destructor
void print();
....
};
Does this help?
--Phil. vr*****@yahoo.com wrote: Hi,
Suppose we have
class CAbstractFile { };
class CDiskFile : public CAbstractFile { };
class CRamFile : public CAbstractFile { };
Now i want to derive a new class CJpegFile that implements reading & writing of Jpeg files.
I want that CJpegFile object can be a CDiskFile or a CRamFile, so that we can read/write a jpeg file as well via a diskfile as well via a ramfile.
How should this be done in good C++?
Personally, I think this may be more of a design issue than a language
issue. It seems to me that the hierarchy you've built is a hierarchy of
file *systems*. JPEG is a file *format*; maybe it doesn't belong in
this hierarchy. How about something like:
class CJpegProcessor
{
return_type Read(CAbstractFile &, CImage &);
return_type Write(CAbstractFile &, CImage &);
};
where CImage is some image class that you've defined to store the image
data? CJpegProcessor "knows" what a JPEG file looks like and contains,
but it doesn't need to know anything about the vagaries of writing data
to disk vs. RAM vs. (say) flash, the way the classes in your hierarchy do.
--
Mike Smith
I think you and Phil are essentially saying the same thing.
And i agree with you that it's much better to do it this way, i.e. give
CJpegFile a pointer/reference to a CAbstractFile,
so it can use that to read/write a file, whatever type that is.
Thanks for your enlightening help guys!
Cheers,
Vrish
> Of course, you'd be much better off if you do all this with "policies", see "Modern C++ Design" by A. Alexandrescu.
Interesting link, thanks Victor.
Cheers,
Vrish This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: KK |
last post by:
Windows Forms Inheritance, Incomplete?
I was playing around with Windows Forms
and found out this Forms Inheritance feature.
The moment I saw that, I felt this can be
used effectively if the...
|
by: KK |
last post by:
** Posting it here cause after couple of days no body responded.**
I was playing around with Windows Forms
and found out this Forms Inheritance feature.
The moment I saw that, I felt this can...
|
by: Dave Theese |
last post by:
Hello all,
The example below demonstrates proper conformance to the C++ standard.
However, I'm having a hard time getting my brain around which language rules
make this proper...
The error...
|
by: __PPS__ |
last post by:
Hello everybody, today I had another quiz question
"if class X is privately derived from base class Y what is the scope of
the public, protected, private members of Y will be in class X"
By...
|
by: Matthew Louden |
last post by:
I want to know why C# doesnt support multiple inheritance? But why we can
inherit multiple interfaces instead? I know this is the rule, but I dont
understand why. Can anyone give me some concrete...
|
by: Ben Blank |
last post by:
I'm writing a family of classes which all inherit most of their methods and
code (including constructors) from a single base class. When attempting to
instance one of the derived classes using...
|
by: VR |
last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some
WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages,
my MasterPage does not have a form tag itself...
|
by: Noah Roberts |
last post by:
Is there anything that says that if you virtually inherit from one
class you have to virtually inherit from anything you inherit from?
|
by: RSH |
last post by:
I have a simple question regarding inheritance in a web form.
I have a DropDownList in an aspx form. It is called DropDownList1
I have a class that will be overriding the render event so I...
|
by: RSH |
last post by:
Hi,
I am working on some general OOP constructs and I was wondering if I could
get some guidance.
I have an instance where I have a Base Abstract Class, and 4 Derived
classes. I now need to...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |