我有以下代码:
#include <bits/stdc++.h>
using namespace std;
class A {
public:
A(const A& a) noexcept { cout << "copy constructor" << endl; }
A& operator=(const A& a) noexcept { cout << "copy assignment operator" << endl; }
A(A&& a) noexcept { cout << "move constructor" << endl; }
A& operator=(A&& a) noexcept { cout << "move assignment operator" << endl; }
A() { cout << "default constructor" << endl; }
};
vector<A> aList;
void AddData(const A&& a)
{
aList.push_back(std::move(a));
}
int …Run Code Online (Sandbox Code Playgroud) For "+" operator overloading, it is pretty easy to understand. c = c1.operator+(c2) is function notation, c = c1 + c2 is operator notation.
However, I just couldn't understand new operator overloading. In the following code:
Please tell me what happened when processing student * p = new student("Yash", 24); Why void * operator new(size_t size) is called. And why size is 28 when entering operator new(size_t size).
// CPP program to demonstrate
// Overloading new and delete operator …Run Code Online (Sandbox Code Playgroud) 在下面的代码中:
请告诉我为什么只调用派生类成员函数,是因为hiding吗?以及为什么在 中没有发生错误b.func(1.1);,参数是int x。
#include <bits/stdc++.h>
using namespace std;
class A {
public:
virtual int func(float x)
{
cout << "A";
}
};
class B : public A {
public:
virtual int func(int x)
{
cout << "B";
}
};
int main()
{
B b;
b.func(1); // B
b.func(1.1); // B
return 0;
}
Run Code Online (Sandbox Code Playgroud)