2016-08-07 3 views
1

구조체를 부모 클래스로 선언하고 상속하는 자식 클래스에서 정의 할 수 있습니까? (다른 자식 클래스에서는 다르게 보일 것입니다)?상속 클래스에서 구조체 확장

예는 다음과 같을 것이다 :

그래서 실제로 내가 처리 할 노력하고 무엇을
class parent 
{ 
    virtual struct AlwaysDifferentStructure; 

    //Some methods here 
    //... 
}; 

class child1 : public parent 
{ 
    struct AlwaysDifferentStructure 
    { 
     int foo; 
    } 

    //Some methods here 
    //... 
}; 

class child2 : public parent 
{ 
    struct AlwaysDifferentStructure 
    { 
     char* foo; 
     float bar; 
     double foobar; 
    } 

    //Some methods here 
    //... 
}; 

는 다음과 같습니다 : 나는 부모 클래스 shader 및 상속 클래스의 많은 (즉 simple_color_shader)를 얻었다. 각 셰이더에는 자체의 정점 구조가 있습니다. 이제이 쉐이더 중 하나를 사용하여 객체를 렌더링하고 싶지만 항상 다른 꼭지점 구조가 어떻게 생겼는지는 모르지만 필요합니다. 그래서 저는 모든 아이들에게 구조체를 다시 선언하고 부모를 통해 접근 할 수 있다고 생각했습니다. 그래서 object 클래스는 기본적으로 다음과 같습니다

class object 
{ 
    parent* m_pParent; 
    AlwaysDifferentStructure* m_structArray; 
}; 

(당신은 내가하려는 말 이해하지 않으면 그냥 물어, 내 영어가 좋지 않다)

내 질문은 : 이것도 가능합니다. 그렇다면 누군가가 어떻게 말해 줄 수 있다면 감사 할 것입니다. 영업 이익은 코멘트에 게시 된 조각이 좋은 제안, 나는 미래의 독자를 위해 여기를 게시하도록하겠습니다 것으로 확인을 위해이 같은

+0

[XY 문제] (http://xyproblem.info/)와 비슷합니다. 그런 시설이 있다면 무엇을 위해 쓰겠습니까? 당신은 정말로 무엇을 성취하려고합니까? –

+0

"부모를 통해 액세스 권한 얻기"부분을 명확하게 설명해 주시겠습니까? 'AlwaysDifferentStructure'를 사용하는 코드는 어떻게 생겼습니까? 무의미한'virtual struct AlwaysDifferentStructure;'행을 삭제하면 당신이 보여주는 예제가 행복하게 컴파일 될 것입니다. 도움이 더 필요하면 어려움이있는 실제 사용량을 보여줘야합니다. –

+0

@IgorTandetnik 접근은'new'를 사용하여 배열을 만들고'm_pParent' 포인터가 가리키는 자식들의 struct \ struct – Rafiwui

답변

0

뭔가가 Curiously recurring template pattern

template <typename derived> 
class parent 
{ 
    void someMethod() 
    { 
     typename derived::AlwaysDifferentStructure x; 

     x.foo; 
    } 
}; 

class child1 : public parent <child1> 
{ 
    struct AlwaysDifferentStructure 
    { 
     int foo; 
    } 

    //Some methods here 
    //... 
} 

class child2 : public parent <child2> 
{ 
    struct AlwaysDifferentStructure 
    { 
     char* foo; 
     float bar; 
     double foobar; 
    } 

    //Some methods here 
    //... 
} 
+0

하지만'child2'에'foo'가 없다면'someMethod'는 실패 할 것입니다, 그렇지 않습니까? – Rafiwui

+0

@Rafiwui 예. 나는 구조의 각기 다른 버전이 공통점이 있다고 가정한다. 당신의 예에서는 foo가 그것이었습니다. – nate

+0

나쁜 예 sry – Rafiwui

0

으로 수행 할 수 있습니다.

class parent { 
    //Some methods here 
    //... 
}; 

class child1 : public parent { 
public: 
    auto alwaysDifferentStructure() { 
     struct S { 
      int foo; 
     } s; 

     return s; 
    } 
}; 

class child2 : public parent { 
public: 
    auto alwaysDifferentStructure() { 
     struct S { 
      char* foo; 
      float bar; 
      double foobar; 
     } s; 

     return s; 
    } 
}; 

int main() { 
    auto c1t = child1{}.alwaysDifferentStructure(); 
    c1t.foo = 42; 
    auto c2t = child2{}.alwaysDifferentStructure(); 
    c2t.foo = new char{'c'}; 
    c2t.bar = .42; 
    c2t.foobar = 0.; 
} 

참고이 두 번째 예제에 필요한 : 또 다른 가능한 솔루션으로

class parent { }; 
class child1 : public parent { }; 
class child2 : public parent { }; 

template<typename> 
struct AlwaysDifferentStructure; 

template<> 
struct AlwaysDifferentStructure<child1> { 
    int foo; 
}; 

template<> 
struct AlwaysDifferentStructure<child2> { 
    char* foo; 
    float bar; 
    double foobar; 
}; 

int main() { 
    AlwaysDifferentStructure<child1> c1t; 
    c1t.foo = 42; 
    AlwaysDifferentStructure<child2> c2t; 
    c2t.foo = new char{'c'}; 
    c2t.bar = .42; 
    c2t.foobar = 0.; 
} 

이 작업을 수행 할 수 있습니다 : 코멘트에 언급 한 바와 같이 여기


는 가능한 방법입니다 C++ 14.

관련 문제