小编mat*_*atn的帖子

Why can delete operator be used in const context?

This question is different from:


I wrote a class Test like this.

class Test {

    private:

        int *p;

    public:

        //constructor
        Test(int i) {
            p = new int(i);
        }

        Test & operator = (const Test &rhs) {
            delete p;
            p = new int(*(rhs.p));
            return *this;
        }

};
Run Code Online (Sandbox Code Playgroud)

When the parameter rhs of the operator function is itself (i.e. …

c++ language-lawyer

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

如何从之前定义的另一个函数调用函数模板?

我有两个函数模板A和B.我在同一个文件中定义A然后定义B. 现在我想在A中打电话给B.我怎么能意识到这一点?正常功能原型在这种情况下不起作用.(请假设您无法更改A和B或拆分文件的顺序.)

#include <iostream>

template <class Type>
Type A(Type x) {
    return 2 * B(x);
}

template <class Type>
Type B(Type x) {
    return 3 * x;
}

int main() {

    int x = 3;
    std::cout << A(x) << "\n"; //=> ERROR

}
Run Code Online (Sandbox Code Playgroud)

来自g ++的错误:

test.cpp: In instantiation of ‘Type A(Type) [with Type = int]’:
test.cpp:40:21:   required from here
test.cpp:29:17: error: ‘B’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of …
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×2

language-lawyer ×1