std::bind 의 두번 째 인자를 가져올 수 있는 방법이 있습니까?

hsnks100의 이미지

class CumberParent
{
public:
std::function test;


} cp;

class AnotherClass
{
public:
void someFunction(){ .... }
void doSome()
{
cp.test = std::bind(&AnotherClass::someFunction, this);
}
};

cp.test 에서 std::bind 의 두번 째 인자 this 를 가져올 수 있는 방법이 있습니까?

아시면 귀중한 답변 부탁드립니다.

불가능한건가요? 가능할거 같은데 암만 찾아도 나오질 않네요.

cp.test.target 이 답인줄 알았는데 제가 잘 모르는건지... 잘 안되네요.

이것을 해야하는 이유는 AnotherClass 가 가지고 있는 함수 someFunction 이 AnotherClass 의 소멸자가 불러진 이후에 호출될 가능성이 있기 때문에
CumberParent 에서 미리 해당하는 함수는 = nullptr 를 대입하여 호출하지 않기 위함입니다.

klyx의 이미지

불가능합니다. 소멸자에서 cp.test를 무효화하려면 여기에 널포인터를넣어버림 됩니다.

hsnks100의 이미지

무효화 시킬 녀석을 찾아야 되는데 별도로 두번 째 인자를 저장을 해야하는건가요?

----------------------------------------------------
개인 블로그: https://kangssu.com

klyx의 이미지

cp는전역변수가 아닌가요? 올리신코드는 전역변수인데요.

익명 사용자의 이미지

예 배열형태입니다.

class CumberParent
{
public:
std::function test;

}

vector cps;

이 상황에서 넘겨줬던 this 가지고 해당 function 호출을 무효화시켜야 합니다.

재답글 달아주셔서 정말 감사합니다.

klyx의 이미지

배열이면 인덱스를 저장하고 있다가 소멸자에서 인덱스로 접근해서 cp[idx].test = nullptr 처럼 하면되겠네요. 중간에 벡터 크기가 줄어든다면 그냥 벡터가 아니라 std::map을 쓰시거나 몇개안된다면 this값도 같이 CumberParent 구조체에 저장해두고 순차 검색으로 처리할수도 있고요.

hsnks100의 이미지

정말 감사합니다.

std::bind(&AnotherClass::someFunction, this) 에서 this 를 빼올 수 없다면

호출 할 때, pair(std::bind(&AnotherClass::someFunction, this), this); 이렇게 this 를 두번 쓰는 방법으로 해결보는 거죠?

참 아쉽네요... ㅜㅜ

----------------------------------------------------
개인 블로그: https://kangssu.com

klyx의 이미지

그렇게 하셔도 되고, CumberParent를 struct CumberParent { std::function test; void *owner = nullptr; } 처럼 하시고 cp[idx] = ...; cp[idx].owner = this; 처럼 해도 되지요.
구현하는 방법이야 이리저리 해보면 뭔가 나오기 마련인데, 그 보다는 이 구조가 필수적인지를 한 번 더 고민해보시는게 좋을 듯합니다. 상속과 가상함수로 해결해야할 문제는 아닌가요?

익명 사용자의 이미지

예 고민해보겠습니다 ~ ^^

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.