[완료] C++과 JAVA inner class 관련 질문입니다.

luibel의 이미지
klyx의 이미지

자바는 잘 모르겠고.. 무슨의도의 코드인가요?
a는 AAA의 멤버이지 BBB의 멤버가 아닙니다.

luibel의 이미지

자바에서는 inner class에서 outer class의 멤버에 접근할 수 있습니다.

자바랑 C++이 문법이 다른건 알지만 혹시나 아시는 분이 계신가 해서요.

klyx의 이미지

자바에서 inner class와 outer class의 관계는 상속관계인가요?

luibel의 이미지

상속하고는 상관없는것 같은데요.

neogeo의 이미지

outer class 의 member 에 접근은 가능합니다만 instance 화 되지도 않은 member 변수에 값을 세팅하는건 뭔가 이상하네요.

static 변수인가요?

일단 a 가 스테틱 변수라는 가정하라면, c++ 에서 접근은

AAA:: 을 이용하시면 AAA 라는 class 의 member 변수에 접근 할 수 있습니다. ( 물론 public 인 녀석 )

class AAA
{
public:
    static int a;
 
    class BBB
    {
    public:
        void test()
        {
            AAA::a = 1;
        }
    };
};

* 다시 해보니 static 의 경우에는 굳이 AAA:: 가 없어도 잘 되는듯 합니다. g++ 기준입니다.
* static 멤버 변수 뿐만아니라 static 멤버 method ( 함수 ) 도 접근 가능합니다.
* 원칙적으로는 AAA:: 를 붙이는게 정석인것 같습니다.

Neogeo - Future is Now.

Neogeo - Future is Now.

luibel의 이미지

-------------------------------------------
C++
AAA:BBB * bbb = new AAA::BBB(); // 사용 가능.
-------------------------------------------
-------------------------------------------
JAVA
AAA.BBB bbb = new AAA.BBB(); // 사용 불가.

Multiple markers at this line
- The local variable bbb is never read
- No enclosing instance of type AAA is accessible. Must qualify the allocation with an enclosing instance of type AAA (e.g. x.new A()
where x is an instance of AAA).
-------------------------------------------
-------------------------------------------
JAVA에서는 대신에 아래와 같이 사용이 가능하네요.

AAA.BBB bbb = new AAA().new BBB(); // 생성 가능.

출처 : http://develop.sunshiny.co.kr/40
-------------------------------------------

luibel의 이미지

C++에서는 불가능하단 결론이네요.

klyx의 이미지

자바를 잘 몰라서 찾아보았습니다.
http://web.lge.cn:8000/system/java/1145
Java에는 내부 클래스가 두종류있는데, 하나는 static이 붙은것이고 하나는 안붙은 것이네요.
안붙은 것은 적으신 것처럼 가능한데, static inner class는 불가능하다고 합니다.
C++에서 nested class는 무조건 static inner class라고 생각하시면될것 같습니다.

댓글 달기

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