Wide use of virtual functions

This is a discussion on Wide use of virtual functions within the C++ Programming forums, part of the General Programming Boards category; I'm learned C and C++ first, and only learned java because my university required me to do so. Other people ...

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    Wide use of virtual functions

    I'm learned C and C++ first, and only learned java because my university required me to do so. Other people at my university learned Java first, and to me, they have a very typical "Java mindset" even when developing in C++.

    I might be wrong though, so I'm asking what you guys think.
    Thing is, they are using abstract classes with virtual functions, in order to create "interfaces"(like in java), that their other classes should implement. They do this for classes that they are not even sure will need it, but they do it "just in case" they will need to be virtual in the future.

    Also, they frequently use classes and functions such as "factories" for creating classes etc.

    I dunno, maybe I'm just too influenced by C-programming. But is this really good C++ programming practice?
    Last edited by Drogin; 02-24-2012 at 04:05 AM.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    They do this for classes that they are not even sure will need it, but they do it "just in case" they will need to be virtual in the future.
    If the interface is designed intuitively, this is a good practice... imho.It makes it almost trivial to all more backends later on ..just by modifying the factory.
    Also, they frequently use classes and functions such as "factories" for creating classes etc.
    Don't you mean "for creating objects" ?If yes, that is a commonly used OOP design pattern.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Quote Originally Posted by Drogin
    they are using abstract classes with virtual functions, in order to create "interfaces"(like in java), that their other classes should implement.
    When used appropriately, this is fine: it is a matter of object oriented design that separates interface from implementation.

    Quote Originally Posted by Drogin
    They do this for classes that they are not even sure will need it, but they do it "just in case" they will need to be virtual in the future.
    This reminds me of YAGNI. Abstractions are useful, but creating unnecessary abstractions can be a waste of time and may end up needing to be refactored later anyway. The problem is that sometimes you need a crystal ball to determine what abstractions should be created right now.

    Quote Originally Posted by Drogin
    Also, they frequently use classes and functions such as "factories" for creating classes etc.
    What do you find questionable about that?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    Quote Originally Posted by laserlight View Post
    This reminds me of YAGNI. Abstractions are useful, but creating unnecessary abstractions can be a waste of time and may end up needing to be refactored later anyway. The problem is that sometimes you need a crystal ball to determine what abstractions should be created right now.
    I agree.

    The rule of thumb I work by is that I only factor out abstractions (into interfaces, or base classes, or whatever) when I have at least two (preferably more than two) distinct classes that need the same abstraction.

    The fact I have at least two distinct classes needing the same abstraction is often a evidence for the value of easing out that abstraction and representing it in a separate class or interface. Having more than two distinct classes needing that abstraction is preferable, as it provides more evidence about what needs to be captured in that abstraction. Even better if those distinct classes are concrete .... i.e. instances need to be instantiated by the program, as it shows that those abstractions are more widely meaningful to the program.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Thing is, they are using abstract classes with virtual functions, in order to create "interfaces"(like in java), that their other classes should implement. They do this for classes that they are not even sure will need it, but they do it "just in case" they will need to be virtual in the future.
    For the most part this is a good practice. Each situation is different and cluttering the code with tons of useless interfaces is not good but sometimes that also makes the code more robust and easier to extend later.
    You can never go wrong with interfaces and virtual functions unless of course your profiler tells you that is where your bottleneck is. A good practice is always code to an interface rather than an implementation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. virtual functions, when to use them. Or not.
    By Subsonics in forum C++ Programming
    Replies: 7
    Last Post: 09-29-2010, 03:10 PM
  2. virtual functions
    By lord in forum C++ Programming
    Replies: 9
    Last Post: 10-18-2008, 02:55 PM
  3. Virtual Functions
    By vb.bajpai in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2007, 12:38 PM
  4. Virtual Functions
    By warfang in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2007, 11:14 PM
  5. Virtual functions but non-virtual destructor
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2007, 11:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21