Casting Pointers

Plz anyone tell me, "What is casting pointers ?"

Give me example.

Thanks.
Or give me any link, where from I can study about casting pointers.

Thanks.
See http://www.cplusplus.com/doc/tutorial/typecasting/

An example? When you use polymorphism, but for some reason the interface of the hierarchy is not enough.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct Foo {
  virtual void fool();
  virtual ~Foo();
};

struct Bar : public Foo {
  virtual void fool();
  void barhop();
};

struct Gaz : public Foo {
  virtual void fool();
  int gasoline();
};


// use
Foo * obj = ....

obj->fool(); // OK, might run Foo::fool() or Bar::fool() or Gaz::fool()
obj->barhop(); // ERROR, there is no Foo::barhop()

if ( auto b = dynamic_cast<Bar*>(obj) ) {
  b->barhop(); // OK, if obj does point to a Bar object
}
Did not get context, how to cast a pointer ?

plz explain, thanks.
There is several reasons to cast a pointer. keskiverto gave example of most popular one.

A little in-depth example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

struct base
{
    virtual void name() const {std::cout << "Base\n"; }
    virtual ~base() = default;
};

struct derived: base
{
    virtual void name() const override {std::cout << "Derived\n"; }
    virtual ~derived() = default;
};

int main()
{
    base* b = new base; //b is a pointer to base;
    derived* d = new derived; //d is a pointer to derived
    base* arr[2]; //Array of two ponters to base
    arr[0] = b; //Assign b to first array element
    arr[1] = d; //Upcast pointer d to base* and assign it to second array element
    //arr[1] = static_cast<base*>(d); ← more clear
    for(auto p: arr) {
        p->name(); //virtual function call
        if(dynamic_cast<derived*>(p) != nullptr)  //Try to downcast pointer
            std::cout  << "Is derived\n";
        else
            std::cout << "Is not derived\n";
    }
}   
Base
Is not derived
Derived
Is derived
Well, I have just started learning C++, So, if I need to ask any thing about it, can anyone give me answer ?

Thanks.
Right-now I can't understand too much complex programs, what are the tips for being good programmer ?

Thanks.
C++ is strictly typed language. If we start by saying that name "foo" means an integer variable, the compiler can and will check that every time the foo is used, it is consistently treated as integer.

At times we need to explicitly convert a value (of variable) to a different type. That is type casting.

For example:
1
2
3
4
int foo = 4;
int bar = 5;
std::cout << foo / bar << '\n';
std::cout << static_cast<double>(foo) / bar << '\n';


0
0.8

Line 3 is integer division and that discards the remainder. Since 4<5, the result is integer 0.

On line 4 we first explicitly create a (temporary unnamed) double value from the foo. We cast int to double.
There is no division operator that takes double and int, so the compiler has to choose between int/int and double/double and the latter wins.
Next, the compiler implicitly casts the bar into double.
The result of floating point division (4.0 / 5.0) is a double value 0.8.


Casting a pointer is similar. A pointer has an address of a memory block and type information about what that memory block is supposed to contain. With a cast of the type we can claim that the memory contains something else than what the pointer's type says. However, the cast should make sense; be a feasible conversion. (Analogously, we could claim that the banana on your hand is a gun, but if you try to fire the "gun", the result will not be what firing a gun is supposed to be like. We would have "broken" the strict type consistency.)
Topic archived. No new replies allowed.