Hamza Khan
Hamza Khan

Reputation: 131

Object type identification using pointers in C++

I recently learned that pointers contain two types of information the memory address and the type they point to. So I know if I have a pointer, then I can tell whether the object it points to is an int or double or any other primitive data type.

My question was if I create an object called myObj and I have a pointer to myObj, then can I tell from the pointer that the thing it points to is an object called myObj? Or will it just say that the pointer points to a non-primitive data type?

Upvotes: 0

Views: 620

Answers (3)

Peter
Peter

Reputation: 36597

It's not correct to say that pointers contain two types of information. In general terms, a pointer (like any variable) has two properties: a type and a value.

The type of a pointer indicates what it contains the address of. So, a pointer to int (aka an int *) contains the address of an int.

The value of a pointer is the address of an actual object. This is the information contained by the pointer.

Using the type information and the value of a pointer, it is possible to do any permitted operations on the object (or variable) the pointer points at.

For example;

  int i;

  int *p = &i;    //  p is of type int *, so points at an int.
                  //   This initialisation gives p a value which is the address of i

  *p = 42;      // p points at i, *p refers to i, so this statement sets i to be 42

There are a few exceptions to the above though.

A void pointer (aka a void *) does not contain type information (void approximately means "does not have a type" in this context) but does have a value.

  int i;
  void *p = (void *)(&i);

  *p = 42;    // invalid, since p is a void * - it could point at anything

A pointer may be uninitialised (e.g. defined without being given an initial value). In that case, accessing its value gives undefined behaviour. As does accessing what it points at (since, to access what a pointer points at, it is necessary to access the value of the pointer itself).

A pointer may be initialised or assigned with the value NULL or (equivalently in C++11 or later) nullptr. This is a special value indicating the pointer does not contain the address of an object. Accessing such a non-existent object also gives undefined behaviour.

Things are more complicated with class/struct types. For example, pointer to a polymorphic type can contain the address of any object of that type, and any type derived from it.

Upvotes: 3

g11101
g11101

Reputation: 11

A pointer has a type but it is not a type, the type must match your returning obj.

#include<typeinfo>
#include<string>
#include<iostream>
//..
//..
//..
string name = typeid(*myObj).name()
//..
cout<<"Name: "<< name;
//..

This will return the myObj type. For more information: http://en.cppreference.com/w/cpp/language/typeid

Upvotes: 1

Nick Pavini
Nick Pavini

Reputation: 342

Given a class named MyObj

class MyObj
{
private:
  //....
  //....
public:
  //....
  //....
};

The pointer type would be of type MyObj, and it would point to a MyObj type or derived type if there are inherited classes under MyObj.

int main()
{
  MyObj obj;
  MyObj *ptr = &obj;  //adress contained in the pointer

return 0;
}

Upvotes: 0

Related Questions