Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ~ (abstract class instance)

Reply
Thread Tools

~ (abstract class instance)

 
 
al pacino
Guest
Posts: n/a
 
      04-08-2006
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-08-2006
* 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?
 
Reply With Quote
 
 
 
 
al pacino
Guest
Posts: n/a
 
      04-08-2006
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.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-08-2006
* 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?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-08-2006
* 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?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-08-2006
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.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      04-08-2006
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)
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-08-2006
* 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?
 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      04-08-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Generic class literals - e.g,, Class<Map<String, Integer>>.class Purush Java 4 04-13-2005 08:40 PM
instanciate a class in a jar file with class.forname, while my main class is in another jar cyril Java 2 08-25-2004 06:55 AM



Advertisments