22 Mayıs 2017 Pazartesi

Unary Operator

Giriş
Açıklaması şöyle
1 The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T”, the type of the result is “T”. [ Note: Indirection through a pointer to an incomplete type (other than cv void) is valid. The lvalue thus obtained can be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to a prvalue, see 4.1. —end note ]
Indirection
Örnek
Elimizde integer'a pointer olsun. Şöyle yaparız.
int &r = *(new int(100));   
std::cout << r << std::endl;
Örnek
Elimizde nesneye pointer olsun. Şöyle yaparız.
struct A
{
  virtual ~A()
  {
    std::wcout << "A::~A()" << std::endl;
  }
};
struct B : A
{
  ~B()
  {
    std::wcout << "B::~B()" << std::endl;
  }
};

A &ra = *(new B);
delete &ra;
Çıktı olarak şunu alırız.
B::~B()
A::~A()

Hiç yorum yok:

Yorum Gönder