gpt4 book ai didi

c++ - 像 C++ 设计模式这样的东西来避免指针?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:35:39 27 4
gpt4 key购买 nike

我有一个类层次结构,如下例所示,其中 State 包含 ZipCode 列表和 City 列表s,每个都包含指向 ZipCodes 的指针。

目标是能够更新 ZipCode 而无需更新 City(或创建 City 的新实例) .

下面的 C++ 代码满足这个要求,但它使用了指针,我希望避免使用指针,因为 thisthat . 我如何重新设计这个 [naive] 实现,使其不依赖指针?感谢您的帮助!

编辑:更新了下面的代码以使用 boost::shared_ptr 而不是原始指针。请注意,StateCityZipCode 只是示例名称,事实证明它们是糟糕的选择名称(我本可以选择“A”、“B”和“C”),因为实际代码允许 City 的等效项共享 ZipCode

#include <iostream>
#include <vector>
#include <boost/shared_ptr.hpp>

using namespace std;

/**
* Zone Improvement Plan (ZIP) code
*/
class ZipCode {
public:
ZipCode() : code_(0), plus4_(0) {}
ZipCode(int code, int plus4 = 0) : code_(code), plus4_(plus4) {}
virtual ~ZipCode() {};

int code() const { return code_; }
int plus4() const { return plus4_; }
void set_code(int code) { code_ = code; }
void set_plus4(int plus4) { plus4_ = plus4; }

private:
int code_;
int plus4_;
};

typedef boost::shared_ptr<ZipCode> ZipPtr;

/**
* City points to one or more zip codes
*/
class City {
public:
const vector<ZipPtr>& zip() const { return zip_; }
void add_zip_ptr(const ZipPtr x) { if (x != NULL) zip_.push_back(x); }

private:
// TODO: this vector should be a hash set
vector<ZipPtr> zip_;
};

/**
* State contains cities, each of which has pointers to
* zip codes within the state.
*/
class State {
public:
const vector<City>& city() const { return city_; }
const vector<ZipPtr>& zip() const { return zip_; }

const ZipPtr zip_of(int code) const {
for (size_t i = 0; i < zip_.size(); i++) {
if (zip_[i]->code() == code) {
return zip_[i];
}
}
return ZipPtr();
}

void add_city(const City& x) { city_.push_back(x); }
void add_zip(int code) { zip_.push_back(ZipPtr(new ZipCode(code))); }

private:
// TODO: these vectors should be hash sets
vector<City> city_;
vector<ZipPtr> zip_;
};

int main() {
State texas;
City dallas, houston;

// create state ZIPs
texas.add_zip(75380);
texas.add_zip(75381);
texas.add_zip(77219);
texas.add_zip(77220);

// point city ZIPs to the ones we just created
dallas.add_zip_ptr(texas.zip_of(75380));
dallas.add_zip_ptr(texas.zip_of(75381));
houston.add_zip_ptr(texas.zip_of(77219));
houston.add_zip_ptr(texas.zip_of(77220));

// print all ZIPs
cout << "ZIPs in Texas: " << endl;
const vector<ZipPtr>& zips = texas.zip();
for (size_t i = 0; i < zips.size(); i++) {
cout << " " << zips[i]->code() << endl;
}
cout << "ZIPs in Dallas, Texas: " << endl;
const vector<ZipPtr> zip_ptrs1 = dallas.zip();
for (size_t i = 0; i < zip_ptrs1.size(); i++) {
cout << " " << zip_ptrs1[i]->code() << endl;
}
cout << "ZIPs in Houston, Texas: " << endl;
const vector<ZipPtr> zip_ptrs2 = houston.zip();
for (size_t i = 0; i < zip_ptrs2.size(); i++) {
cout << " " << zip_ptrs2[i]->code() << endl;
}

// change a state ZIP...
cout << "Changing Houston's ZIP 77220..." << endl;
ZipPtr z = texas.zip_of(77220);
if (z != NULL) z->set_code(88888);

// ...and show the ZIPs of the affected city
cout << "ZIPs in Houston, Texas: " << endl;
const vector<ZipPtr> zip_ptrs3 = houston.zip();
for (size_t i = 0; i < zip_ptrs3.size(); i++) {
cout << " " << zip_ptrs3[i]->code() << endl;
}

return 0;
}

最佳答案

我将这种情况视为两个 1:n 关系

  1. 州 : 城市 == 1 : n

  2. 城市:邮政编码== 1 : n

基于此,我认为 State 包含

vector<ZipCode> zip_;

不是声音。

我可能会

class State {
vector< City > cities_in_state_;
};

class City {
vector< Zipcode > zips_in_city_;
};

这不需要指针。

关于c++ - 像 C++ 设计模式这样的东西来避免指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4904995/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com