[英]Is it possible to overload both the () operator as a dereference and the = operator as assignment in same class?
I am working on a program that has a grid of object pointers that point to either a Null value or a derived child. 我正在开发一个程序,该程序具有指向Null值或派生子对象的对象指针网格。 I want to be able to set values on this grid to the address of their derived child such that I can "place" a child on the grid and access the child through their location in memory.
我希望能够在此网格上将值设置为其派生子代的地址,这样我就可以“将”一个子代“放置”在网格上并通过其在内存中的位置访问该子代。
Heres what the interface looks like for the grid. 这是网格的界面外观。
class Grid{
public:
virtual int get(g_type) const;
public:
parent* operator() (int,int);
Grid() : _amtCol(10),_amtRow(10)
{construct();}
~Grid() {deconstruct();}
private:
int _amtCol;
int _amtRow;
parent* **_grid;
private:
parent ***meddle(access);
void meddle(access, parent***);
virtual void construct();
virtual void deconstruct();
};
Heres what the () overload looks like. 这是()重载的样子。
parent* Grid::operator() (int i,int j){
if(i < get(r_QTY) && j < get(c_QTY)){
return this->meddle(key)[i+1][j+1];
}else{return NULL;}
}
What I want to be able to do is call this within the rest of my program as: 我想要做的是在程序的其余部分中将此称为:
Grid b;
Child c;
Child c2;
b(1,1) = &c;
b(1,4) = &c2;
b(1,1)->foo(); //calls the first Childs foo()
b(1,4)->foo(); //calls the second Childs foo()
The rest of my classes are created and work as far as inheritance and structure go. 我的其余课程都是在继承和结构方面创建并起作用的。
Is there a way that I could chain the overloads or something such that this works? 有什么方法可以链接重载或类似的东西吗?
I thought that perhaps I needed to iron out my assignment overloads in the parents and child class's but they seem to work great. 我以为也许我需要解决父母和孩子班上的作业超负荷问题,但他们似乎工作得很好。
///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///
An aside, I do have this implemented: 顺便说一句,我确实实现了这一点:
void Grid::operator() (int i,int j,parent &cpy){
if(i < get(r_QTY) && j < get(c_QTY)){
this->meddle(key)[i+1][j+1] = &cpy;
}
}
That does allows for this functionality. 确实允许使用此功能。
There is my dissertation! 有我的论文! Thanks!
谢谢!
////////////A quick addition: So perhaps I don't necessarily need to know if this is morally and ethically just. ///////////////////////////快速补充:因此,也许我不必知道这在道德上和道德上是否公正。 I have a way to implement the functionality that works.
我有一种方法来实现有效的功能。 I guess I do understand that using something that already exists in a library is to be preferred over my own creations but the fact that it is do-able if you use a std::vector for example means that it is possible.
我想我确实知道,使用库中已有的东西比我自己创建的东西更可取,但是如果您使用std :: vector例如它是可行的,则意味着有可能。 I am wondering how this is made possible and where it exists in the language's syntax.
我想知道如何做到这一点以及它在语言语法中的位置。
Not sure exactly what is your question, but you can do something like: 不确定确切的问题是什么,但是您可以执行以下操作:
struct parent
{
virtual ~parent() = default;
virtual void foo() = 0;
};
struct Child : parent
{
Child(int n) : n(n) {}
void foo() override { std::cout << n << std::endl;}
int n;
};
class Grid{
public:
Grid() : col(10), row(10), grid(col * row, nullptr) {}
parent* operator() (std::size_t x, std::size_t y) const {
if (col <= x || row <= y) { throw std::runtime_error("Invalid arguments"); }
return grid[y * col + x];
}
parent*& operator() (std::size_t x, std::size_t y) {
if (col <= x || row <= y) { throw std::runtime_error("Invalid arguments"); }
return grid[y * col + x];
}
private:
int col;
int row;
std::vector<parent*> grid;
};
And then you have: 然后您有:
Grid b;
Child c(1);
Child c2(2);
b(1,1) = &c;
b(1,4) = &c2;
b(1,1)->foo(); //calls the first Childs foo()
b(1,4)->foo(); //calls the second Childs foo()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.