Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table? (http://www.velocityreviews.com/forums/t453132-can-abstract-base-class-have-v-table-will-the-pointer-to-virtual-destructor-be-entered-into-the-virtual-table.html)

sojin 04-06-2006 11:11 AM

Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table?
 
Hi all,

I'm a new to c++... and I've some doubts on "virtual" topics.

1) Can abstract base class have V-table?

Here's the way,,

Class CTemp{

CTemp(){};
~CTemp(){};

virtual void display() = 0;

virtual void draw() { cout << " It's ready!!" << endl;}
}

Class CDerived : public CTemp{

CDerived(){};
~CDerived(){};

}

Can this class CTemp have a v-table?

2) Will the virtual destructor be entered into the virtual table?

please give me good directions..

With rgds
soj


marcwentink@hotmail.com 04-06-2006 11:22 AM

Re: Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table?
 
Sojin

> Can this [abstract] class CTemp have a v-table?


Class Ctemp cannot have an object of his type since it is virtual,
hence if there is no class there is no v-table? Right? Or do not I
understand your question?

> 2. Will the virtual destructor be entered into the virtual table?


But you have not declared the destructor virtual to start with?

Marc Wentink


benben 04-06-2006 12:02 PM

Re: Can abstract base class have V-table?, Will the pointer to virtualdestructor be entered into the virtual table?
 
sojin wrote:
> Hi all,
>
> I'm a new to c++... and I've some doubts on "virtual" topics.
>
> 1) Can abstract base class have V-table?
>
> Here's the way,,
>
> Class CTemp{
>
> CTemp(){};
> ~CTemp(){};
>
> virtual void display() = 0;
>
> virtual void draw() { cout << " It's ready!!" << endl;}
> }
>
> Class CDerived : public CTemp{
>
> CDerived(){};
> ~CDerived(){};
>
> }
>
> Can this class CTemp have a v-table?


Yes

>
> 2) Will the virtual destructor be entered into the virtual table?


Yes

>
> please give me good directions..
>
> With rgds
> soj
>


al pacino 04-06-2006 02:00 PM

Re: Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table?
 

sojin wrote:
> Hi all,
>
> I'm a new to c++... and I've some doubts on "virtual" topics.
>
> 1) Can abstract base class have V-table?
>
> Here's the way,,
>
> Class CTemp{
>
> CTemp(){};
> ~CTemp(){};
>
> virtual void display() = 0;
>
> virtual void draw() { cout << " It's ready!!" << endl;}
> }
>
> Class CDerived : public CTemp{
>
> CDerived(){};
> ~CDerived(){};
>
> }
>
> Can this class CTemp have a v-table?
>
> 2) Will the virtual destructor be entered into the virtual table?
>
> please give me good directions..
>
> With rgds
> soj

yes even though the class is abstract it does have a vtable, in that
at least on entry for a function is zero.


Samineni 04-06-2006 02:25 PM

Re: Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table?
 
1. For every class if it contains at least one virtual function (or
pure virtual function) V-TABLE will be formed. In the case of pure
virtual fucntions , the corresponding entry of the VTABLE will be NULL.
When you are trying to instantiate any object of the class, runtime
will check if the VTABLE will have any NULL entries, if it's so it
won't alllow you to create any objects of that class.

2. As per above, virtual destructor also be entered into the virtual
table as it's a virtual function.


al pacino wrote:
> sojin wrote:
> > Hi all,
> >
> > I'm a new to c++... and I've some doubts on "virtual" topics.
> >
> > 1) Can abstract base class have V-table?
> >
> > Here's the way,,
> >
> > Class CTemp{
> >
> > CTemp(){};
> > ~CTemp(){};
> >
> > virtual void display() = 0;
> >
> > virtual void draw() { cout << " It's ready!!" << endl;}
> > }
> >
> > Class CDerived : public CTemp{
> >
> > CDerived(){};
> > ~CDerived(){};
> >
> > }
> >
> > Can this class CTemp have a v-table?
> >
> > 2) Will the virtual destructor be entered into the virtual table?
> >
> > please give me good directions..
> >
> > With rgds
> > soj

> yes even though the class is abstract it does have a vtable, in that
> at least on entry for a function is zero.



red floyd 04-06-2006 04:07 PM

Re: Can abstract base class have V-table?, Will the pointer to virtualdestructor be entered into the virtual table?
 
sojin wrote:
> Hi all,
>
> I'm a new to c++... and I've some doubts on "virtual" topics.
>
> 1) Can abstract base class have V-table?


There is nothing in the Standard that mandates virtual functions be
provided via vtable. It's an implementation detail. Therefore the
answer to this question is undefined. Granted, all current
implementations use a vtable, but it is not required. Assuming a vtable
implementation, the answer is it will *always* have a vtable.

