什么是 C++相当于 C# 的 readonly 字段修饰符?

发布于 2024-11-07 09:40:26 字数 318 浏览 3 评论 0原文

锁定状态很棒。在 C# 中,您可以通过将字段声明为只读来确保构造函数完成后字段不会更改其值/引用。

class Foo
{
    private readonly string _foo;

    public Foo() {
        _foo = "Unchangeable";
    }

    public void ChangeIt() {
        _foo = "Darn";        // compiler error
    }
}

我可以用 C++ 做同样的事情吗?如果是这样,怎么办?如果没有,为什么不呢?

Locking down state is great. In C# you can ensure that a field doesn't change it's value/reference once the constructor completes by declaring it as readonly.

class Foo
{
    private readonly string _foo;

    public Foo() {
        _foo = "Unchangeable";
    }

    public void ChangeIt() {
        _foo = "Darn";        // compiler error
    }
}

Can I do the same thing with C++? If so, how? If not, why not?

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

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

发布评论

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

评论(7

好菇凉咱不稀罕他 2024-11-14 09:40:26
class Foo
{
private:
    const string _foo;
public:
    Foo() : _foo("Unchangeable")
    {
    }
    void ChangeIt()
    {
        _foo = "Darn";        // compiler error
    }
};
class Foo
{
private:
    const string _foo;
public:
    Foo() : _foo("Unchangeable")
    {
    }
    void ChangeIt()
    {
        _foo = "Darn";        // compiler error
    }
};
山人契 2024-11-14 09:40:26

那将是 const。请注意,此关键字在不同的上下文中表示不同的含义。

That would be const. Note that this keyword means a couple of different things in different contexts.

听不够的曲调 2024-11-14 09:40:26

直接没有这样的事情。您可以将私有字段与公共 getter(但没有 setter)一起使用。但这仅适用于调用您的代码的其他类。 Foo 始终对其成员拥有完全访问权限。但由于您是 Foo 的实现者,所以这不是真正的问题。

There is no such thing directly. You can use a private field with a public getter (but no setter). But that would only apply to other classes calling your code. Foo always has full acces to its members. But since you are the implementer of Foo, this is no real problem.

墨离汐 2024-11-14 09:40:26

在阅读了已接受的答案后,我并不清楚要完全等同于 readonly 关键字,您需要像这样声明成员:

class Y
{
public:
     void mutate() { x = 7; } // not const member
     int x;
};

class X
{
private:
    Y * const member; // this only makes the pointer to Y const, 
                      // but you can still modify the object itself
public:
    X(Y *m) : member(m) {}

    void f() { member->mutate(); }
};

希望这会有所帮助。

It was not immediately clear to me after reading the accepted answer that to make exact equivalent of readonly keyword you need to declare the member like this:

class Y
{
public:
     void mutate() { x = 7; } // not const member
     int x;
};

class X
{
private:
    Y * const member; // this only makes the pointer to Y const, 
                      // but you can still modify the object itself
public:
    X(Y *m) : member(m) {}

    void f() { member->mutate(); }
};

Hope this helps.

罗罗贝儿 2024-11-14 09:40:26

C++ 中的引用不可重新绑定,因此它相当于 C# 只读引用。

A reference in C++ is not rebindable, so it's equivalent to a C# readonly reference.

小猫一只 2024-11-14 09:40:26

C++ 有 const,它的作用与 C# 中的 readonly 相同。

const int Constant1 = 96;    
Constant1 = 200   // Compiler Error.

C++ has const, which does the same job as readonly in C#.

const int Constant1 = 96;    
Constant1 = 200   // Compiler Error.
半山落雨半山空 2024-11-14 09:40:26

在进行从 C# 到 C++ 的转录时,我有与您相同的需求
这只是一个语法错误,因为该指令存在于 C++ (Visual C++)

语法中: [readonly]

“只读 C++ 属性与只读 MIDL 属性具有相同的功能。
如果要禁止修改方法参数,请使用 in 属性。”

来源:https://msdn.microsoft.com/en-us/library/45x4ky7s.aspx

来源:https://msdn.microsoft.com/library/windows/desktop/aa367152

I had the same need as you when doing a transcription from C# to C++
and it is just a syntax error because this instruction exists in C++ ( visual C++)

Syntax : [readonly]

"The readonly C++ attribute has the same functionality as the readonly MIDL attribute.
If you want to prohibit modification of a method parameter, then use the in attribute."

source : https://msdn.microsoft.com/en-us/library/45x4ky7s.aspx

source : https://msdn.microsoft.com/library/windows/desktop/aa367152

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