194

How can I pass a pointer (Object *ob) to a function which prototype is void foo(Object &) ?

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
Dewsworld
  • 13,367
  • 23
  • 68
  • 104
  • The title says "cast [sic] reference to pointer" but the question starts with a pointer and needs a reference. Which direction is it? – MSalters Apr 16 '12 at 10:56
  • @MSalters it should be *convert*. I'm editing it. But I was looking for whatever solution I get, so I mentioned *cast* – Dewsworld Apr 16 '12 at 11:00

2 Answers2

288

Call it like this:

foo(*ob);

Note that there is no casting going on here, as suggested in your question title. All we have done is de-referenced the pointer to the object which we then pass to the function.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 33
    @Ricobob That's what happens on SO so very often. Answers to simple questions that can be easily understood often garner lots of up votes. Long and complex answers to tricky questions often get few upvotes because voters can't easily judge merit. As for rep, I got nothing on this because of rep cap. ;-) But I heartily agree. I wish complex answers garnered more rep. – David Heffernan Apr 16 '12 at 21:42
  • @DavidHeffernan Yes this seems to be the problem - I guess its an issue to take to MetaSO - if someone hasn't raised it there already. – Ricibob Apr 16 '12 at 21:47
  • 8
    Does this create a copy of `ob`, or just convert (if not cast) the pointer to a reference? What if `ob` was `nullptr`? – Drew Noakes Jun 15 '14 at 23:15
  • 7
    @Drew It simply dereferences the pointer. No copy. No conversion. No cast. The object is then passed by reference to the function. It is an error if the point is null. – David Heffernan Jun 16 '14 at 06:33
  • Makes sense. By error do you mean an immediate segfault, or that you're opening up to future segfaults/undefined behaviour? – Drew Noakes Jun 16 '14 at 10:01
  • 1
    @DrewNoakes http://stackoverflow.com/questions/2727834/c-standard-dereferencing-null-pointer-to-get-a-reference – David Heffernan Jun 16 '14 at 10:31
  • @Ricobob there is a simple explanation for this. There are much more people that are new to the topic, hence they up vote "simple/short" answers more. According to uncle Bob estimates, at any given moment HALF of all programmers in the world have less than 5 years of experience. – A_P Oct 16 '18 at 12:43
  • 2
    **Just a note :** if you want to have the reference without passing to function use it like this : `Object& objref = *ob;` –  Oct 06 '20 at 21:36
58
foo(*ob);

You don't need to cast it because it's the same Object type, you just need to dereference it.

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
  • guessing amd adding lots of ... is not usually the format of a good answer here (even though it is correct in this case), that's probably why you got one downvote. – KillianDS Apr 16 '12 at 11:20
  • 7
    bhhaaa, I added the "I guess" because it made me write at least 30 chars. that's also way I add the "..........." – Roee Gavirel Apr 16 '12 at 11:41
  • 14
    @RoeeGavirel I'm your first up-voter because I felt the downvote to be harsh. FWIW you can get over the 30 char limit but adding an HTML comment which I did in my first version ofthe answer! My edit to your answer shows how. – David Heffernan Apr 16 '12 at 12:04
  • Code-only answers, with no explanation, are frowned upon these days. – Keith M Dec 04 '18 at 19:44
  • 2
    @KeithM - That's a random comment and -1. The answer was given about 6 years ago, also the question didn't ask for the "why" it only asked for the "how". But sure, I'll add an explanation. – Roee Gavirel Dec 05 '18 at 07:09
  • @RoeeGavirel Make it 10 now. – I try so hard but I cry harder Dec 27 '21 at 12:44