-3
我有這樣的當前設置所謂的禁忌:C++使用的拷貝構造函數是由PriorityQueue中
#include <iostream>
#include <queue>
#include <vector>
class A
{
int val;
public:
A()
{
std::cout << "Inside A. Constructor with val =" << val << ".\n";
}
A(const A& msg)
{
std::cout << "Inside const A. Never want to come here. val =" << msg.val << ".\n";
}
bool operator()(const A* m1, const A* m2)
{
std::cout << "Inside() function.\n";
std::cout << "m1: " << m1->GetVal() << " m2: " << m2->GetVal()<<"\n";
return (m1->GetVal() < m2->GetVal());
}
void setVal(int input) { std::cout << "inside setVal.\n"; val = input; }
int GetVal()const { return val; }
};
void specialPriorityQueue()
{
//init
A* val = new A();
val->setVal(5);
std::priority_queue<A*, std::vector<A*>, A> pq;
pq.push(val);
A* val2 = new A();
val2->setVal(3);
pq.push(val2);
delete val;
delete val2;
}
int main()
{
specialPriorityQueue();
return 0;
}
輸出:
Inside A. Constructor with val =-85000000...
inside setVal.
Inside A. Constructor with val =-85000000...
Inside const A. Never want to come here. val =-85000000....
Inside A. Constructor with val =-85000000...
inside setVal.
Inside const A. Never want to come here. val =-85000000....
Inside() function.
m1: 5 m2: 3
我的問題是:有沒有辦法避免使用拷貝構造函數優先隊列。這會導致未定義的行爲。我無法刪除該功能,因爲它在別處有太多的依賴關係。
如果您的複製構造函數導致UB,請修復它。 – juanchopanza
@juanchopanza在這種情況下它不。 –
您是否考慮過刪除複製構造函數並使用'A(const A&)= delete'和'A&operator =(const A&)= delete;'複製賦值運算符? –