C++ - 在创建时调用赋值运算符而不是复制构造函数

发布于 2024-09-24 08:37:09 字数 1262 浏览 6 评论 0原文

我想强制在结构之间进行显式转换,就像本机类型一样:

int i1;
i1 = some_float; // this generates a warning
i1 = int(some_float): // this is OK
int i3 = some_float; // this generates a warning

我想使用赋值运算符和复制构造函数来执行相同的操作,但行为不同:

Struct s1;
s1 = other_struct; // this calls the assignment operator which generates my warning
s1 = Struct(other_struct) // this calls the copy constructor to generate a new Struct and then passes that new instance to s1's assignment operator
Struct s3 = other_struct; // this calls the COPY CONSTRUCTOR and succeeds with no warning

是否有任何技巧可以获取第三种情况 Struct s3 = other_struct; 用默认构造函数构造s3,然后调用赋值运算符?

这一切都按预期编译和运行。 C++ 的默认行为是在创建新实例时调用复制构造函数而不是赋值运算符立即调用复制构造函数,(即 MyStruct s = other_struct; 变成 MyStruct s(other_struct); 不是 MyStruct s; s = other_struct;

。显式”关键字正是我所需要的!

class foo {
    foo(const foo& f) { ... }
    explicit foo(const bar& b) { ... }
    foo& operator =(const foo& f) { ... }
};

foo f;
bar b;
foo f2 = f; // this works
foo f3 = b; // this doesn't, thanks to the explicit keyword!
foo f4 = foo(b); // this works - you're forced to do an "explicit conversion"

I want to enforce explicit conversion between structs kind of like native types:

int i1;
i1 = some_float; // this generates a warning
i1 = int(some_float): // this is OK
int i3 = some_float; // this generates a warning

I thought to use an assignment operator and copy constructor to do the same thing, but the behavior is different:

Struct s1;
s1 = other_struct; // this calls the assignment operator which generates my warning
s1 = Struct(other_struct) // this calls the copy constructor to generate a new Struct and then passes that new instance to s1's assignment operator
Struct s3 = other_struct; // this calls the COPY CONSTRUCTOR and succeeds with no warning

Are there any tricks to get that third case Struct s3 = other_struct; construct s3 with the default constructor and then call the assignment operator?

This all compiles and runs as it should. The default behavior of C++ is to call the copy constructor instead of the assignment operator when you create a new instance and call the copy constructor at once, (i.e. MyStruct s = other_struct;becomes MyStruct s(other_struct); not MyStruct s; s = other_struct;. I'm just wondering if there are any tricks to get around that.

EDIT: The "explicit" keyword is just what I needed!

class foo {
    foo(const foo& f) { ... }
    explicit foo(const bar& b) { ... }
    foo& operator =(const foo& f) { ... }
};

foo f;
bar b;
foo f2 = f; // this works
foo f3 = b; // this doesn't, thanks to the explicit keyword!
foo f4 = foo(b); // this works - you're forced to do an "explicit conversion"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌梦深 2024-10-01 08:37:09

免责声明:我准备对此投反对票,因为这并不能回答问题。但这对OP可能有用。

我认为将复制构造函数视为默认构造+赋值是一个非常糟糕的主意。反之亦然:

struct some_struct
{
    some_struct();  // If you want a default constructor, fine
    some_struct(some_struct const&); // Implement it in the most natural way
    some_struct(foo const&);         // Implement it in the most natural way

    void swap(some_struct&) throw(); // Implement it in the most efficient way

    // Google "copy and swap idiom" for this one
    some_struct& operator=(some_struct x) { x.swap(*this); return *this; }

    // Same idea
    some_struct& operator=(foo const& x)
    {
        some_struct tmp(x);
        tmp.swap(*this);
        return *this;
    }
};

以这种方式实现是万无一失的,并且是您在 C++ 中的转换语义方面可以获得的最佳方式,因此这是这里的方法。

Disclaimer: I'm ready to take the downvotes on this, since this doesn't answer the question. But this could be useful to the OP.

I think it is a very bad idea to think of the copy constructor as default construction + assignment. It is the other way around:

struct some_struct
{
    some_struct();  // If you want a default constructor, fine
    some_struct(some_struct const&); // Implement it in the most natural way
    some_struct(foo const&);         // Implement it in the most natural way

    void swap(some_struct&) throw(); // Implement it in the most efficient way

    // Google "copy and swap idiom" for this one
    some_struct& operator=(some_struct x) { x.swap(*this); return *this; }

    // Same idea
    some_struct& operator=(foo const& x)
    {
        some_struct tmp(x);
        tmp.swap(*this);
        return *this;
    }
};

Implementing things that way is fool proof, and is the best you can obtain in term of conversion semantics in C++, so it is the way to go here.

御守 2024-10-01 08:37:09

如果您重载 other_struct 的类型转换运算符,并相应地编辑原始结构,则可以解决此问题。也就是说,它非常混乱,而且通常没有充分的理由这样做。


#include <iostream>

using namespace std;

struct bar;

struct foo {
    explicit foo() {
        cout << "In foo default constructor." << endl;
    }

    explicit foo(bar const &) {
        cout << "In foo 'bar' contructor." << endl;
    }

    foo(foo const &) {
        cout << "In foo constructor." << endl;
    }

    foo const & operator=(bar const &) {
        cout << "In foo = operator." << endl;
        return *this;
    }
};

struct bar {
    operator foo() {
        cout << "In bar cast overload." << endl;
        foo x;
        x = *this;
        return x;
    }
};

int main() {
    bar b;
    foo f = b;
    return 0;
}

输出:

在酒吧演员重载。
在 foo 默认构造函数中。
在 foo = 运算符中。
在 foo 构造函数中。
在 foo 构造函数中。

You can get around this if you overload the type cast operator for other_struct, and edit the original structure accordingly. That said, it's extremely messy and there generally isn't a good reason to do so.


#include <iostream>

using namespace std;

struct bar;

struct foo {
    explicit foo() {
        cout << "In foo default constructor." << endl;
    }

    explicit foo(bar const &) {
        cout << "In foo 'bar' contructor." << endl;
    }

    foo(foo const &) {
        cout << "In foo constructor." << endl;
    }

    foo const & operator=(bar const &) {
        cout << "In foo = operator." << endl;
        return *this;
    }
};

struct bar {
    operator foo() {
        cout << "In bar cast overload." << endl;
        foo x;
        x = *this;
        return x;
    }
};

int main() {
    bar b;
    foo f = b;
    return 0;
}

Outputs:

In bar cast overload.
In foo default constructor.
In foo = operator.
In foo constructor.
In foo constructor.
骷髅 2024-10-01 08:37:09

简而言之,不。

长版……其实就是这样。事实并非如此。但必须想出一些东西来满足角色要求。

In short, no.

The long version...actually that's about it. That's just not how it works. Had to come up with something to fill the character requirement though.

请爱~陌生人 2024-10-01 08:37:09

我不这么认为。当你编写

Struct s3 = other_struct;

它时,它看起来像一个赋值,但实际上它只是调用构造函数的声明性语法。

I don't think so. When you write

Struct s3 = other_struct;

It looks like an assignment, but really it's just declarative syntax that calls a constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文