[RESOLVED] Abstract
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] Abstract

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    16

    [RESOLVED] Abstract

    Why does the compiler give me a warning that my class is not abstract if I have a pure virtual function?

    But if I declare my class to be abstract without any pure virtual functions I don't get a warning?

    That is like a "Are you sure..." prompt to ask the user if he really knowns what he is doing.

    Also why have you renamed pure virtual to abstract?
    Share on Google+

  2. #2
    Join Date
    May 2006
    Posts
    22

    Re: Abstract

    This C++/CLI issue: abstract and pure-virtual are slightly different concepts - if a class has a pure-virtual function then is is abstract - and to make this clear to anyone reading the code it is probaly best if you specify this at the class-head (after all the single pure-virtual function that makes the class abstract may be buried several screens down in the class definition).

    But a class can be abstract even if there are no pure-virtual functions - all abstract means is that you have to extend this class if you want to uses - a pure virtual function requires this - but there are other circumstances in which it can arise.

    We gave you the ability to use the 'abstract' keyword instead of '= 0' (and the term' pure virtual function') as abstract is the term that is widely used in OOP for this situation while 'pure virtual function' is a C++-ism.
    Jonathan Caves
    Visual C++ Compiler Team
    Share on Google+

  3. #3
    Join Date
    Dec 2005
    Posts
    16

    Re: Abstract

    Quote Originally Posted by joncaves
    if a class has a pure-virtual function then is is abstract - and to make this clear to anyone reading the code it is probaly best if you specify this at the class-head (after all the single pure-virtual function that makes the class abstract may be buried several screens down in the class definition).
    If someone tries to instantiate my class or derives a class from it without implementing the pure virtual function the compiler gives him an error. And the compiler should only give him the error but not me any warnings.


    Quote Originally Posted by joncaves
    But a class can be abstract even if there are no pure-virtual functions - all abstract means is that you have to extend this class if you want to uses - a pure virtual function requires this - but there are other circumstances in which it can arise.
    If I don't want my class to be instanciated I make the constructor protected. So there is absolutely no need for the abstract keyword.


    Quote Originally Posted by joncaves
    We gave you the ability to use the 'abstract' keyword instead of '= 0' (and the term' pure virtual function') as abstract is the term that is widely used in OOP for this situation while 'pure virtual function' is a C++-ism.
    Google Fight says "pure virtual function" wins over "abstract function" with more than twice as much results ;-)

    http://www.googlefight.com/index.php...ct+function%22
    Share on Google+

  4. #4
    Join Date
    Jun 2006
    Posts
    1

    Re: Abstract

    During the language design process, I did look at existing practices that achieved the same results as 'abstract'. Creating a private virtual destructor is one way to force someone to extend a class. And you've mentioned creating a private constructor.

    The problem with all of these solutions is that they require metadata to be created. Solutions in native C++ allow a member function to never be defined as long as it is not called. Metadata does not allow that.

    Ultimately, as a language designer, I see actual keywords and language features as a much better solution than work arounds and cool language tricks.

    The name 'abstract' comes about because we are targeting the common language runtime. It is the name that is widely used across languages, and has the most cross language understanding. There is no confusion between the difference of an abstract function and a pure virtual function, and as result there is no reason to avoid using the term. Any C++ programmer should know what an abstract function is anyways.

    Hope that makes sense,
    Brandon Bray
    Author of the C++/CLI language
    Share on Google+

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center