문제

in C++, I can easily create a function pointer by taking the address of a member function. However, is it possible to change the address of that local function?

I.e. say I have funcA() and funcB() in the same class, defined differently. I'm looking to change the address of funcA() to that of funcB(), such that at run time calling funcA() actually results in a call to funcB(). I know this is ugly, but I need to do this, thanks!

EDIT----------

Background on what I'm trying to do:

I'm hoping to implement unit tests for an existing code base, some of the methods in the base class which all of my modules are inheriting from are non-virtual. I'm not allowed to edit any production code. I can fiddle with the build process and substitute in a base class with the relevant methods set to virtual but I thought I'd rather use a hack like this (which I thought was possible).

Also, I'm interested in the topic out of technical curiosity, as through the process of trying to hack around this problem I'm learning quite a bit about how things such as code generation & function look-up work under the hood, which I haven't had a chance to learn in school having just finished 2nd year of university. I'm not sure as to I'll ever be taught such things in school as I'm in a computer engineering program rather than CS.

Back on topic The the method funcA() and funcB() do indeed have the same signature, so the problem is that I can only get the address of a function using the & operator? Would I be correct in saying that I can't change the address of the function, or swap out the contents at that address without corrupting portions of memory? Would DLL injection be a good approach for a situation like this if the functions are exported to a dll?

도움이 되었습니까?

해결책

No. Functions are compiled into the executable, and their address is fixed throughout the life-time of the program.

The closest thing is virtual functions. Give us an example of what you're trying to accomplish, I promise there's a better way.

다른 팁

It cannot be done the way you describe it. The only way to change the target for a statically bound call is by modifying the actual executable code of your program. C++ language has no features that could accomplish that.

If you want function calls to be resolved at run-time you have to either use explicitly indirect calls (call through function pointers), or use language features that are based on run-time call resolution (like virtual functions), or you can use plain branching with good-old if or switch. Which is more appropriate in your case depends on your specific problem.

Documentation Listing 오류 코드 802를 찾을 수 없습니다. 다음 페이지는 오류 코드 1, 800, 801 및 1을 나열합니다. 1의 두 번째 항목이 오타인지 여부를 알 수 없습니다.그것은 당신의 오류 802 일 수 있습니다 ...

"NoFollow"> http://developer.android.com/reference/android/media/mediarecorder.html

What you want is a pointer to a function, you can point it to FuncA or FuncB assuming that they have the same signature.

I am fairly sure this is impossible in pure C++. C++ is not a dynamic language.

bytea가 해시를 저장하는 데 최적입니다.

PostgreSQL의 바이너스 와이어 프로토콜 (Libpq에서 및 PGJDBC에서 부분적으로 PGJDBC에서 부분적으로 PGJDBC에서 지원)을 사용하지 않는 한 데이터베이스에서 HEX 문자열로 데이터베이스에서 전송할 것입니다.

최상의 결과를 얻으려면 BYTEA로 저장하고 클라이언트 응용 프로그램이 바이너리 결과를 요청하는 PQexecParams 호출을 사용하십시오.

다시 읽을 때, 이것은 혼란 스럽습니다 :

내 구현을 위해 해시는 데이터베이스를 남겨 두지 않아야하지만 해시 데이터는 자주 존재에 대한 외부 데이터와 비교되어야합니다

해시가 비교를 위해 전송되지 않음을 의미합니까? 원래 번식하지 않은 텍스트 데이터는 무엇입니까? 그렇다면 바이너리 프로토콜이 텍스트 형식 데이터에 대한 이점을 제공하지 않기 때문에 위의 사항은 부적합합니다.

또한 : "수십억 수십억"이 많이 있습니다. PostgreSQL은 28 바이트에서 꽤 큰 오버 헤드가 있으므로 많은 공간을 잃을 것입니다. 특히 인덱스 오버 헤드도 포함하면됩니다. 이 작업을위한 PostgreSQL이 올바른 도구입니까?

최종 생각 : 그 많은 행을 사용하면 해시 충돌 영토로 일어납니다. 가능하면 가능 하시는지 - 틀림없이 동일한 해시를 갖기 위해 두 개의 다른 문자열의 경우 잘못된 고유 한 위반이보고됩니까? 문제가 해시에 해시의 고유 한 B 트리 인덱스가 작업을위한 올바른 도구가 아닐 것입니다.

Its simple!

For

at run time calling funcA() actually results in a call to funcB().

write funcA() similar to following:

int funcA( int a, int b) {
  return funcB( a, b );
}

:-)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top