来自同一对象的不同指针值
different pointer values from the same objects
我有一个非常奇怪的行为,我想知道我错过了什么。
我有一个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 应该可以工作。
相关文章:
- 为什么指针对象没有调用默认构造函数
- 如何访问双指针对象中的方法
- 如何使用智能指针对象执行成员函数指针
- 调用带有指针对象错误的指针变量
- 如何从模板类重载创建的指针对象上的运算符?
- C++指针对象的运算符重载
- 如何初始化数组指针对象
- 如何打印出唯一指针对象向量中的值(基于范围的循环)?C++
- C++指针对象差异
- 涉及继承,指针对象的交易以及何时进行的问题
- 表达式必须在C 中具有指针对象类型
- 下标需要数组或指针类型表达式必须具有指针对象类型
- 字符指针对象和相应的字符数组元素比较
- 如何在 c++ 中克隆为弱指针对象
- 指针对象使用CPP中的指针表达式进行比较
- Visual Studio中智能指针对象的自定义视图
- 使用指针 /对象 /模板参数正确构成
- 如果我确定只有一个线程一次处理指针/对象,则C/C 仍应使用同步
- 如何将值分配给指针对象 c++
- 更改智能指针对象,然后访问它