What is better if you want upcasting in intermediate classes like below?
Multiple Inheritance and Overloading or simply RTTI?
RTTI wants time but MI and Overloading create big objects
(because of virtual) and finally they need time too to access objects
inside big object.
(Examples looks complicated but are very simple)
Paradigm #1: MI & Overloading
=============== ==============
//--------------- base classes
class Position { public: virtual size_t tell() = 0; };
class InputStream : virtual public Position { public: virtual size_t
read(const void *base, size_t size) = 0; };
class OutputStream : virtual public Position { public: virtual size_t
write(const void *base, size_t size) = 0; };
class Seekable { public: virtual size_t size() = 0;
public: virtual void seek(size_t position) = 0; };
//--------------- intermediate classes
class SeekableInputSt ream : public InputStream, virtual public Seekable {};
class SeekableOutputS tream : public OutputStream, virtual public
Seekable {};
class SeekableStream : virtual public SeekableInputSt ream, virtual
public SeekableOutputS tream {};
//--------------- most derived and fully implemented classes
class FileInputStream : public virtual SeekableInputSt ream { ..... };
class FileOutputStrea m : public virtual SeekableOutputS tream { ..... };
class FileStream : public FileInputStream , public FileOutputStrea m,
virtual public SeekableStream { ...... };
//--------------- overloaded functions
void ZipFileSystem:: attachFileSyste m(SeekableInput Stream &sis);
void ZipFileSystem:: attachFileSyste m(SeekableStrea m &ss);
Paradigm #2: RTTI
=============== ==
//--------------- base classes
class Position { public: virtual size_t tell() = 0; };
class InputStream { public: virtual size_t read(const void *base, size_t
size) = 0; };
class OutputStream { public: virtual size_t write(const void *base,
size_t size) = 0; };
class Seekable { public: virtual size_t size() = 0;
public: virtual void seek(size_t position) = 0; };
//--------------- there are no intermediate classes
//--------------- most derived and fully implemented classes
class FileInputStream : virtual public Position, virtual public
Seekable, public InputStream { ..... };
class FileOutputStrea m : virtual public Position, virtual public
Seekable, public OutputStream { ..... };
class FileStream : public FileInputStream , public FileOutputStrea m {
....... };
//--------------- overloaded functions
void ZipFileSystem:: attachFileSyste m(Seekable &s)
{
// by definition:
// Seekable object CONTAINS Position object AND (InputStream object OR
OutputStream object)
if (!dynamic_cast< InputStream*(&s )) throw EXCEPTION; // if contains
only OutputStream
if (dynamic_cast<O utputStream*(&s )) { ...... } else { .......}
} 3 2038
* Chameleon:
What is better if you want upcasting in intermediate classes like below?
Multiple Inheritance and Overloading or simply RTTI?
Not a meaningful question. Upcasting is implicit in C++. You can use a
static_cast to be clear about it.
RTTI wants time but MI and Overloading create big objects
(because of virtual)
No, that's incorrect.
and finally they need time too to access objects
inside big object.
No.
(Examples looks complicated but are very simple)
Paradigm #1: MI & Overloading
=============== ==============
//--------------- base classes
class Position { public: virtual size_t tell() = 0; };
class InputStream : virtual public Position { public: virtual size_t
read(const void *base, size_t size) = 0; };
class OutputStream : virtual public Position { public: virtual size_t
write(const void *base, size_t size) = 0; };
class Seekable { public: virtual size_t size() = 0;
public: virtual void seek(size_t position) = 0; };
Is your intent really that an InputStream *is* a Position?
//--------------- intermediate classes
class SeekableInputSt ream : public InputStream, virtual public Seekable {};
class SeekableOutputS tream : public OutputStream, virtual public
Seekable {};
class SeekableStream : virtual public SeekableInputSt ream, virtual
public SeekableOutputS tream {};
//--------------- most derived and fully implemented classes
class FileInputStream : public virtual SeekableInputSt ream { ..... };
class FileOutputStrea m : public virtual SeekableOutputS tream { ..... };
class FileStream : public FileInputStream , public FileOutputStrea m,
virtual public SeekableStream { ...... };
//--------------- overloaded functions
void ZipFileSystem:: attachFileSyste m(SeekableInput Stream &sis);
void ZipFileSystem:: attachFileSyste m(SeekableStrea m &ss);
Not sure what you're trying to do but it's a mess. Perhaps you've
modelled this on standard iostream and inherited the mess from there?
Offer interfaces rather than let a stream really be two streams.
Paradigm #2: RTTI
=============== ==
//--------------- base classes
class Position { public: virtual size_t tell() = 0; };
class InputStream { public: virtual size_t read(const void *base, size_t
size) = 0; };
class OutputStream { public: virtual size_t write(const void *base,
size_t size) = 0; };
class Seekable { public: virtual size_t size() = 0;
public: virtual void seek(size_t position) = 0; };
//--------------- there are no intermediate classes
//--------------- most derived and fully implemented classes
class FileInputStream : virtual public Position, virtual public
Seekable, public InputStream { ..... };
class FileOutputStrea m : virtual public Position, virtual public
Seekable, public OutputStream { ..... };
class FileStream : public FileInputStream , public FileOutputStrea m {
...... };
Again, are you sure that e.g. a FileInputStream *is* a Position?
And again, are you sure you want one logical stream to really be two
streams?
//--------------- overloaded functions
void ZipFileSystem:: attachFileSyste m(Seekable &s)
{
// by definition:
// Seekable object CONTAINS Position object AND (InputStream object OR
OutputStream object)
?
if (!dynamic_cast< InputStream*(&s )) throw EXCEPTION; // if contains
only OutputStream
if (dynamic_cast<O utputStream*(&s )) { ...... } else { .......}
}
Evidently you want 'attachFileSyst em' to behave differently depending on
whether it's dealing with an input stream or an output stream. That
means you're really dealing with two different functions. So just make
them two different functions.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
sA: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Chameleon <ch******@hotma il.comwrote:
What is better if you want upcasting in intermediate classes like below?
Multiple Inheritance and Overloading or simply RTTI?
Given only the two examples to choose from, I'd go with example 1. I
don't like either very much though.
(Examples looks complicated but are very simple)
When simple stuff looks complicated, then there is probably something
wrong. Of course idiomatic use of newlines and indentation would
probably help quite a bit.
Paradigm #1: MI & Overloading
=============== ==============
//--------------- base classes
class Position { public: virtual size_t tell() = 0; };
class InputStream : virtual public Position { public: virtual size_t
read(const void *base, size_t size) = 0; };
class OutputStream : virtual public Position { public: virtual size_t
write(const void *base, size_t size) = 0; };
class Seekable { public: virtual size_t size() = 0;
public: virtual void seek(size_t position) = 0; };
//--------------- intermediate classes
class SeekableInputSt ream : public InputStream, virtual public Seekable {};
class SeekableOutputS tream : public OutputStream, virtual public
Seekable {};
class SeekableStream : virtual public SeekableInputSt ream, virtual
public SeekableOutputS tream {};
//--------------- most derived and fully implemented classes
class FileInputStream : public virtual SeekableInputSt ream { ..... };
class FileOutputStrea m : public virtual SeekableOutputS tream { ..... };
class FileStream : public FileInputStream , public FileOutputStrea m,
virtual public SeekableStream { ...... };
//--------------- overloaded functions
void ZipFileSystem:: attachFileSyste m(SeekableInput Stream &sis);
void ZipFileSystem:: attachFileSyste m(SeekableStrea m &ss);
Paradigm #2: RTTI
=============== ==
//--------------- base classes
class Position { public: virtual size_t tell() = 0; };
class InputStream { public: virtual size_t read(const void *base, size_t
size) = 0; };
class OutputStream { public: virtual size_t write(const void *base,
size_t size) = 0; };
class Seekable { public: virtual size_t size() = 0;
public: virtual void seek(size_t position) = 0; };
//--------------- there are no intermediate classes
//--------------- most derived and fully implemented classes
class FileInputStream : virtual public Position, virtual public
Seekable, public InputStream { ..... };
class FileOutputStrea m : virtual public Position, virtual public
Seekable, public OutputStream { ..... };
class FileStream : public FileInputStream , public FileOutputStrea m {
...... };
//--------------- overloaded functions
void ZipFileSystem:: attachFileSyste m(Seekable &s)
{
// by definition:
// Seekable object CONTAINS Position object AND (InputStream object OR
OutputStream object)
if (!dynamic_cast< InputStream*(&s )) throw EXCEPTION; // if contains
only OutputStream
if (dynamic_cast<O utputStream*(&s )) { ...... } else { .......}
}
>What is better if you want upcasting in intermediate classes like below?
>Multiple Inheritance and Overloading or simply RTTI?
Not a meaningful question. Upcasting is implicit in C++. You can use a
static_cast to be clear about it.
>RTTI wants time but MI and Overloading create big objects (because of virtual)
No, that's incorrect.
>and finally they need time too to access objects inside big object.
No.
>(Examples looks complicated but are very simple)
Paradigm #1: MI & Overloading ============== ===============
//--------------- base classes class Position { public: virtual size_t tell() = 0; }; class InputStream : virtual public Position { public: virtual size_t read(const void *base, size_t size) = 0; }; class OutputStream : virtual public Position { public: virtual size_t write(const void *base, size_t size) = 0; }; class Seekable { public: virtual size_t size() = 0; public: virtual void seek(size_t position) = 0; };
Is your intent really that an InputStream *is* a Position?
Position is the interface of giving the stream pointer's position. Any
stream (InputStream, OutputStream and derived) want to implement this.
No. Stream is not a Position but contains the complete functionality of
Position.
>//--------------- intermediate classes class SeekableInputSt ream : public InputStream, virtual public Seekable {}; class SeekableOutputS tream : public OutputStream, virtual public Seekable {}; class SeekableStream : virtual public SeekableInputSt ream, virtual public SeekableOutputS tream {}; //--------------- most derived and fully implemented classes class FileInputStream : public virtual SeekableInputSt ream { ..... }; class FileOutputStrea m : public virtual SeekableOutputS tream { ..... }; class FileStream : public FileInputStream , public FileOutputStrea m, virtual public SeekableStream { ...... }; //--------------- overloaded functions void ZipFileSystem:: attachFileSyste m(SeekableInput Stream &sis); void ZipFileSystem:: attachFileSyste m(SeekableStrea m &ss);
Not sure what you're trying to do but it's a mess. Perhaps you've
modelled this on standard iostream and inherited the mess from there?
Offer interfaces rather than let a stream really be two streams.
No. Classes are exactly as is in sample code. There are more most
derived classes although. (like Bz2DecompressIn putStream)
>Paradigm #2: RTTI ============== ===
//--------------- base classes class Position { public: virtual size_t tell() = 0; }; class InputStream { public: virtual size_t read(const void *base, size_t size) = 0; }; class OutputStream { public: virtual size_t write(const void *base, size_t size) = 0; }; class Seekable { public: virtual size_t size() = 0; public: virtual void seek(size_t position) = 0; }; //--------------- there are no intermediate classes //--------------- most derived and fully implemented classes class FileInputStream : virtual public Position, virtual public Seekable, public InputStream { ..... }; class FileOutputStrea m : virtual public Position, virtual public Seekable, public OutputStream { ..... }; class FileStream : public FileInputStream , public FileOutputStrea m { ...... };
Again, are you sure that e.g. a FileInputStream *is* a Position?
No. Contains a Position.
And again, are you sure you want one logical stream to really be two
streams?
Yes. With this, in a function with an OutputStream parameter, I can pass
an OutputStream, a SeekableOutputS tream, a SeekableStream etc because
all of them contain the OutputStream functionality.
I am trying the streams contain the most possible shared code.
>//--------------- overloaded functions void ZipFileSystem:: attachFileSyste m(Seekable &s) { // by definition: // Seekable object CONTAINS Position object AND (InputStream object OR OutputStream object)
?
> if (!dynamic_cast< InputStream*(&s )) throw EXCEPTION; // if contains only OutputStream if (dynamic_cast<O utputStream*(&s )) { ...... } else { .......} }
Evidently you want 'attachFileSyst em' to behave differently depending on
whether it's dealing with an input stream or an output stream. That
means you're really dealing with two different functions. So just make
them two different functions.
I take the point. Thanks. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Torsten Curdt |
last post by:
Let's assume I have a base class
class X
{
};
and the the following classes inheriting
class BX : public X
{
|
by: Rick |
last post by:
Hi,
I wrote a few classes recently and am writing a small GC implementation
for C++. I will be implementing my own gc_ptr type maintain a list where
I'll store pointers to allocated memory. I was wondering if it is
possible to identify objects during runtime via RTTI because I don't
want to be doing:
gc_ptr<int>::doGC();
gc_ptr<double>::doGC();
|
by: Kleidemos |
last post by:
If I implement a simple RTTI system, more simple than C++ RTTI system
for my program and this system is plus or minus:
#define DEF_RTTI_BASE(name) virtual inline const char *Name(){ return
#name; }
#define DEF_RTTI(name) inline const char *Name(){ return #name; }
And(for example)
class CProva
|
by: Agoston Bejo |
last post by:
Hello there,
I would like to know what overheads there are to think of when using RTTI. I
understand that enabling RTTI increases the sizes of the classes, but not
the objects themselves. This doesn't sound too bad.
What I'm worried about is whether there is a general performance overhead
caused by enabling RTTI, such as enabling exceptions makes a C++ program
slower even when there are no exceptions actually used.
Are there similar...
|
by: deancoo |
last post by:
I'm having some trouble with a couple of classes I've created (base and
derived). I had defined a function in a derived class, and then overloaded
that function name in a class derived from the first derived class. Now I
need to store objects instantiated from these classes (excluding the base
class) in a container. The container is defined to hold objects of the base
class type. So far so good, however, when it comes time to use the...
| |
by: denny |
last post by:
Hey all,
I know that dynamic_cast<> takes some time, but , for instance, is there a memoy cost associated in with it? Does it have to maintain a table in memory, thus bloating the runtime ram needs of my dll? Does it bloat the actual download size - would my dll be smaller without it?
thanks - I'm using rtti in some instances, but I jsut want to know if it's costing me hundreds of K in extra download.
-denny-
|
by: dotNeter |
last post by:
I'm studying the RTTI, and my current work is concern for how to get
the self-defined type at runtime, that's exactly what the RTTI does.
I mean, in my application, I built several self-defined data types, so
I have to implement the RTTI by myself.
I need a simple and effective example to help me decide how to design.
Can someon help me?
|
by: Chameleon |
last post by:
I know than dynamic_cast check string name of derived to base class and
if one of them match, return the pointer of that object or else zero.
I suppose, I dynamic_cast instead of strings, checks integers, then this
procedure will be more fast, so I create something like this:
---------------------------------------------
class A
{
public:
|
by: [rob desbois] |
last post by:
Hi all, I have a set of classes which implement the virtual
constructor idiom.
I had a slicing problem which resulted when I forgot to override the
clone() function in a derived class.
Is there something (other than documentation) that I can do to prevent
this from happening again?
TIA,
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |