-4
#include <iostream>
using namespace std;

class A
{
    public:
        A(int x){ a = x;}
        ~A();
    private:
        int a;
};

int main()
{
    A* a = new A(10);
    void** p;
    p = &a;
    return 0;
}

After compiling with g++ (GCC) 4.1.2 20080704 , I am getting following error :

test.cpp: In function 'int main()':
test.cpp:17: error: invalid conversion from 'A**' to 'void**'

Garf365
  • 3,579
  • 5
  • 29
  • 40
Undefined Behaviour
  • 709
  • 2
  • 8
  • 26

3 Answers3

1

There's one star to much. void* can already hold any pointer, without casts:

int main()
{
    A* a = new A(10);
    void* p;
    p = &a;
    return 0;
}

This works for multiple levels, even:

int main()
{
    A* a = new A(10);
    A** aa = &a;
    A*** aaa = &aa;
    void* p = &aaa;
}

This works because A*** is a pointer to a A**.

MSalters
  • 167,472
  • 9
  • 150
  • 334
  • While you're right, I have to ask, does it matter, wouldn't it be kinda helpful to maintain the level of indirection as part of the pointer type? Also wouldn't your code throw the same error, as it attempts to cast from a `A***` to a `void*`. – Jonathan Mee Aug 16 '16 at 17:51
  • 1
    @JonathanMee: No, that's pretty much the point. `void*` can hold any type of pointer. `void**` holds a pointer to a `void*`. For that reason, you need **exactly** one * on `void*` here. Not 2 or 3, not 0, but 1. – MSalters Aug 16 '16 at 20:47
0

You can't just assign the value of one type (A**) to the variable of another one (void**), if it can't be converted implicitly. You need to explicitly convert value from one type to another:

class A
{
    public:
        A(int x){ a = x;}
        ~A();
    private:
        int a;
};

int main()
{
    A* a = new A(10);
    void** p;

    // use `reinterpret_cast'
    p = reinterpret_cast<void**>(&a);

    // C-style cast works but it is a bad practice in C++
    p = (void**)&a;

    return 0;
}
KolesnichenkoDS
  • 520
  • 1
  • 5
  • 19
  • 1
    I'm glad you added the `reinterpret_cast` but you should really remove the C-Style cast all together. It just encourages bad coding practices. – Jonathan Mee Aug 16 '16 at 13:51
  • Actually, this answer isn't really correct. You **can** assign a variable of some type *U* to a variable of some type *V* without a cast, as long as *U* can be implicitly converted to *V*. If *V* is `void*` then *U* can be **any** pointer. – IInspectable Aug 16 '16 at 16:31
0

You need to use reinterpret_cast:

It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type.

In this case your expression will be &a which has the type A** and your new_type will have the type void**:

p = reinterpret_cast<void**>(&a);

Do have a care here as C++ isn't really that fond of the void type and your code is very likely going to violate Type Aliasing Rules.

In my mind a cast to void** is an indication that a template should probably be used. Without seeing your code I can't guarantee that, but, if you want to fill me in anymore in the comments perhaps I can help.

Community
  • 1
  • 1
Jonathan Mee
  • 36,513
  • 20
  • 115
  • 270