How to create an implicit cast between two pointers of two types

Suppose I have a pointer of type *Foo. I want to pass it to a function that expects a pointer of type *Bar, so I have to create a cast between those. How do I do this? I know I can create a cast to Foo using constructors, but how about *Foo?
(Bar *)foo
Note that this may or may not be safe. You haven't provided enough details for us to know which.
reinterpret_cast<type to cast to>(object to cast)

Run this code live: http://cpp.sh/93yef
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

#include <iostream>
typedef int Foo;
typedef unsigned int Bar;

void Func1(Foo *f)
{
  std::cout << *f << std::endl;
}

void Func2(Bar *b)
{
  std::cout << *b << std::endl;
}

int main()
{
  Bar *B = new Bar;
  *B = 123;
  Foo *F = new Foo;
  *F = -789; // note this int* is cast to unsigned int*
  Func1(reinterpret_cast<Foo*>(B));
  delete B;
  Func2(reinterpret_cast<Bar*>(F)); // note what happened when we reinterpreted the type and tried to use it... sometimes unexpected things occur
  delete F;
  return 0;
}
Last edited on
Notice that both answers ignore your "implicit" requirement.

The reason is that you should not do that.

If you have a specific function that should take one pointer type and convert it to another, create an overload specifically for it:

1
2
3
4
5
6
void does_something_with_foo( Foo* foo );

void does_something_with_foo( Bar* bar_as_a_foo )
{
  return does_something_with_foo( reinterpret_cast <Foo*> (bar_as_a_foo) );
}

This keeps the data types opaque and prevents use (either intentional or accidental) outside of the API.

Hope this helps.
Suppose I have a pointer of type *Foo. I want to pass it to a function that expects a pointer of type *Bar, so I have to create a cast between those. How do I do this? I know I can create a cast to Foo using constructors, but how about *Foo?


The trouble with describing things in abstract terms like Foo & Bar is that there is an implication of telling us how to do it, rather than describing then asking for a solution. In other words you are asking how to do a solution that came from your interpretation of the problem. If we saw an actual real life scenario, we might come up with a completely different and much better solution. For example, one solution might possibly be polymorphism. If that is the case, there may be no need for any casting, and there could be other advantages as well. There could be other solutions as well: Design Patterns, template code etcetera.
I am trying to implement a Splay Tree, or a binary search tree in general. I have two classes: TNode and TTree. TTree consists of a pointer to TNode named root and other member functions.

The thing is sometimes I want to do something to a subtree (which is a node) rather than the tree itself, so I want to convert the node to a tree to be able to use its member functions. And because I am working on pointers (pointer to node and pointer to tree), I need to convert the pointers instead.

Thanks for the replies and help.
Last edited on
1
2
3
4
5
6
7
void make_snafucated(Node *node){
    node->snafucation();
}

void make_snafucated(Tree *tree){
    make_snafucated(tree->root);
}

In other words, implement operations that may apply to whole trees or to subtress in terms of the nodes, and add an overload for the whole tree that just calls the actual implementation and passes the root node.
Last edited on
Topic archived. No new replies allowed.