By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

~ (abstract class instance)

P: n/a
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

Apr 8 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
* 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?
Apr 8 '06 #2

P: n/a
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.

Apr 8 '06 #3

P: n/a
* 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?
Apr 8 '06 #4

P: n/a
* 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?
Apr 8 '06 #5

P: n/a
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.
Apr 8 '06 #6

P: n/a
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)
Apr 8 '06 #7

P: n/a
* 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?
Apr 8 '06 #8

P: n/a
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
Apr 8 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.