c++ new관련 질문

zlzlzl315의 이미지

go.Add(new turn(2, 2));하면 turn클래스가 힙에 x=2,y=2로 생성되고 그주소를 p에 저장한후 p에 저장된
주소로 turn의 k함수를 main함수에서 호출하고싶은데 어떻게 해야될지 모르겠습니다.
그리고 Add함수에서 private을 변경하지않고 x-y값을 구현할 수 있는 방법이 있나요? 함수가 추가되는것은 상관 없습니다.

#include <iostream>
 
class turn
{
private:
    int x, y;
public:
    turn(int x, int y)
        :x{ x }, y{ y }
    {
    }
    int k()
    {
        return x + y;
    }
};
class game
{
private:
    turn* p;
public:
    void Add(turn *turn_p)
    {
        p = turn_p;
    }
};
int main()
{
    game game;
    game.Add(new turn(2, 2));
    p->k;
}
나빌레라의 이미지

포인터 p를 리턴하세요.

----------------------
얇은 사 하이얀 고깔은 고이 접어서 나빌레라

zlzlzl315의 이미지

p->k;를 적으면 바인딩된 함수에 대한 포인터는 함수를 호출하는 데에만 사용할 수 있습니다.라고 뜹니다.
p->k;를 사용할 방법이 없을까요 turn 함수의 변수private을 유지하고요

나빌레라의 이미지

포인터 p를 리턴하는 것이 방법입니다. 그리고 p->k(); 겠죠.

어떻게 하는지는 찾아보세요. 쉽습니다. 심심해서 검색해봤는데 구글에서 답을 찾는데 약 5초 정도 걸립니다.

----------------------
얇은 사 하이얀 고깔은 고이 접어서 나빌레라

zlzlzl315의 이미지

그렇네요.. 뻘짓하고있었네요..

익명 사용자의 이미지

절대 권장할 만한 일은 아닙니다만, 곧 죽어도 main에서 game.pk 함수를 호출하겠다면, 방법이야 있지요.

#include <iostream>
 
class turn
{
private:
    int x, y;
public:
    turn(int x, int y)
        :x{ x }, y{ y }
    {
    }
    int k()
    {
        return x + y;
    }
};
 
class game
{
private:
    turn* p;
public:
    void Add(turn *turn_p)
    {
        p = turn_p;
    }
 
    friend int main(); // A friend declaration
};
 
int main()
{
    game game;
    game.Add(new turn(2, 2));
 
    game.p->k(); // main에서 p에 k 호출하기
}

늘 방법은 있기 마련입니다.
이런 식의 우격다짐을 동원하는 게 과연 프로그래머의 의도에 부합하는지는 또 완전히 다른 문제지만요.

그 부분에 있어서는 도와드릴 수 있는 게 전혀 없군요.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.