473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

~ (abstract class instance)

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
8 2058
* 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
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
* 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
* 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
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
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
* 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Murat Tasan | last post by:
so here is another general question about java... why can't you declare an abstract static method. i can envision the case (indeed i have experienced the case) where one would want an...
4
by: Tony Johansson | last post by:
Hello! Assume you have an abstract class called Body and a derived class called cylinder. When you have an abstract class you can't instansiate an object. As you can see in the abstract class...
12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
6
by: Steve | last post by:
I am designing a new class hierarchy and I've already run into a bit of a bump in the road. I have this structure so far; class CodeGen class CodeGenHeader : CodeGen class CodeGenProtocolHeader...
0
by: Craig Buchanan | last post by:
i am trying to build an application that uses plugins to extend the business functionality of an application. i've decided to use inheritance of abstract classes as the mechanism to do this. ...
18
by: Vedo | last post by:
ref struct XXX abstract sealed { literal int A = 5; }; The definition above gives me the compiler warning "C4693: a sealed abstract class cannot have any instance members 'A'". The equivalent...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.