[완료]C++에서 static 멤버 변수를 함수로 초기화 해야 하는 경우, 어떻게 하면 좋을까요?

bluekyu의 이미지

데이터 구조가 특이해서 어떤 형식으로 객체를 만드는 것이 좋을 지 감이 안 잡혀서 질문을 올려봅니다.

아주 간단한 구조로 표현하면 아래와 같이 되어 있습니다.

struct A
{
    static const int list[10];
};

그런데 list가 복잡한 값을 가져서 여러 계산을 거쳐서 초기화 되어야 합니다. 그리고 A는 여러 개가 생성될 수 있습니다.

이를 해결하는 방식으로,
1. 초기화를 확인하는 변수를 하나 두고, A 객체를 처음 생성하면 생성자가 list를 초기화 합니다. 그리고 다른 A 객체가 생성되면 이미 초기화 되었으므로 다시 초기화 하지 않습니다.

2. 아래와 같이 객체를 중첩시키는 방식도 생각해봤습니다.

struct A
{
private:
    struct B
    {
        int list[10];
 
        B(void)
        {
            for (int i = 0; i < 10; i++)
                list[i] = i * i;
        }
    };
 
public:
    static const B index;
};
const A::B A::index;

두 경우 모두 쓸 수는 있지만 더 나은 방식이 있을 것 같아서 질문을 드려봅니다. 이러한 데이터 구조가 있을 경우 어떻게 처리하는 것이 적절할까요?

klyx의 이미지

어느게 더 나은지는 잘 모르겠네요.
저라면 다음과 같이 할 것같습니다.

// .hpp

struct A {
static const int *const list;
};

// .cpp

static const int *getInitializedList() {
static int list[10];
// .. initializing
return list;
}

const int *const A::list = getInitializedList();

배열을 쓰셨길래 배열로 했지만 정말로 저라면 배열도 안쓰고 벡터를 썼을것 같습니다.
그리고 저 스태틱 멤버가 중요한 역할을 한다면 싱글톤으로 빼버릴것 같습니다.

bluekyu의 이미지

답변 감사합니다.

결국 Null 포인터 여부를 가지고 생성자에서 해결을 했습니다.
class 보다는 struct 형식에 가깝다보니까, 멤버를 private로 처리하지 않아서 문제가 더 복잡해졌네요.

/*** Signature ******************
* blog: http://blog.bluekyu.me/ *
********************************/

댓글 달기

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