小编fiz*_*uzz的帖子

为什么调用复制构造函数而不是移动构造函数?

我有以下代码:

#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)

c++ copy-constructor move-constructor move-semantics c++11

1
推荐指数
1
解决办法
149
查看次数

How to understand new operator overloading?

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)

c++ operator-overloading new-operator

1
推荐指数
1
解决办法
71
查看次数

为什么只调用派生类成员函数?

在下面的代码中:
请告诉我为什么只调用派生类成员函数,是因为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)

c++ inheritance overloading function method-hiding

0
推荐指数
1
解决办法
39
查看次数