> 2) Will the virtual destructor be entered into the virtual table?
>


As there is no requirement for a vtable, the answer to this question is
undefined. However, assuming a vtable implementation, the answer is yes.

sojin 04-06-2006 04:08 PM

Re: Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table?
 
Thanks for your reply.... my queries ends here....
now i'm digging to the deep....
sojin....


Kaz Kylheku 04-06-2006 05:19 PM

Re: Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table?
 
marcwentink@hotmail.com wrote:
> Sojin
>
> > Can this [abstract] class CTemp have a v-table?


Note that vtables are implementation artifacts. They are not defined by
the C++ language.

> Class Ctemp cannot have an object of his type since it is virtual,
> hence if there is no class there is no v-table?


There certainly is a class. You might thing that since CTemp cannot be
instantiated by itself, there is no need for a vtable. However, during
the construction of a CDerived object, there is a brief moment during
which the object is considered to be a CTemp, and is not yet a
CDerived.

That happens when the CTemp constructor has been entered, but the
CDerived constructor has not yet been. During this time, if virtual
functions are called, they have to go to CTemp definitions.

This cannot be implemented statically, because a constructor can run
arbitrary code which doesn't know what type the object is:

void LibraryFunction(CTemp *basePtr)
{
basePtr->virtualFunction();
}

CTemp::CTemp()
{
LibraryFunction(this);
}

The virtualFunction() call in LibraryFunction has to
CTemp::virtualFunction even though the object is really a CDerived, and
there is a CDerived::virtualFunction.

LibraryFunction cannot do this by clairvoyance: it has to retrieve some
type-related information from the object, such as a vtable pointer.

Even if CTemp is a pure abstract base class (pure virtuals only) so
that the call to virtualFunction is actually a pure virtual call error,
there still has to be something in the object to detect that run-time
error.

Quite conceivably, if the base class has nothing but pure virtuals,
perhaps the vtable could be optimized away. The vtable pointer in the
partially constructed object could just be set to some special value
(like null). (But then the run-time system would not be able to
provide a diagnostic which distinguishes between a pure virtual call
error, and a virtual call on an object that has been stomped-over with
zero bytes).


Howard 04-06-2006 05:27 PM

Re: Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table?
 

"red floyd" <no.spam@here.dude> wrote in message
news:w1bZf.52519$2O6.37871@newssvr12.news.prodigy. com...
> sojin wrote:
>> Hi all,
>>
>> 2) Will the virtual destructor be entered into the virtual table?
>>

>
> As there is no requirement for a vtable, the answer to this question is
> undefined. However, assuming a vtable implementation, the answer is yes.


And also assuming that the destructor was actually declared as virtual in
the first place! :-) (It wasn't virtual in the code given.)

-Howard



Kaz Kylheku 04-06-2006 05:35 PM

Re: Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table?
 
marcwent...@hotmail.com wrote:
> Sojin
>
> > Can this [abstract] class CTemp have a v-table?


Note that vtables are implementation artifacts. They are not defined by
the C++ language.

> Class Ctemp cannot have an object of his type since it is virtual,
> hence if there is no class there is no v-table?


There certainly is a class. You might think that since CTemp cannot be
instantiated by itself, there is no need for a vtable. However, during
the construction of a CDerived object, there is a brief moment during
which the object is considered to be a CTemp, and is not yet a
CDerived.

That happens when the CTemp constructor has been entered, but the
CDerived constructor has not yet been. During this time, if virtual
functions are called, they have to go to CTemp definitions.

This cannot be implemented statically, because a constructor can run
arbitrary code which doesn't know what type the object is:

void LibraryFunction(CTemp *basePtr)
{
basePtr->virtualFunction();
}

CTemp::CTemp()
{
LibraryFunction(this);
}

The virtualFunction() call in LibraryFunction has to
CTemp::virtualFunction even though the object is really a CDerived, and
there is a CDerived::virtualFunction.

LibraryFunction cannot do this by clairvoyance: it has to retrieve some
type-related information from the object, such as a vtable pointer.

Even if CTemp is a pure abstract base class (pure virtuals only) so
that the call to virtualFunction is actually a pure virtual call error,
there still has to be something in the object to detect that run-time
error.

Quite conceivably, if the base class has nothing but pure virtuals,
perhaps the vtable could be optimized away. The vtable pointer in the
partially constructed object could just be set to some special value
which summarizes the idea that the object is a pure ABC. Of course,
the code generated for virtual calls would then have to check for this
value to detect pure virtual calls.

(Null would be a poor choice for this value, because, then the run-time
system would not be able to provide a diagnostic which distinguishes
between a pure virtual call
error, and a virtual call on an object that has been stomped-over with
zero bytes).

In a sense, that special value could still be considered a vtable,
albeit a compressed, summarized one.



All times are GMT. The time now is 05:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57