-3

Possible Duplicate:
What does (char *) x or (void *) z mean?

I am working with a c++ file and have encountered the following line:

tmp.sort(Hash::pairval, printPair, (void *)(tmp.bitSize()));

I am most unsure of what (void *) means. bitsize() is a function, and I have heard the term passing a function pointer before. Is that what this is?

I know the :: is normally the scope resolution operator, which I have seen in .cpp/.h object type files. I believe it is serving the same purpose here, to state that pairval is found in Hash.

Thanks

Community
  • 1
  • 1
SwimBikeRun
  • 4,192
  • 11
  • 49
  • 85

3 Answers3

0

The (void *) is simply casting the return value of tmp.bitSize() to a void pointer type. Casting is a very common operation in C++ and c as well.

Florin Stingaciu
  • 8,085
  • 2
  • 24
  • 45
  • "void type" != "pointer to void" – Griwes Oct 22 '12 at 16:34
  • 2
    Its a cast to a **pointer to** `void` – K-ballo Oct 22 '12 at 16:34
  • @K-ballo Sorry, missed that word. Please see my edits. – Florin Stingaciu Oct 22 '12 at 16:35
  • Explicit type conversions are very rare in well written C++ (and then are almost always `dynamic_cast`). Explicit type conversions _do_ occur when interfacing to C or to legacy code, but even then, you should almost never need an explicit conversion to `void*`. – James Kanze Oct 22 '12 at 16:38
  • @JamesKanze: I would argue that `dynamic_cast<>` is not the most common. If it is then I would be more inclined to look at the design than the code. – Martin York Oct 22 '12 at 18:31
  • @LokiAstari Most common is relative. `dynamic_cast` should be fairly rare. But there are cases where it is justified. As for the other casts, it's hard to think of such cases, other than interfacing with poorly written or legacy code. – James Kanze Oct 22 '12 at 19:29
0

Hash::pair

Is most probably a call to a static member of class Hash.

The (void*) part is a cast to void pointer of tmp.bitSize() which most probably returns some kind of value. So there is no function pointer.

ApplePie
  • 8,814
  • 5
  • 39
  • 60
0

I am most unsure of what (void *) means. bitsize() is a function, and I have heard the term passing a function pointer before. Is that what this is?

Nope. Note the parentheses, tmp.bitSize() is a function call expression that is called and returns a value. Hence - no function pointers involved here.

The return value is then cast to the pointer-to-void type (i.e. the catch-all "pointer to something" type) in order to be passed to a function which expects such pointer.

Why on Earth would someone convert a bit size (which looks like a number) into a pointer, I have no idea. This is somewhere between dubious and incorrect.

Read up on casting in C++. C-style casts are discouraged and casting to void* is seldomly useful and often dangerous because of the strict aliasing rule.

know the :: is normally the scope resolution operator, which I have seen in .cpp/.h object type files. I believe it is serving the same purpose here, to state that pairval is found in Hash.

That's correct.

Community
  • 1
  • 1
Kos
  • 70,399
  • 25
  • 169
  • 233