C++ stuff you _never_ use?
-
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 use Virtual Base Classes all the time. Usually when there is a common interface or some common code between classes (which can all be manipulated by other things) but where the base class itself doesn't make sense on its own. For example, say you have a cross-platform password changer. You might have a base class CPlatform: class CPlatform { public: CPlatform(); virtual ~CPlatform(); void RemoveAllAccoutns(void); virtual bool DetectAccounts(void) = 0; virtual bool ChangePasswords(const char *old, const char *new) = 0; protected: CAccountList m_accounts; }; Some other code could take a list of classes derived from CPlatform and, without caring which platforms they represent or if they are all the same platform, call DetectAccounts and ChangePasswords on all of them. The RemoveAllAccounts function is the same for all platform classes because it'll just do something like m_accounts.RemoveAll(), but DetectAccounts and ChangePassword will be completely different for each platform class which is derived from the base class. It makes no sense at all to instantiate the base class because it's incomplete and doesn't do everything a platform class needs to do. Making the unimplemented functions in the base class pure-virtual (so the base class is virtual) makes sure that (a) nobody accidentally instantiates the base class, and (b) all classes derived from the base class implement the required functions (or they are virtual themselves and need further derivation). This is a very real-world example, only slightly simplified from a real program which I have worked on, and I use this technique quite often. Hope that makes sense!
-
I use Virtual Base Classes all the time. Usually when there is a common interface or some common code between classes (which can all be manipulated by other things) but where the base class itself doesn't make sense on its own. For example, say you have a cross-platform password changer. You might have a base class CPlatform: class CPlatform { public: CPlatform(); virtual ~CPlatform(); void RemoveAllAccoutns(void); virtual bool DetectAccounts(void) = 0; virtual bool ChangePasswords(const char *old, const char *new) = 0; protected: CAccountList m_accounts; }; Some other code could take a list of classes derived from CPlatform and, without caring which platforms they represent or if they are all the same platform, call DetectAccounts and ChangePasswords on all of them. The RemoveAllAccounts function is the same for all platform classes because it'll just do something like m_accounts.RemoveAll(), but DetectAccounts and ChangePassword will be completely different for each platform class which is derived from the base class. It makes no sense at all to instantiate the base class because it's incomplete and doesn't do everything a platform class needs to do. Making the unimplemented functions in the base class pure-virtual (so the base class is virtual) makes sure that (a) nobody accidentally instantiates the base class, and (b) all classes derived from the base class implement the required functions (or they are virtual themselves and need further derivation). This is a very real-world example, only slightly simplified from a real program which I have worked on, and I use this technique quite often. Hope that makes sense!
Isn't that an 'abstract base class', where one or more member functions are pure virtual? I agree this is a useful thing, and I use them as well. Virtual base classes are something different, involving avoiding inheriting multiple copies of a common base class in multiple inheritance. i.e. A derives from B and C (multiple inheritance), but both B and C derive from D; A would inherit two copies of D with normal inheritance rules. There's an example here: http://www.purdue.edu/PUCC/Short-Courses/c.files/p\_07515.html I'd never even seen virtual base class inheritance before, since I avoid multiple inheritance in the first place.
-
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
Please tell me you're joking.....its one of the three legs of object oriented programming. Virtual function in a class allow you to eliminate a ton of 'if/else' code. Basically the object you create KNOWS how to perform a function or method accoding to its own data. It may sound too simple to benefit you, but just last week I restructured someone's code that didn't use classes with virtual functions. I rewrote it and eliminated about 3/4 of the accessor functions (things that get data) and about 100 if/else blocks while shrinking the amount of code needed to perform the same task by about a half (from about 10000 lines down to about 5000 lines). without any fancy rework of any of the other code. Once I'd done that, I was able to add the features I wanted by adding a new class derived off the virtual base class.
-
Isn't that an 'abstract base class', where one or more member functions are pure virtual? I agree this is a useful thing, and I use them as well. Virtual base classes are something different, involving avoiding inheriting multiple copies of a common base class in multiple inheritance. i.e. A derives from B and C (multiple inheritance), but both B and C derive from D; A would inherit two copies of D with normal inheritance rules. There's an example here: http://www.purdue.edu/PUCC/Short-Courses/c.files/p\_07515.html I'd never even seen virtual base class inheritance before, since I avoid multiple inheritance in the first place.
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.
-
Please tell me you're joking.....its one of the three legs of object oriented programming. Virtual function in a class allow you to eliminate a ton of 'if/else' code. Basically the object you create KNOWS how to perform a function or method accoding to its own data. It may sound too simple to benefit you, but just last week I restructured someone's code that didn't use classes with virtual functions. I rewrote it and eliminated about 3/4 of the accessor functions (things that get data) and about 100 if/else blocks while shrinking the amount of code needed to perform the same task by about a half (from about 10000 lines down to about 5000 lines). without any fancy rework of any of the other code. Once I'd done that, I was able to add the features I wanted by adding a new class derived off the virtual base class.
Virtual functions and virtual base class inheritance are two very different things. Virtual functions are, as you say, vital. Virtual base class inheritance is to do with resolving multiple inclusions of the same base class in multiple inheritance. I doubt you really have a virtual base class; you just have a base class with a virtual member function. You have to be very precise with terminology of programming languages.
-
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
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.
-
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