c + +使用一个类中的一类(一类),并调用其功能
问题描述:
我有以下问题:我想从好老C
项目重做C++
,使一切class(y) :)
并保持它从一开始可扩展性。c + +使用一个类中的一类(一类),并调用其功能
它是细胞的网格上的一个模拟(A群被部分),所以决定以下结构:
class Simulation has an instance of
class Grid has an instance of
class Swarm has an instance of
class Cell
我在单独的头文件中定义的类。那么当然,我需要能够在grid,swarm和cell中调用函数。我想要做的直截了当:
Simulation mysim;
mysim.get_grid(0).any_function_here();
与电网作为返回参数
Grid Sim::get_grid(int grid_no)
{
std::cout << "sim.get_grid(" << grid_no << ") called." << std::endl;
if (grid_no <= amount_of_grids)
return this->test;//##//this->gridlist[grid_no];
else
std::cout << "you have not created this grid number yet" << std::endl;
Grid dummy;
return dummy;
}
它调用的函数,并且只要在电网未进行任何更改的作品。这些似乎在太空中消失了。可能是一个指针错误,但我不能找到一个错误,因为完全相同的代码工作的Simulation
类...
更多来源:
int Grid::create_swarm(std::string name)
{
Swarm new_swarm;
new_swarm.set_name("Protoswarm");
swarmlist.push_back(new_swarm);
this->amount_of_swarms ++;
std::cout << "amount_of_swarms = " << amount_of_swarms << std::endl;
return 0;
}
Swarm Grid::get_swarm(int swarm_no)
{
std::cout << "grid.get_swarm(" << swarm_no << ") called." << std::endl;
if (swarm_no <= amount_of_swarms)
return swarmlist[swarm_no];
else
std::cout << "oh oh - you have not this swarm in here..." << std::endl;
Swarm dummy;
return dummy;
}
我可以随时调用create_swarm
功能我想要,但是群体永远不会出现,并且柜台不会在该网格中提升,只要功能在那里即可。我错过了什么吗?它真的只是一个指针错误?为什么这段代码工作,如果我这样称呼它:
Grid newgrid;
newgrid.create_swarm();
一个快速ç& p'ed MWE
#include <iostream>
#include <string>
#include <vector>
class Sim
{
public:
Sim();
virtual ~Sim();
Grid get_grid(int grid_no);
protected:
private:
std::vector<Grid> gridlist;
int amount_of_grids = -1;
};
class Grid
{
public:
Grid();
virtual ~Grid();
int set_size(int x, int y);
int create_swarm(std::string name);
Swarm get_swarm(int swarm_no);
void print_swarms();
protected:
private:
std::vector<Swarm> swarmlist;
int amount_of_swarms = -1;
/*static const*/ int size_x;
/*static const*/ int size_y;
std::vector<std::vector<Field>> fields;
std::string gridname;
};
Grid Sim::get_grid(int grid_no)
{
std::cout << "sim.get_grid(" << grid_no << ") called." << std::endl;
if (grid_no <= amount_of_grids)
return this->gridlist[grid_no];
else
std::cout << "you have not created this grid number yet" << std::endl;
Grid dummy;
return dummy;
}
int Grid::create_swarm(std::string name)
{
Swarm new_swarm;
new_swarm.set_name("Protoswarm");
swarmlist.push_back(new_swarm);
this->amount_of_swarms ++;
std::cout << "amount_of_swarms = " << amount_of_swarms << std::endl;
return 0;
}
Swarm Grid::get_swarm(int swarm_no)
{
std::cout << "grid.get_swarm(" << swarm_no << ") called." << std::endl;
if (swarm_no <= amount_of_swarms)
return swarmlist[swarm_no];
else
std::cout << "oh oh - you have not this swarm in here..." << std::endl;
Swarm dummy;
return dummy;
}
using namespace std;
int main(int argc, char* argv[])
{
Sim mysim;
mysim.create_grid();
mysim.get_grid(0).create_swarm("Alpha-Swarm");
mysim.get_grid(0).create_swarm("Betaa-Swarm"); //doesn't work
Grid newgrid;
newgrid.create_swarm("Gamma-Swarm");
newgrid.create_swarm("Delta-Swarm"); // works, but is not needed.
return 0;
}
答
Grid Sim::get_grid(int grid_no) {...}
您是通过值返回,而不是通过引用。这意味着您要返回的是实际成员的副本。但是,对于您的情况,您希望通过引用返回,以便能够更改原始对象。您的代码将成为
Grid& Sim::get_grid(int grid_no) {...}
请记住,但是,您将无法返回任何临时对象这种方式(如您的dummy
网格),所以你需要改变你的方法来解决这个问题。如果你不想这样做,你仍然可以返回一个指针,虽然这会稍微改变语法。
答
你get_grid
和get_swarm
方法返回原始数组项目的副本。您应该将参考(或指针)返回至Grid
或Swarm
。
请尽可能包含[MCVE]。 – tambre
在C++中返回一个类就好像在“good old”C中返回一个结构一样;它会创建一个副本。 – molbdnilo
没有[mcve]很难说,但看起来问题是你没有返回向量中数据的引用。 – NathanOliver