~ (abstract class instance)

Discussion in 'C++' started by al pacino, Apr 8, 2006.

  1. al pacino

    al pacino Guest

    hi,
    i want to know how does c++ compiler prohibits the creation of instance
    of an abstract class?

    i think it must be checking on the vtable entry if there is an entry
    with zero(indicting abstract class) but exactly how it is done.
    thanks in advance
     
    al pacino, Apr 8, 2006
    #1
    1. Advertising

  2. * al pacino:
    > i want to know how does c++ compiler prohibits the creation of instance
    > of an abstract class?


    By prohibiting the creation of an instance... :)


    > i think it must be checking on the vtable entry if there is an entry
    > with zero(indicting abstract class) but exactly how it is done.


    No, on two counts. First, at compilation time there is no vtable, even
    for a vtable-based compiler (which they all are, in practice): the
    compiler uses the source code's definition of the class. Second, the
    vtable entry for a pure virtual function is in general the address of
    some error handling function that cries out "runtime error" or some
    such, instead of 0, even though this mechanism is often explained using
    a 0 value /as an example/.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Apr 8, 2006
    #2
    1. Advertising

  3. al pacino

    al pacino Guest

    hi,
    i wanted to know how it prohibits the creation of
    the instance.
    how does it decides that since this class is abstract
    any instance in source code is illegal and no memory is allocated for
    that class etc etc.
     
    al pacino, Apr 8, 2006
    #3
  4. * al pacino:

    > i wanted to know how it prohibits the creation of
    > the instance.


    Please quote what you're replying to.

    Anyway, I don't understand the question.

    Perhaps you're wondering what error message you get (if any)?

    That depends on your compiler.

    Try the following code with your most favorite compiler:

    struct Huh { virtual foo() = 0; };
    int main() { Huh x; }


    > how does it decides that since this class is abstract


    If a class has a pure virtual function, like Huh above, then it's
    abstract at the language level.


    > any instance in source code is illegal and no memory is allocated for
    > that class etc etc.


    Well, that's not strictly true.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Apr 8, 2006
    #4
  5. * Alf P. Steinbach:
    >
    > struct Huh { virtual foo() = 0; };
    > int main() { Huh x; }


    Sorry, should be

    struct Huh { virtual void foo() = 0; };
    int main() { Huh x; }

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Apr 8, 2006
    #5
  6. al pacino

    Ian Collins Guest

    al pacino wrote:
    > hi,
    > i wanted to know how it prohibits the creation of
    > the instance.

    By emitting an error and exiting.

    > how does it decides that since this class is abstract
    > any instance in source code is illegal and no memory is allocated for
    > that class etc etc.
    >

    It knows the class has pure virtual methods that haven't been overloaded.

    --
    Ian Collins.
     
    Ian Collins, Apr 8, 2006
    #6
  7. al pacino

    Default User Guest

    al pacino wrote:

    Please see my .sig for valuable information.

    > hi,
    > i wanted to know how it prohibits the creation of
    > the instance.
    > how does it decides that since this class is abstract
    > any instance in source code is illegal and no memory is allocated for
    > that class etc etc.


    How does it decide that you trying to pass the wrong number of
    arguments to a function? The compiler has all the information it needs
    to make that decision, that is the declaration of the class, and is
    required to issue a diagnostic if there is a subsequent attempt to
    create an instance.



    Brian

    --
    If televison's a babysitter, the Internet is a drunk librarian who
    won't shut up.
    -- Dorothy Gambrell (http://catandgirl.com)
     
    Default User, Apr 8, 2006
    #7
  8. * al pacino in private correspondence:
    > Alf P. Steinbach wrote:
    >> * al pacino:
    >>
    >>> i wanted to know how it prohibits the creation of
    >>> the instance.

    >> Please quote what you're replying to.
    >>
    >> Anyway, I don't understand the question.
    >>
    >> Perhaps you're wondering what error message you get (if any)?
    >>
    >> That depends on your compiler.
    >>
    >> Try the following code with your most favorite compiler:
    >>
    >> struct Huh { virtual foo() = 0; };
    >> int main() { Huh x; }
    >>
    >>
    >>> how does it decides that since this class is abstract

    >> If a class has a pure virtual function, like Huh above, then it's
    >> abstract at the language level.
    >>
    >>
    >>> any instance in source code is illegal and no memory is allocated for
    >>> that class etc etc.

    >> Well, that's not strictly true.
    >>

    >
    > """> > any instance in source code is illegal and no memory is
    > allocated for"""
    > """> Well, that's not strictly true.""""
    > please elobrate more on that.


    Please reply in the newsgroup. That way others can benefit from the
    discussion, my mailbox doesn't fill up :), and not the least, others
    can criticize and participate in the discussion, e.g. correcting me...

    Regarding "instance in source code", you can form an instance of an
    abstract class by using it as a base class for a concrete class.

    struct Abstract { virtual void foo() = 0; };
    struct Concrete: Abstract { void foo() {} };

    int main(){ Concrete c; Abstract& a = c; }

    Here 'a' refers to the (now concrete) Abstract part of 'c'.

    Probably what was meant wherever you picked up that, is that an abstract
    class cannot be instantiated directly: it can only be instantiated as
    part of a concrete class.

    Regarding "no memory is allocated for", I'm not sure what's meant, but
    it sounds wrong... :)

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Apr 8, 2006
    #8
  9. al pacino

    benben Guest

    al pacino wrote:
    > hi,
    > i wanted to know how it prohibits the creation of
    > the instance.
    > how does it decides that since this class is abstract
    > any instance in source code is illegal and no memory is allocated for
    > that class etc etc.
    >


    Just in case you are thinking that prohibition of creation of an
    instance of an abstract type is done by runtime, it is actually done by
    compile time.

    So if you have an abstract class:

    class shape
    {
    public:
    virtual std::string name() = 0;
    virtual ~shape(){}
    };

    And somewhere else you have a line like:

    shape s;

    The compiler looks up the information it just collected from parsing the
    the definition of class shape and finds out that it is an abstract
    class. Then the compiler issues an error message and the compilation
    fails. Basically if you try to instantiate an abstract class you won't
    even get the executable file generated, so never mind what happens when
    you run it.

    Regards,
    Ben
     
    benben, Apr 8, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler
    Replies:
    1
    Views:
    457
    R.F. Pels
    May 22, 2005
  2. Sameer
    Replies:
    4
    Views:
    613
    Roedy Green
    Aug 31, 2005
  3. Uzytkownik
    Replies:
    3
    Views:
    601
    Uzytkownik
    Apr 3, 2005
  4. Iyer, Prasad C

    Abstract Methods & Abstract Class

    Iyer, Prasad C, Oct 20, 2005, in forum: Python
    Replies:
    0
    Views:
    541
    Iyer, Prasad C
    Oct 20, 2005
  5. Replies:
    4
    Views:
    840
    Rolf Magnus
    May 17, 2006
Loading...

Share This Page