c++ 초보입니다. 이게 클래스인지 배열인지 뭔지, 어떻게 써야 하는지 모르겠습니다.

bunkhead의 이미지

프로그래밍 초보라 아직 개념이 잘 안잡혀 있어서
착각하는 부분이 있을 수 있습니다ㅎㅎ..

큐를 구현 하는 중인데
주어진 소스를 기반으로 구현해야 합니다.

주어진 소스는 아래와 같은 함수들과

itemType Queue::get()
{
    itemType t = queue[head++];     //
    if (head > size) head = 0;
    return t;
}
itemType Queue::get()
{
    itemType t = queue[head++];     //여기선 배열처럼..
    if (head > size) head = 0;
    return t;
}

--------------------------------------------------------
이런 주어진 특정 함수에서 queue를 이렇게 써먹고 있습니다.

void traverse(struct treeNode *t)
{
    queue.put(t);                   //근데 여기선 또 클래스처럼..
    while (!queue.empty())
    {
        t = queue.get();
        visit(t);
        if (t->l != z) queue.put(t->l);
        if (t->r != z) queue.put(t->r);
    }
}

Queue:: 이것으로 함수를 재정의 하는듯 보여서
Queue가 클래스이거나 네임스페이스 일거라 생각했는데
이 때 queue를 어떻게 정의 혹은 선언해서 쓰는 것인지 모르겠습니다.
아니 애초에 이 때 Queue랑 queue가 무엇인가요?
위의 queue랑 밑의 queue는 다른 것인가요?

아니면 혹시 소스에 오류가 있다면 알려주시면 감사하겠습니다

HDNua의 이미지

1. operator 키워드를 이용해 객체에 대해 특정 연산자에 별도로 의미를 부여할 수 있습니다.

#include <iostream>
using namespace std;
 
class List { // 크기 3짜리 배열을 저장하는 리스트 클래스 작성
	int list[3]; // 크기 3짜리 배열
 
public:
	int &operator [] (unsigned index) { // List 인스턴스에 대해 [] 연산자 사용 가능
		return list<ol>
</ol>
;
	}
	void show() { // List 인스턴스 메서드
		for (int i=0; i<3; ++i) {
			int &element = list[i];
			cout<<element<<endl;
		}
	}
};
 
int main() {
	List list;
	list[0] = 1; // list.operator [](0) 호출 -> list.list[0]이 반환됨
	list[1] = 2; // list.operator [](1) 호출 -> list.list[1]이 반환됨
	list[2] = 4; // list.operator [](2) 호출 -> list.list[2]이 반환됨
	list.show(); // list의 show 메서드 호출
	return 0;
}

2. Queue도 이런 식으로 구현된 겁니다.

3. ol, /ol 부분은 배열 기호로 index를 감싼 것입니다. lt;는 < 부등호를 나타냅니다.

저는 이렇게 생각했습니다.

익명 사용자의 이미지

위에건 Queue class의 구현 부분이고
아래는 Queue class의 실제 사용 예제네요.
보아하니 트리 탐색 중 깊이 우선 탐색을 Queue를 통해서 구현하는 예제 같은데...

위에서 queue[i++] 이건 사용형태로 봐서는 []연산자를 재정의한 것 같지는 않고
(Queue 자체가 배열 연산을 쓸일이 없음)
Queue class의 맴버 함수를 구현할 때
내부적으로 배열을 이용하여 구현하기 때문에
그런 코드를 쓴 것 같네요.

즉 위 예제에서 queue는 itemType 배열 타입의 클래스 맴버 변수지
Queue 타입의 인스턴스가 아닙니다.

댓글 달기

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