Is the "this" RTTI pointer enabled?

I am trying to find the most derived object class in the constructor of one of the classes in my inheritance tree. I have spent several hours on it now and do not understand how else I can do it or why it does not make sense. It seems to make sense, but he refuses to work. I found many pages about RTTI and got mostly nowhere with them. I will continue the explanation after my test case and its output.

Source:

#include <iostream>
#include <typeinfo>
#include <string>

class A
{
public:
  A(std::string foo);

  virtual void bar(A* a) = 0;
};

class B : public A
{
public:
  B();

  virtual void bar(A* a);
};

A::A(std::string foo)
{
  std::cout << "type as passed to A constructor: " << foo << " (" << this << ")" << std::endl;
  std::cout << "type as determined in A constructor: " << typeid(*this).name() << " (" << this << ")" << std::endl;
}

B::B() : A(typeid(*this).name())
{
  A* a = (A*)this;
  std::cout << "type as determined in B constructor: " << typeid(*a).name() << " (" << this << ")" << std::endl;
  this->bar(this);
}

void B::bar(A* a)
{
  std::cout << "type as determined in bar: " << typeid(*a).name() << " (" << a << ")" << std::endl;
}

int main()
{
  B b;
  b.bar(&b);

  return 0;
}

Output (on g ++):

type as passed to A constructor: 1B (0x7fff5fbff910)
type as determined in A constructor: 1A (0x7fff5fbff910)
type as determined in B constructor: 1B (0x7fff5fbff910)
type as determined in bar: 1B (0x7fff5fbff910)
type as determined in bar: 1B (0x7fff5fbff910)

, "1B" "1A". RTTI "this" - ? ? ( , , RTTI, ). , , "", .

+5
3

, .

:

this / , / .

, virtual, , , , virtual .

, . -, .

+4

. , , .

typeid(*this) . undefined ++ 03, ++ 11, , , .

+3

, , A , B. A B, . , . .

, , , .

0

All Articles