C ++指针复制重载

问题描述 投票:1回答:1

我想重写指针副本,如下面的代码。 我尝试了很多东西,但我找不到方法......

template <typename T>
Class Test {
private:
    T* obj;
public:
    inline Test<T>& operator= (const Test<T>& other) {
    }
    inline Test<T>* operator= (const Test<T>* other) {
    }
    inline Test<T> operator= (const Test<T> other) {
    }
    ...
}
int main() {
    Test *a, *b;
    a = new Test;

    // *b = *a; // I know how to overload this copy
    b = a;   // But, I want to overload this pointer copy operator!
}
c++ overloading
1个回答
2
投票

但是,我想重载这个指针复制操作符!

你不能。指针赋值将始终采用内置操作。您不能为内置类型重载任何运算符 - 包括指针,即使它们指向类类型 - 也总是使用内置赋值。

© www.soinside.com 2019 - 2024. All rights reserved.