function that returns a void pointer to itself? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

function that returns a void pointer to itself?

I was thinking that in python it's pretty simple to make a function that returns itself, but it seems to be much harder to do something like that in c++. So I'm wondering if it's possible to make a function fun void * fun() where (void*)&fun==fun()

24th Feb 2021, 10:33 PM
Shoo sortai
Shoo sortai - avatar
4 Answers
+ 1
A function is just a lot of instructions in memory, when you call some the CPU jumps into memory's index where the first function's commands are then the CPU read all instructions until have to jump again. Python is an interpreted language, the interpreter can link the function's name with some object in memory C++ is compiled, so pointing for the first function's instruction would work. One way I think is: tells the compiler (i don't know how) that you want it. By coding... Seems impossible for me. In assembly it is pretty simple
25th Feb 2021, 10:11 AM
Igor Padilha dos Santos
Igor Padilha dos Santos - avatar
0
It looks like you are trying to make reference to function, what do you mean by "function that returns itself"? Do you think of recursive functions?
24th Feb 2021, 11:18 PM
Michal Doruch
0
Mirielle[ INACTIVE ] Michał Doruch I want to do a slightly different thing than I did in python in python I did li = [lambda l: lambda: l[0]] li[0] = li[0](li) autoreturner = li[0] del li assert autoreturner == autoreturner() this creates a completely useless function "autoreturner" that just returns itself, so autoreturner==autoreturner()==autoreturner()() I wanted to make something similar in c++, but it's not easy since it is strongly typed, so I'd need to return a function pointer that can't exist (the type of the pointer includes the return type of the function, and to make this you'd need to make a type that's defined recursively, which I'm pretty certain is impossible in c++), but you can cast function pointers to void pointers, so what I wanted to do was make a function that, when called, returns a void pointer to its own position in memory so (void*)&fun==fun()
25th Feb 2021, 7:27 AM
Shoo sortai
Shoo sortai - avatar
0
Igor Padilha dos Santos yeah, I figured I could ask around to see if anyone had ideas, but it's probably impossible :( thanks anyway
25th Feb 2021, 2:01 PM
Shoo sortai
Shoo sortai - avatar