C++ 在C++;
我正在做“生活游戏”作为学校的一项作业,导师给了我们部分代码,但我在理解两部分方面有问题 第一:C++ 在C++;,c++,data-structures,struct,constructor,C++,Data Structures,Struct,Constructor,我正在做“生活游戏”作为学校的一项作业,导师给了我们部分代码,但我在理解两部分方面有问题 第一: grid(int scale) : scale_(scale), width_(problem_definition::width(scale)), height_(problem_definition::height(scale)), data_(new problem_definition::data_type[width_*height_]) 直到我能理解的时候,他才请求内存,用于模拟网格
grid(int scale) : scale_(scale), width_(problem_definition::width(scale)), height_(problem_definition::height(scale)), data_(new problem_definition::data_type[width_*height_])
直到我能理解的时候,他才请求内存,用于模拟网格。但是,我无法掌握新语法以外的内容。为什么那些括号中有空格?网格后的冒号(整数比例)是什么意思
第二:
void initialize()
{
for (std::size_t y = 0; y < height_; ++y)
{
for (std::size_t x = 0; x < width_; ++x)
{
at(x,y) = problem_definition::initialize(x, y, scale_);
}
}
void initialize()
{
对于(标准::尺寸y=0;y<高度++y)
{
对于(标准::尺寸x=0;x
}
第二个for循环中的“at()”是什么
如果您能了解完整的结构定义,我将不胜感激:
struct grid
{
grid(int scale) : scale_(scale), width_(problem_definition::width(scale)), height_(problem_definition::height(scale)), data_(new problem_definition::data_type[width_*height_])
{
}
void initialize()
{
for (std::size_t y = 0; y < height_; ++y)
{
for (std::size_t x = 0; x < width_; ++x)
{
at(x,y) = problem_definition::initialize(x, y, scale_);
}
}
}
problem_definition::data_type at(std::size_t x, std::size_t y, int dx, int dy)
{
if (dx<0 && -dx>x) return problem_definition::out_of_bounds_value();
if (dy<0 && -dy>y) return problem_definition::out_of_bounds_value();
if (x + dx >= width_) return problem_definition::out_of_bounds_value();
if (y + dy >= height_) return problem_definition::out_of_bounds_value();
return at((int)x + dx, (int)y + dy);
}
problem_definition::data_type& at(std::size_t x, std::size_t y)
{
return data_[x + y*width_];
}
void swap(grid& other)
{
std::swap(scale_, other.scale_);
std::swap(width_, other.width_);
std::swap(height_, other.height_);
std::swap(data_, other.data_);
}
void print(std::ostream& str)
{
for (std::size_t y = 0; y < height_; ++y)
{
for (std::size_t x = 0; x < width_; ++x)
{
str << at(x, y);
}
str << std::endl;
}
}
private:
int scale_;
std::size_t width_;
std::size_t height_;
std::unique_ptr<problem_definition::data_type[]> data_;
};
problem_definition::data_type& at(std::size_t x, std::size_t y)
{
return data_[x + y*width_];
}
结构网格
{
网格(整数比例):比例(比例),宽度(问题定义::宽度(比例)),高度(问题定义::高度(比例)),数据(新问题定义::数据类型[宽度*高度])
{
}
void initialize()
{
对于(标准::尺寸y=0;y<高度++y)
{
对于(标准::尺寸x=0;xgrid(int scale) : scale_(scale),
width_(problem_definition::width(scale)),
height_(problem_definition::height(scale)),
data_(new problem_definition::data_type[width_*height_])
现在我们可以很容易地看到冒号(a:
是冒号,a;
是分号)定义了一个初始化器列表,它接受一系列var(value)
对,并将每个值
分配给每个var
我没有看到任何“带空格的括号”,因此无法回答。但是beyondnew
只是一种标准的数组类型:元素类型[length]
。元素类型是问题定义::数据类型
,数组的长度宽度
倍高度
最后,at()
只是一个返回引用的函数调用。如果研究函数定义:
struct grid
{
grid(int scale) : scale_(scale), width_(problem_definition::width(scale)), height_(problem_definition::height(scale)), data_(new problem_definition::data_type[width_*height_])
{
}
void initialize()
{
for (std::size_t y = 0; y < height_; ++y)
{
for (std::size_t x = 0; x < width_; ++x)
{
at(x,y) = problem_definition::initialize(x, y, scale_);
}
}
}
problem_definition::data_type at(std::size_t x, std::size_t y, int dx, int dy)
{
if (dx<0 && -dx>x) return problem_definition::out_of_bounds_value();
if (dy<0 && -dy>y) return problem_definition::out_of_bounds_value();
if (x + dx >= width_) return problem_definition::out_of_bounds_value();
if (y + dy >= height_) return problem_definition::out_of_bounds_value();
return at((int)x + dx, (int)y + dy);
}
problem_definition::data_type& at(std::size_t x, std::size_t y)
{
return data_[x + y*width_];
}
void swap(grid& other)
{
std::swap(scale_, other.scale_);
std::swap(width_, other.width_);
std::swap(height_, other.height_);
std::swap(data_, other.data_);
}
void print(std::ostream& str)
{
for (std::size_t y = 0; y < height_; ++y)
{
for (std::size_t x = 0; x < width_; ++x)
{
str << at(x, y);
}
str << std::endl;
}
}
private:
int scale_;
std::size_t width_;
std::size_t height_;
std::unique_ptr<problem_definition::data_type[]> data_;
};
problem_definition::data_type& at(std::size_t x, std::size_t y)
{
return data_[x + y*width_];
}
然后,通过引用的魔力,您可以看到调用at()
的行与以下内容相同:
data_[x + y*width_] = problem_definition::initialize(x, y, scale_);
构造函数后面的冒号和后续代码是一个。它初始化类的字段,即为它们赋值。at()只是一个方法,返回对网格数据中元素的引用,即数据的简写[x+y*width]这是一个相当复杂的问题。在这个网站上,你最好问一个定义明确的问题,而不是寻求开放式的帮助。谢谢你的回答,我更正了问题中的冒号(分号)。