Thread: What does this code mean?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128

    What does this code mean?

    Code:
     virtual bool isCorrectAnswer(const std::string& answer) const = 0;
    Im guessing that it is a overide-able yes or no which is decided later.

    It checks if the value stored in answer is correct?

    Probably not correct just wondering if anyone could explain

    would this be considered a virtual function or constructor, im guessing function but then again it is assigned value of 0

    (And yes I know the snippet is short)

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The =0 makes the function pure, i.e. the containing class is abstract, as is any derived class unless it has an overriding definition of this function.

    As for the semantics, you can only guess - the name and signature suggest that the function indeed checks the value of answer and returns whether it is correct, but that depends on the implementation. Of course, if this function does anything else, it's grossly misnamed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Do a search for "pure virtual function" and you'll find your answer.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    26
    Code:
    virtual bool isCorrectAnswer(const std::string& answer) const = 0;
    This is a pure virtual function..means if it is declared in a class ,the class becomes an abstract class that means you can't create any object of that class and it will enforce any subsequent classes those are derived from it to implement this function in their own class...

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    it will enforce any subsequent classes those are derived from it to implement this function in their own class...
    No, it won't. I worded my post very precisely.
    A class must define or inherit a concrete implementation of all its pure functions in order not to be abstract. However, if a class doesn't mind being abstract, it doesn't need to override its pure functions. There is a school of thought in object-oriented design (especially in C++) that says that only leaf classes of the hierarchy should ever be concrete.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM