来自同一对象的不同指针值

different pointer values from the same objects

本文关键字:指针 对象      更新时间:2023-10-16

我有一个非常奇怪的行为,我想知道我错过了什么。

我有一个Bus对象,它代表一个图形节点。

我有一个表示图形边缘的Branch对象。

然后我有一个Circuit对象来托管公共汽车和分支。

Circuit对象具有一个函数addBus,该函数创建Bus对象,将副本存储在向量中,并返回指向向量中Bus对象的指针。

分支使用这些指针来"记住"它连接到哪个两个节点。请参阅代码中的 main 函数。

Circuit中,函数compile_all将创建一个字典,其中包含指向总线向量中Bus实例的指针,以及它们对应的向量索引。这允许我对总线和分支进行编号,并构建将在电路矩阵计算中使用的邻接矩阵等。

问题是,我从addBus函数获得的指向Bus实例的指针与我在compile_all函数中再次循环总线向量时得到的指针是不同的。

我在矢量中插入对象时的指针值:

ptr: 0x555d54bfc0d0
ptr: 0x555d54bfc268
ptr: 0x555d54bfc4d0
ptr: 0x555d54bfc598
ptr: 0x555d54bfc990
ptr: 0x555d54bfca58

指针值,当我在 compile_all 函数中循环向量时:

set: 0x55b59aafd670
set: 0x55b59aafd738
set: 0x55b59aafd800
set: 0x55b59aafd8c8
set: 0x55b59aafd990
set: 0x55b59aafda58

我不明白为什么这些不同,因为它们指向的对象是相同的。

/**
* Bus bar
*/
template <int n_phase> class Bus{

public:
string name;
/**
* @brief Bus constructor
*/
Bus(string name_){
name = name_;
}
};

/**
* General pi-model branch element
*/
template <int n_phase> class Branch{

public:

/**
* @brief Node connection 1
*/
Bus<n_phase> * from;
/**
* @brief Node connection 2
*/
Bus<n_phase> * to;

/**
* @brief Branch
* @param z0_
* @param z1_
*/
Branch(Bus<n_phase> * from_, Bus<n_phase> * to_){
from = from_;
to = to_;
}
};
/**
* general single-island circuit model.
*/
template <int n_phase> class Circuit{

private:
std::vector<Bus<n_phase>> buses;
std::vector<Branch<n_phase>> branches;
std::map<Bus<n_phase>*, int> bus_dict;
public:
/**
* @brief Circuit
*/
Circuit(){
}

/**
* @brief Add a bus to the circuit
* @return poiter to the added bus object
*/
Bus<n_phase>* addBus(string name_){
buses.push_back(Bus<n_phase>(name_));
return &buses[buses.size()-1];
}

/**
* @brief Add a branch to the circuit
* @return pointer to the branch objects that has been added
*/
Branch<n_phase>* addBranch(Bus<n_phase>* bf, Bus<n_phase>* bt){
branches.push_back(Branch<n_phase>(bf, bt));
return &branches.back();
}

/**
* @brief compile all the data into matrices for calculation
*/
void compile_all(){
// initialize variables
int n = buses.size();
int m = branches.size();
int f, t;
// initialize structures
bus_dict.clear();
// loop through the nodes
for (int i = 0; i < n; i++){
// create entry in the bus_dict dictionary
bus_dict[&buses[i]] = i;
}
std::cout << std::endl;
// loop through the branches
for (int i = 0; i < m ; i++){
f = bus_dict[branches[i].from];
t = bus_dict[branches[i].to];
// add connectivity relation
connectivity.set_relation(f, t, i);
}
}

};

void main(){
cout << "Test Engine 1" << endl;
const int phases = 3;
Circuit<phases> circuit;
Bus<phases>* b0 = circuit.addBus("B0");
Bus<phases>* b1 = circuit.addBus("B1");
Bus<phases>* b2 = circuit.addBus("B2");
Bus<phases>* b3 = circuit.addBus("B3");
Bus<phases>* b4 = circuit.addBus("B4");
Bus<phases>* b5 = circuit.addBus("B5");
circuit.addBranch(b0, b1);
circuit.addBranch(b0, b2);
circuit.addBranch(b2, b3);
circuit.addBranch(b3, b5);
circuit.addBranch(b2, b4);
circuit.addBranch(b4, b5);
circuit.compile_all();
};

std::d eque 或 std::list 应该可以工作。