little question - boost

Hello!.I am trying to understand a code and I have this definition (using library boost)

boost::shared_ptr< Node > m_node;

and then I have :

1
2
3
4
5
6
7
8
9
10
11
12
13

if ( m_node )
		{
  boost::any currentValue = m_node->getValue();

  if ( m_node->getType() == &typeid ( boost::shared_ptr< Mesh > ) ||
      m_node->getType() == &typeid ( boost::shared_ptr< Mask > ))
              .
              .
              .
           // some code
		}


The first one is : 1) What's the meaning of &typeid

2) Why can I put inside the if confition m_node ?
The meaning would be : If("the pointer is defined") . . .

Thank u!
Last edited on
1)
MSDN wrote:
The result of typeid is a const type_info&. The value is a reference to a type_info object that represents either the type-id or the type of the expression, depending on which form of typeid is used
It compares the adresses of the type_info.

2) The bool operator is overwritten by smart pointer so you can use them within an if clause
Thank you for the quick response =)

Concerning question 2. I look here : http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/shared_ptr.htm#dynamic_pointer_cast

But I don't find what you said. I think I am not understanding well =( .. sorry for making you work..
You may find this http://www.cplusplus.com/reference/std/typeinfo/type_info/ better to understand what typeid/type_info is.

Yes, dynamic_pointer_cast is what usually is used in such a case. It returns null if it's not the required type. if it's not null you usually want to access that object.

The example above compares two pointers to the type_info which is somewhat faster than a dynamic_pointer_cast. The issue is: is the type_info of an object of the same type always on the same memory address? Regarding libraries and inheritance.
Thank u.

What I didn't understand is

if (m_node)

why can be a pointer inside an if condition.
If x is a bultin integral type or pointer then if (x) is equivalent to if ( x != 0 ).
This behavior is mimicked by shared_ptr, as it is convertible to bool :
So if (m_node) and if (m_node.get() != 0) is equivalent.
Last edited on
Yes, that's ok, thank you!

But I still confused with


get

T * get() const; // never throws
Returns: the stored pointer.
Throws: nothing.


How can you compare 0 with a type.
It doesn't compare it with a type, it compares it with value it holds.
Pointers value is an address, so you could say it compares it with address.
Topic archived. No new replies allowed.