C++数据成员。目标:在构造函数中初始化然后不管,const在这里可以工作吗?

发布于 2024-12-01 14:13:00 字数 676 浏览 1 评论 0原文

我有以下内容,

    struct dweDMPair {
        const dweller   *occu;
        const double    sqDM;
        float   prob;
        dweDMPair(dweller *inOccu, double sqdm) : occu(inOccu), sqDM(sqdm) {}       };

我想返回指向这些对象的指针数组,但希望客户端不会意外删除它们。或者,呃,也许不是,只是尝试这个设计迭代。

我的问题是,是否有一种(非常简洁和简洁)的方式来指定(正如我用 const 前缀说明的那样)成员仅在构造函数中分配?

我知道私有/公共和接口/方法可以对此进行排序,但是幽默一下, const 关键字可以被利用到什么程度?

_编辑_

Nawaz,我的目标只是在构造函数中仅初始化 3 个数据成员中的 2 个一次。然后我可以将此实例导出为 const dweDMPair *ptrToVal,以便客户端无法调用 delete- 因为这不适用于指向 const 实例的指针,不是吗?然后,客户端将继续为第三个成员 prob 提供自己的值。是的,我知道函数会加强保护,但我希望它更快。

成员不能是静态的,而是实例成员。如果您想知道..

I have the following

    struct dweDMPair {
        const dweller   *occu;
        const double    sqDM;
        float   prob;
        dweDMPair(dweller *inOccu, double sqdm) : occu(inOccu), sqDM(sqdm) {}       };

I want to return an array of pointers to these objects but desire that they should not be accidentally deleted by the client. Or, err, maybe not, just trying this design iteration.

My question is, is there a (very concise and neat) way of specifying (as I have illustrated with a const prefix) that members are only ever assigned in the constructor?

I know private/public and interface/methods could sort this, but humour me, how far can the const keyword be exploited?

_EDIT_

Nawaz, my goal is simply to initialize 2 of the 3 data members once only, in the constructor. Then I can export this instance as a const dweDMPair *ptrToVal so that the client cannot then call delete- because that won't work on pointers to const instances will it? The client will then proceed to give their own value for the third member prob. yeah, I know functions enforce protection but I want it faster.

The members can't be static but instance members. In case you were wondering..

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

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

发布评论

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

评论(2

南街九尾狐 2024-12-08 14:13:00

是的,const 可以满足你的要求。请注意,您在构造函数初始值设定项列表中所做的不是赋值,而是初始化。您不能分配给 const 对象,但可以使用值初始化它们。
另外,无论是否将它们设置为常量,您可能都需要考虑将这些成员作为私有成员封装在类中。打字不多,但确实提高了可维护性。

编辑:

要定义 const 指针,您需要执行以下操作:

Type * const member;

不是指向 const 类型的指针,而不是 const 指针。

const Type* member;

第二个语法

Yes, const does what you want. Note that what you do in the constructor initializer list is not assignment, it's initialization. You can't assign to const objects, but you can initialize them with a value.
Also, regardless of making them const, you might want to consider encapsulating these members in a class as private members. It's not much more typing, but it does improve maintainability.

Edit:

To define const pointer you do:

Type * const member;

not

const Type* member;

the second syntax is pointer to const type, not const pointer.

始于初秋 2024-12-08 14:13:00

您澄清您真正想要的是:

然后我可以将此实例导出为 const dweDMPair *ptrToVal,以便客户端无法调用 delete- 因为这不适用于指向 const 实例的指针,不是吗?

可以在指向 const 实例的指针上调用删除,也可以在包含 const 成员的对象上调用它。使用 const 将不会帮助您实现目标。

一些编译时没有抱怨的示例代码:

struct dweller {
};

 struct dweDMPair {
        const dweller   *occu;
        const double    sqDM;
        float   prob;
        dweDMPair(dweller *inOccu, double sqdm) : occu(inOccu), sqDM(sqdm) {}       
 };

int main() {

    dweDMPair const* p = new dweDMPair(NULL, 3.14);

    delete p;
}

您可能想进一步阐明您的最终目标(可能在另一个问题中,如果它会太多地改变这个问题)。

You clarify that what you really want is:

Then I can export this instance as a const dweDMPair *ptrToVal so that the client cannot then call delete- because that won't work on pointers to const instances will it?

Delete can be called on pointers to const instances and it can be called on objects which contain const members. This use of const will not help you reach your goal.

Some example code that compiles without complaint:

struct dweller {
};

 struct dweDMPair {
        const dweller   *occu;
        const double    sqDM;
        float   prob;
        dweDMPair(dweller *inOccu, double sqdm) : occu(inOccu), sqDM(sqdm) {}       
 };

int main() {

    dweDMPair const* p = new dweDMPair(NULL, 3.14);

    delete p;
}

You might want to further clarify your end-goal (possibly in another question if it would change this one too much).

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