CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2002
    Location
    India
    Posts
    26

    Abstract Class problem

    CMy is a abstract class. As per abstract class definition we can not make any instance of that. But it is not giving any error in MSDEV IDE.

    #include <iostream.h>
    class CMy
    {
    public:
    CMy(){}
    CMy(int i)
    {
    this->i=i;
    };

    ~CMy()
    {
    };
    virtual void show() const =0;

    int i;
    };

    void show(CMy &myObject)
    {
    myObject.i = 7;
    }

    void main(int argc, char* argv[])
    {
    CMy(9);
    show(CMy(6));
    }

  2. #2
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661
    well sionce the class has a constructor and a destructor it's not abstract.
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  3. #3
    Join Date
    Nov 2002
    Location
    Ufa, Russia
    Posts
    36
    well sionce the class has a constructor and a destructor it's not abstract.
    But there is a pure virtual function. AFAIK it's meaning that CMy is a abstract class.

  4. #4
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661
    Originally posted by deesan


    But there is a pure virtual function. AFAIK it's meaning that CMy is a abstract class.
    Okay. You CAN call it abstract but the constructor and destuctor allow you to create it.
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  5. #5
    Join Date
    Nov 2002
    Location
    Ufa, Russia
    Posts
    36
    We don't need to declare constructors and destructors for all classes.
    If some class don't have one of them, then compiler will generate default constructor (destructor)

    So if class have not constructor, it's not meaning that we can't create variable of this class.

    Am I wrong???

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    No, no, no, no, no.

    You cannot create an object of abstract class type no matter how many constructors you provide.
    Code:
    // Two things: don't use the ".h" forms of these includes
    // and why is it here anyway?
    #include <iostream.h> 
    
    class CMy
    {
    public:
        CMy(){}
        CMy(int i)
        {
            this->i=i;    // Use an initialiser list
        };  // this semicolon is redundant.
    
        ~CMy() // If you're going to make this class abstract,
                     // you really ought to make this virtual
        {
        }; // this semicolon is redundant.
    
        virtual void show() const =0;
    
        int i; // You really should make data members private
    };
    
    void show(CMy &myObject)
    {
        myObject.i = 7;
    }
    
    void main(int argc, char* argv[])
    {
    CMy(9);
    show(CMy(6));
    }
    It's possible that the compiler optimises away both temporary objects, but it really ought to generate an error. There must be more to this. It's probably something to do with temporaries and references - anyone with a copy of the standard able to explain it? Unless it's a bug in VC++.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Note that if you declare an actual object in main() - e.g. "CMy f(6)", then compilation fails with expected errors about abstract classes.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Apr 2002
    Location
    India
    Posts
    26
    Hi folks,
    I am asking only one thing.
    Is it MSDEV ID(VC++) bug or i could be happened. that was the
    sample program so there could be some flaw but here my concern abt abstract class behavior .
    Thanks for yours input.

    Saurabh

  9. #9
    Join Date
    Dec 2002
    Posts
    1,050
    VC6 seemed to be an issue so I searched and found
    a good discussion here to determine if it is a bug. Slightly different
    from this in syntax( several different methods )but still generating
    a ctor call

    http://www.experts-exchange.com/Prog..._20250027.html

    With VC7 that the correct compiler error is now generated
    for the code you posted here.
    Last edited by mdmd; February 13th, 2003 at 12:57 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured