Solved

RTTI on a pure abstract class.

Posted on 1998-12-22
12
368 Views
Last Modified: 2010-04-02
Is it possible to obtain RTTI information for a pure abstract base class?

I have a class that calls a "debug check" procedure.  For one or two of the checks in the procedure it calls a virtual function that is pure in the base class.  This is not ussually a problem, except if the check procedure is called from the constructor or destructor (or procedures they call).  In that case, this causes an exception as the virtual function is not implimented.  I would like to automatically skip this check during construction and destruction.  

To detect if the class is being constructed or destructed, I could get the class's RTTI information in the check procedure.  The question is, does that work?  Does C++ return information for a pure abstract base class?  Is that information different than the information for the fully constructed class?  I want to know what the standard prescribes, not what a particular compiler does.
0
Comment
Question by:nietod
  • 5
  • 4
  • 3
12 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1180829
>> Is it possible to obtain RTTI information for a pure abstract base class?
Usually no, because you cannot have an instance of an abstract class.
However, inside the construcor, typeid() behaves normally so:

>> does that work?
Yes.

>> Does C++ return information for a pure abstract base class?
Yes, if you can get one.

>> Is that information different than the information for the fully constructed class?
No.

>> In that case, this causes an exception as the virtual function is not implimented.
Pure virtual functions *can* have bodies.  Just create an empty body for the base func.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1180830
Quoting chapter and verse: ISO C++ standard paragraph 12.7.4
"The typeid operator can be used during construction or destruction.  When typeid id used in a constructor or in a destructor, or used in a function called (directly or indirectly) from a constructor or destructor, if the operator of the typeid refers to the object under construction or destruction, typeid yields the type_info representing the constructor or destructor's class.  If the operand of typeid refers to the object under construction or destruction and the static type of the operand is neither the constructor or destructor's class nor one of its bases, the result of the typeid is undefined".

0
 
LVL 5

Expert Comment

by:yonat
ID: 1180831
Here is the relevant caluse (from _class.cdtor_):

  The typeid operator (_expr.typeid_) can be used during construction or
  destruction (_class.base.init_).  When typeid is used in a constructor
  (including from the  mem-initializer  for  a  data  member)  or  in  a
  destructor, or used in a function called (directly or indirectly) from
  a constructor or destructor, if the operand of typeid  refers  to  the
  object  under construction or destruction, typeid yields the type_info
  representing the constructor or destructor's class.  If the operand of
  typeid  refers to the object under construction or destruction and the
  static type of the operand is neither the constructor or  destructor's
  class nor one of its bases, the result of typeid is undefined.

So, if I understand correctly, you will not get the desired result.
0
 
LVL 5

Expert Comment

by:yonat
ID: 1180832
Seems alexo not only beat me to answer, but also provided a good solution (provide an empty body to the pure virtual function).
0
 
LVL 22

Author Comment

by:nietod
ID: 1180833
>> >> Is that information different than the information for the fully constructed class?
>>   No.

I meant, durring construction, does it return the type info for the base class, or for the class to be constructed. Which you answered below.

>> >> In that case, this causes an exception as the virtual function is not implimented.
>>   Pure virtual functions *can* have bodies.  Just create an empty body for the base func.
It does have a body.  In fact all my pure virtual functions do, due to the requirements of a utility that I wrote.  Despite this, under VC 6 (but I think not 5) I get an error when I call this virtual function from the constructor or destructor.  It looks like the vtbl entry for the function points directly to a a debug function for reporting this "problem", rather than to the actual function.  (I believe this can be turned off, which would be another fix, but I prefer to leave it on.)  (I should have mentioned that the problem is intentionaly caught by the debugger, it is not like the program jumps to some random address and causes an exception.)

I'm going to try coding that soon.  I don't know if VC will actually handle it right, we'll see.  But whether or not it does, this answers my question.  But, I'll leave this open a little longer just in case some one else has something to add.

Where did you get the quote?  an online source?
0
 
LVL 22

Author Comment

by:nietod
ID: 1180834
>>So, if I understand correctly, you will not get the desired result.

Why is that?  

The object specifed to typeid() will "refer  to  the object  under construction or destruction"
so the I should get the "type_info representing the constructor or destructor's class"  This will be different than the type for the derived classes.  So i can tell if the check is being made from the constructor, right?

>> Provided a good solution (provide an empty body to the pure virtual function).
Unfortuantely, not.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:yonat
ID: 1180835
Say you have:

class Derived : public Base {}

If I understand correctly, when you call typeid() from Base's constructor, you get the type_info of Base, not Derived. ("typeid yields the type_info representing the constructor or destructor's class.")
0
 
LVL 22

Author Comment

by:nietod
ID: 1180836
Right, that's the way I understood it.  And I'll use that info to skip performing some checks.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1180837
>> Where did you get the quote?  an online source?
I got it from the final C++ standard, which is available from ANSI as a PDF document for the reasonable sum of US$18.  Unfortunately the bastards disabled the copy&paste option of the document making me type the paragraph by hand (I only did it because I love you so much :-).

I think that Yonat pasted from the *draft* standard which is pretty close and available online in a multitude of formats.  She'll correct me if I'm wrong.
0
 
LVL 22

Author Comment

by:nietod
ID: 1180838
It is implimented and working.  (Nothing ELSE is, but that is).

>> making me type the paragraph by hand (I only did it
>> because I love you so much
I am truly honored.  And extra 25 pts X 4 for the effort.  

Strange that copy and paste can be dissabled and strange that it was.  Seems like an ineffective way to prevent illegal copy.  Force users to steal the whole file rather than parts?

Thanks to both of you.
0
 
LVL 22

Author Comment

by:nietod
ID: 1180839
EE's server screwed up and I had to repost the grade and forgot to change the points the 2nd time.  Look for another one.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1180840
>> Strange that copy and paste can be dissabled and strange that it was.
A PDF file can have certain restrictions which should be honored by the reader application.  Printing the document can be disabled as can selcting and copying.  I have no idea why ANSI decided to disable it.

>> Seems like an ineffective way to prevent illegal copy.
It sure wasn't an attempt at that since the file is printable.

>>  am truly honored.  And extra 25 pts X 4 for the effort.
Ah, now I understand where the "bonus question" came from.

BTW, it appears that there is a way to circumvent the protection after all...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now