~ (abstract class instance)

A

al pacino

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
 
A

Alf P. Steinbach

* 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

al pacino

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.
 
A

Alf P. Steinbach

* 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

Alf P. Steinbach

* 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; }
 
I

Ian Collins

al said:
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.
 
D

Default User

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
 
A

Alf P. Steinbach

* al pacino in private correspondence:
"""> > 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... :)
 
B

benben

al said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top