当友元函数 mag() 在类内部定义时,下面显示的代码不会编译,但如果在类外部定义(已注释),则可以工作。我认为差异是由用于将参数类型从 A 更改为 B 的复制构造函数引起的。有人可以解释为什么我应该在外部定义友元函数吗?
而且,如果B类是模板类(template <class T>在最上面添加),在外面定义友元函数也是不行的。
#include <iostream>
using namespace std;
class A {
};
class B {
public:
B(const A& p) {
std::cout << "Copy/Conversion constructor" << std::endl;
}
friend void mag(const B& p) {
std::cout << "Mag Inside`.\n";
}
};
//void mag(const B& p) {
// std::cout << "Mag Outside.\n";
//}
int main() {
A a;
mag(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 据我所知,理论上,如果一个类有一个原始指针成员,那么默认的复制构造函数将对该指针进行浅表复制,这样当原始对象被销毁时,副本中的指针成员将具有该值它指向的已删除。这似乎意味着,除了我们出于某种原因想要限制复制的情况外,任何具有原始指针成员的类都应该定义一个复制构造函数来对该指针进行深层复制。
我正在使用一个受人尊敬的第三方 API,并且遇到了一个带有原始指针成员的类,但没有定义的复制构造函数,这对我上面的理解产生了怀疑。我错过了什么吗?
更新:第三方告诉我这个类不应该被复制,因为该对象代表一个视觉元素。他们指出他们应该创建一个私有的复制构造函数。
我正在尝试创建一个包含类实例的向量,该类实例又包含(除其他外)std::atomic。
我已经尝试过以下几种:
如果指定了复制构造函数,我尝试了两件事:
对于 foo(foo& other) ,它会抱怨没有找到 foo 的复制构造函数。
编辑:复制构造函数是 foo(foo& other) :atomic(other.atomic.load()) {}
对于 foo(const foo& other) ,它会抱怨 std::atomic 没有 const 复制构造函数。
编辑:复制构造函数是 foo(const foo& other) :atomic(other.atomic.load()) {}
我完全不知道如何解决这个问题,所以非常感谢任何帮助
我有 2 个课程,User,和BBoard(公告板)。
我有一个成员函数BBoard,它检查从文件中读取的数据并将其推回向量userList。我还有一个成员函数来检查 a 是否User存在(如果User存在,我想使用默认的复制构造函数并将其分配给currentUser)
这些是我在 BBoard 中的私有变量:
// private:
// std::string title;
// std::vector<User> userList;
// User currentUser;
// std::vector<Message> messageList;
Run Code Online (Sandbox Code Playgroud)
BBoard这是推回s的成员函数User:
while (inFS >> dataName && inFS >> dataPass) {
User x(dataName, dataPass);
userList.push_back(x);
}
Run Code Online (Sandbox Code Playgroud)
BBoard这是检查 a 是否User存在并尝试使用默认复制构造函数将其分配给 的成员函数currentUser。
bool BBoard::userExists(const string& uName, const string& uPass) const {
for (int i = 0; i < …Run Code Online (Sandbox Code Playgroud) 我有一个 A 级,有一个参考成员num。我编写了一个num在初始化列表中初始化的复制构造函数。但结果好像很奇怪,打印出来的值不应该是100吗?a.num我的程序什么时候修改了和的值aa.num?
#include <iostream>
using namespace std;
class A{
public:
int& num;
A(int n):num(n){}
A(const A& obj):num(obj.num){}
void print(){
cout << num << endl;
}
};
int main(){
A a(100);
A aa = a;
a.print(); //Expected to be 100, but it isn't
aa.print(); //Also expected to be 100, but it isn't
//The address of a.num and aa.num are the same, so both of them are referencing to the same place. But …Run Code Online (Sandbox Code Playgroud) 当然,返回类型会有所不同,但概念是相同的:将数据从一个对象复制到另一个对象,对吧?
c++ operator-overloading copy-constructor assignment-operator
template<typename T>
class TSQueue {
public:
TSQueue() {}
TSQueue(const TSQueue& rhs) {
lock_guard<mutex> lg1(rhs._mutex);
_data = rhs._data;
}
private:
queue<shared_ptr<T> > _data;
mutex _mutex;
};
Run Code Online (Sandbox Code Playgroud)
我在教科书中看到,只有源代码(rhs)被锁定在复制构造函数中。我不确定如何仅通过锁定 rhs 来确保对目的地(此)的独占访问。我认为源和目的地都应该被锁定。编写线程安全复制构造函数的理想方法是什么?
#include <iostream>
#include <memory>
using namespace std;
class Init {
private:
int x;
public:
Init(int y) {
x = y;
cout << "default constructor called" << endl;
}
Init(std::shared_ptr<Init> z) {
this->x = z->x;
cout << "copy constructor called" << endl;
}
};
int main()
{
int k = 5;
std::shared_ptr<Init> a = std::make_shared<Init>(k);
std::shared_ptr<Init> b(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的期望是同时调用默认构造函数和复制构造函数,但只调用默认构造函数。可能是什么问题?
输出是: 默认构造函数称为
c++ smart-pointers copy-constructor shared-ptr default-constructor
我有一个 std::unordered_map ,我发现我插入其中的对象与我通过使用范围遍历从中获得的对象不同。
我怀疑这里可能发生了一些对象复制,但是在我向复制构造函数添加了一些转储之后,它根本没有被调用。
谁能告诉我插入和遍历 std::unordered_map 时后台发生了什么?
我尝试了以下代码并转储:
[结果]
mypair constuctor
mypair constuctor
string1 address:0x7ffccb813ba0
string2 address:0x7ffccb813bd0
++++++++++++++++
auto &x address:0x55fb40529378
string2: 0.5
auto &x address:0x55fb405292c8
string1: 0.3
++++++++++++++++
auto x address:0x7ffccb813c00
string2: 0.5
auto x address:0x7ffccb813c00
string1: 0.3
++++++++++++++++
Run Code Online (Sandbox Code Playgroud)
[来源]
#include <iostream>
#include <string>
#include <unordered_map>
class mypair : public std::pair<std::string,double> {
public:
mypair(std::string str, double num):std::pair<std::string,double>(str, num) {
std::cout<<"mypair constuctor"<<std::endl;
}
mypair( const mypair& ) {
std::cout<<"mypair copy constuctor"<<std::endl;
}
mypair& operator=(const mypair&) {
std::cout<<"mypair copy assignment"<<std::endl;
return …Run Code Online (Sandbox Code Playgroud) 我创建了一个向量并使用push_back将几个节点对象放入其中。但是,我无法预测何时将使用移动构造函数或复制构造函数。
当push_back使用复制构造函数或移动构造函数时有什么模式吗?c++参考说它有时会复制,有时会移动,但从未详细说明它们何时做什么。
#include <iostream>
#include <vector>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include <map>
#include <queue>
using namespace std;
struct Node {
int val;
Node(int val) : val(val) {
cout<<"created object" << val<<endl;
}
Node(const Node& m) : val(m.val) {
cout<<"copy constructor is called on value " << m.val << endl;
}
~Node() {
cout<<"destroyed val" << val<<endl;
}
Node(Node&& other) noexcept
: val(other.val) {
cout<<"moved val " << other.val << endl;
}
};
void f(vector<Node>& a) {
cout<<"______________________"<<endl;
Node …Run Code Online (Sandbox Code Playgroud) c++ ×10
copy-constructor ×10
atomic ×1
copy ×1
destructor ×1
mutex ×1
reference ×1
shared-ptr ×1
stl ×1
templates ×1
vector ×1