Thread: Wide use of virtual functions

  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 05:05 AM.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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