[英]Can the C++ struct aggregate initialisation be used to create temporary instance?
[英]Correct form for dynamic aggregate initialisation of a struct?
我正在嘗試做一些非常簡單的事情,但我做錯了什么。
頭文件:
class Example
{
public:
typedef struct
{
float Position[3];
float Color[4];
float TexCoord[2];
} IndicatorVertex;
void doSomething();
};
.cpp文件:
void Example::doSomething()
{
IndicatorVertex *vertices;
vertices = IndicatorVertex[] {
{{-1.0, 1.0, 1.0}, {1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
{{1.0, 1.0, 1.0}, {1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
};
}
編譯后,我得到Error:(12, 13) unexpected type name 'IndicatorVertex': expected expression
。
(我故意不使用std::vector
等;我故意在c ++ 11設置中使用C功能。)
您不能像創建動態數組那樣,需要定義一個實際的數組,例如
IndicatorVertex vertices[] = { ... };
如果以后需要指針,請記住,數組自然會衰減到指向其第一個元素的指針。 因此,例如,如果您要調用一個需要有IndicatorVertex*
參數的函數,則只需傳入vertices
,它仍將按預期運行。
如果要具有不同的數組並使vertices
指向其中之一,則必須如上所述定義數組,並使vertices
指向其中之一。 喜歡
IndicatorVertex vertices1[] = { ... };
IndicatorVertex vertices2[] = { ... };
// ...
IndicatorVertex* vertices = vertices1;
// ...
vertices = vertices2;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.