在 C++ 中使用指向对象的指针和 operator new 来定义对象
Defining an object with a pointer to an object and operator new in c++
关于使用指向对象的指针定义对象的简单示例。我们定义一个对象 A *a = new A(123.4);
然后另一个对象 A *b = new A(*a);
我不明白的是,这对 b(指向)对象究竟是如何工作的?复制构造函数如何在这里启动并将值初始化为与对象 a 相同的值?我认为为了让它起作用,我们应该在 class.
中声明一个自定义复制构造函数
#include <iostream>
using namespace std;
class A {
public:
A(float v) { A::v = v; }
float v;
float set(float v) {
A::v = v;
return v;
}
float get(float v) {
return A::v;
}
};
int main() {
A *a = new A(123.4);
A *b = new A(*a);
cout << a->v << endl;
cout << b->v << endl;
a->v = 0.0;
cout << a->v << endl;
cout << b->v << endl;
b->v = 1.0;
cout << a->v << endl;
cout << b->v << endl;
return 0;
}
程序的输出是:
123.4
123.4
0
123.4
0
1
提前致谢。
编译器为您生成了复制构造函数。它将执行成员变量的浅拷贝,这对你的情况来说已经足够了。
来自Wikipedia:
Special member functions in C++ are functions which the compiler will automatically generate if they are used, but not declared explicitly by the programmer. The special member functions are:
- Default constructor (if no other constructor is explicitly declared)
- Copy constructor if no move constructor or move assignment operator is explicitly declared. If a destructor is declared, generation of a copy constructor is deprecated.
- Move constructor if no copy constructor, move assignment operator or destructor is explicitly declared.
- Copy assignment operator if no move constructor or move assignment operator is explicitly declared. If a destructor is declared, generation of a copy
assignment operator is deprecated.
- Move assignment operator if no copy constructor, copy assignment operator or destructor is explicitly declared.
- Destructor
关于使用指向对象的指针定义对象的简单示例。我们定义一个对象 A *a = new A(123.4);
然后另一个对象 A *b = new A(*a);
我不明白的是,这对 b(指向)对象究竟是如何工作的?复制构造函数如何在这里启动并将值初始化为与对象 a 相同的值?我认为为了让它起作用,我们应该在 class.
中声明一个自定义复制构造函数#include <iostream>
using namespace std;
class A {
public:
A(float v) { A::v = v; }
float v;
float set(float v) {
A::v = v;
return v;
}
float get(float v) {
return A::v;
}
};
int main() {
A *a = new A(123.4);
A *b = new A(*a);
cout << a->v << endl;
cout << b->v << endl;
a->v = 0.0;
cout << a->v << endl;
cout << b->v << endl;
b->v = 1.0;
cout << a->v << endl;
cout << b->v << endl;
return 0;
}
程序的输出是:
123.4
123.4
0
123.4
0
1
提前致谢。
编译器为您生成了复制构造函数。它将执行成员变量的浅拷贝,这对你的情况来说已经足够了。
来自Wikipedia:
Special member functions in C++ are functions which the compiler will automatically generate if they are used, but not declared explicitly by the programmer. The special member functions are:
- Default constructor (if no other constructor is explicitly declared)
- Copy constructor if no move constructor or move assignment operator is explicitly declared. If a destructor is declared, generation of a copy constructor is deprecated.
- Move constructor if no copy constructor, move assignment operator or destructor is explicitly declared.
- Copy assignment operator if no move constructor or move assignment operator is explicitly declared. If a destructor is declared, generation of a copy assignment operator is deprecated.
- Move assignment operator if no copy constructor, copy assignment operator or destructor is explicitly declared.
- Destructor