C++ stuff you _never_ use?
-
From the amount of confusion the terminology has caused here I think it's pretty obvious that most people don't use virtual base classes as they don't even know what they are =) [No, I have never used them, not even in a classroom setting] A couple of other people mentioned they don't use MI. I think MI mixed with templates is a fantastic way of implementing interfaces (ie. ATL), and the thing causes the most concern for me as I consider the c# future... Back to virtual base classes though - isn't that the corner stone of c++'s iostream? My memory says that was the (only!) example used whenever virtual base classes were mentioned??? Have fun, Paul Westcott.
Right you are, Paul. iostream uses VBCs: class istream : virtual public ios class ostream : virtual public ios class iostream : public istream, public ostream This is the case with the old-school classes from iostream.h. I never can make sense of the code in the new-style STL headers (like iostream) since the template and function parameters are given meaningless names. :( --Mike-- http://home.inreach.com/mdunn/ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas." -- Buffy
-
I was just thinking about obscure C++ features. You know, stuff that you only use in classrooms. :) One really odd feature (in my mind, at least) is virtual base classes. I know the syntax, and I know what it does. But I've literally never seen them used in practice. Has anyone used them in a real project? I'm just curious how they're used in actual code (meaning, code not out of a textbook). --Mike-- http://home.inreach.com/mdunn/ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas." -- Buffy
- Virtual base classes: Never used or needed - Covariant return types: Seldomly used or needed - Partial Template Specialization: Never used, seldomly needed - Multiple inheritance: Only used when modeling "interfaces" using Abstract Base Classes
-
I was just thinking about obscure C++ features. You know, stuff that you only use in classrooms. :) One really odd feature (in my mind, at least) is virtual base classes. I know the syntax, and I know what it does. But I've literally never seen them used in practice. Has anyone used them in a real project? I'm just curious how they're used in actual code (meaning, code not out of a textbook). --Mike-- http://home.inreach.com/mdunn/ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas." -- Buffy
1. Virtual base classes? Having a shell context extension to implement and two classes: CContextMenu (public IContextMenu) and CShellExtInit (public IShellExtInit), you'll probably have to write CShellExtension : public CCOntextMenu, public CShellExtInit... Or (this is really used in a large project): having a CDlgProtect (dialog catching exceptions) and CDlgResize (dialog that automates the resizing of controls inside), both derived from MFC's CDialog, the majority of the dialogs are derived from class CDlgProtectResize : public CDlgProtect, public CDlgResize { public: CDlgProtectResize(CWnd * _pWndParent, CModuleManager *_pModMan = NULL); virtual ~CDlgProtectResize(); public: virtual void OnException(); // from CDlgProtect virtual void OnResize(); // from CDlgResize } Am I reaching you?
-
From the amount of confusion the terminology has caused here I think it's pretty obvious that most people don't use virtual base classes as they don't even know what they are =) [No, I have never used them, not even in a classroom setting] A couple of other people mentioned they don't use MI. I think MI mixed with templates is a fantastic way of implementing interfaces (ie. ATL), and the thing causes the most concern for me as I consider the c# future... Back to virtual base classes though - isn't that the corner stone of c++'s iostream? My memory says that was the (only!) example used whenever virtual base classes were mentioned??? Have fun, Paul Westcott.
You're not wrong. :-) This page gives a good example of virtual base classes (how they are used and what they are good for): http://www.programmingjunkies.com/cppbook/Cbook8\_12.htm Am I right that covariant returns just allow a virtual function to be overridden by another function which takes the same arguments but returns a different type, so long as that type derives from (or is the same as) the original function's type? Are there any catches with this? (I found a comment about static typing but there wasn't much context for me to understand what they meant.) Assuming I understand them, covariant returns seem useful (essential) for copy functions.
-
Ah, yes, I think you're right. :-) Oops. I need to brush up on my C++ terminology (I've started reading Stroustrup again :-)). I also completely avoid multiple inheritance. When learning OO at university I was told it's dangerous concept and I tend to agree.
You say that MI (Multiple Inheritance - nothing to do with Tom Cruise) is a dangerous concept - but isn't that the whole idea with C++? You get the freedom and all the 'dangerous' tools. It's your job avoiding shooting yourself in the foot. I think this is where C++ really sets itself apart from most common languages. Christian Skovdal Andersen
-
Right you are, Paul. iostream uses VBCs: class istream : virtual public ios class ostream : virtual public ios class iostream : public istream, public ostream This is the case with the old-school classes from iostream.h. I never can make sense of the code in the new-style STL headers (like iostream) since the template and function parameters are given meaningless names. :( --Mike-- http://home.inreach.com/mdunn/ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas." -- Buffy
Mention that on the moderated C++ newsgroup and people will tell you that you're an idiot...for some reason the people there seem to have lots and lots of time on their hands and don't understand that making things clear is very important for people who are in the trenches trying to create applications.
-
I was just thinking about obscure C++ features. You know, stuff that you only use in classrooms. :) One really odd feature (in my mind, at least) is virtual base classes. I know the syntax, and I know what it does. But I've literally never seen them used in practice. Has anyone used them in a real project? I'm just curious how they're used in actual code (meaning, code not out of a textbook). --Mike-- http://home.inreach.com/mdunn/ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas." -- Buffy
Hey Mike, >...like virtual base classes; I've seen examples of them, but not really used in practice... What do you think COM interfaces are? :rolleyes: Even if you're not writing COM servers, but if you write a COM client and it calls CoCreateInstance() at least once, then you're also using virtual base classes. In addition, the MFC class CDocTemplate is a virtual base class... Plus, the STL uses function objects and template specifications all over the place. Get to know something really well before you bash it. STL's utility comes in handy when you're outside of MFC but you're wishing you had some high-performance list, map, or array (vector) functionality handy. Then again, every interface method is itself a virtual function, too... that's why your ATL class derives directly from the interface and then implements all its methods! Plus, ATL makes widespread use of multiple inheritance, and does it just fine, thank you very much. If you aren't using the 'obscure' parts of C++, then I would give a friendly suggestion to start increasing the variety of applications you develop... Brian Hart Brian
-
Hey Mike, >...like virtual base classes; I've seen examples of them, but not really used in practice... What do you think COM interfaces are? :rolleyes: Even if you're not writing COM servers, but if you write a COM client and it calls CoCreateInstance() at least once, then you're also using virtual base classes. In addition, the MFC class CDocTemplate is a virtual base class... Plus, the STL uses function objects and template specifications all over the place. Get to know something really well before you bash it. STL's utility comes in handy when you're outside of MFC but you're wishing you had some high-performance list, map, or array (vector) functionality handy. Then again, every interface method is itself a virtual function, too... that's why your ATL class derives directly from the interface and then implements all its methods! Plus, ATL makes widespread use of multiple inheritance, and does it just fine, thank you very much. If you aren't using the 'obscure' parts of C++, then I would give a friendly suggestion to start increasing the variety of applications you develop... Brian Hart Brian
CDocTemplate's an abstract base class (a class with one or more pure virtual member functions), not a virtual base class. I don't know a lot about COM, but I doubt it's got much to do with virtual base classes either. Have you made the same mistake as Leo Davidson above? I agree about STL being extremely useful, and I wouldn't call it obscure at all :-)
-
I was just thinking about obscure C++ features. You know, stuff that you only use in classrooms. :) One really odd feature (in my mind, at least) is virtual base classes. I know the syntax, and I know what it does. But I've literally never seen them used in practice. Has anyone used them in a real project? I'm just curious how they're used in actual code (meaning, code not out of a textbook). --Mike-- http://home.inreach.com/mdunn/ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas." -- Buffy
I don't think there is a feature of C++ that I haven't used at some point, including virtual base classes. People say C++ is far too complicated, but every single feature is there for a very good reason, and taking it out really wouldn't make it the general purpose programming language it is. E.g., virtual inheritance is pretty important in a lot of big class hierarchies -- e.g., the iostream hierarchy in the C++ library. If you've never used a feature of C++, and wondering where it gets used, I recommend Stroustrup's 2nd book, called something along the lines of "The Design & Evolution of C++". it explains his reasoning behind every feature.
-
I was just thinking about obscure C++ features. You know, stuff that you only use in classrooms. :) One really odd feature (in my mind, at least) is virtual base classes. I know the syntax, and I know what it does. But I've literally never seen them used in practice. Has anyone used them in a real project? I'm just curious how they're used in actual code (meaning, code not out of a textbook). --Mike-- http://home.inreach.com/mdunn/ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas." -- Buffy
Hi I am using virtual base classes a lot. This is simple example: class AUnknown // this is base "interface" class { Object* m_owner; // pointer to base "object" class }; class IMove : virtual public AUnknown { void move() = 0; }; class IName : virtual public AUnknown { string getName() = 0; void setName(string) = 0; }; // class that implements two interfaces and has only one //instance of m_owner; class CanMoveHasName : public IMove, public IName { }; Mike
-
I was just thinking about obscure C++ features. You know, stuff that you only use in classrooms. :) One really odd feature (in my mind, at least) is virtual base classes. I know the syntax, and I know what it does. But I've literally never seen them used in practice. Has anyone used them in a real project? I'm just curious how they're used in actual code (meaning, code not out of a textbook). --Mike-- http://home.inreach.com/mdunn/ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas." -- Buffy