[英]Passing array into constructor parameter and creating an array of structs using the value of the array
我目前正在為將繪制到屏幕上的多邊形編寫 class。 然而,我的問題是,我無法弄清楚如何從 arrays 數組(對於每個頂點的 x 和 y 保存整數)創建一個結構數組。 我通過構造函數傳遞這個數組。 我認為我的錯誤與嘗試將指針作為 integer 傳遞有關,盡管經過危險的研究,我似乎無法弄清楚如何解決我的錯誤。 我來自動態類型語言的背景(主要是 Js 和 Python),這是我第一個使用靜態類型語言的大型項目。 任何幫助是極大的贊賞。
struct Point {
int x , y;
};
class Polygon
{
private:
Point centre;
Polygon(int x, int y, int vertices[]) {
centre = {x, y};
struct Point points[sizeof(vertices)/sizeof(vertices[0])] = {vertices};
}
//etc....
};
//Example of how it will be called in main.cpp
int main{
Polygon polygon(0, 0, {{5,5}, {-5,5}, {5,-5}, {-5,-5}} );
}
這個怎么樣?
struct Point {
int x , y;
};
class Polygon
{
private:
Point m_centre;
std::vector <Point> m_vec_vertices;
public:
Polygon(const Point& centre, const std::vector<Point> &vertices)
:m_centre(centre), m_vec_vertices(vertices) { }
//etc....
};
int main(){
Polygon polygon({0, 0}, {{5,5}, {-5,5}, {5,-5}, {-5,-5}} );
}
您在Polygon
構造函數中定義了一個點 class,為什么不使用它來代替x
和y
對吧?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.