简体   繁体   English

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

[英]C++ data members. Goal: Initialize in constructor then leave alone, will const work here?

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? 我的问题是,是否有一种(非常简洁明了的)方式来指定成员(正如我用const前缀说明的那样),成员只能在构造函数中分配?

I know private/public and interface/methods could sort this, but humour me, how far can the const keyword be exploited? 我知道私有/公共和接口/方法可以对此进行排序,但令我感到幽默的是,可以利用const关键字多远?

_ EDIT _ _ 编辑 _

Nawaz, my goal is simply to initialize 2 of the 3 data members once only, in the constructor. Nawaz,我的目标只是在构造函数中只初始化一次3个数据成员中的2个。 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? 然后,我可以将此实例导出为const dweDMPair * ptrToVal,以便客户端无法再调用delete-,因为这对指向const实例的指针不起作用吗? The client will then proceed to give their own value for the third member prob . 然后,客户将继续为第三位成员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.. 如果您想知道..

Yes, const does what you want. 是的,const可以满足您的需求。 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. 您不能分配给const对象,但可以使用值对其进行初始化。 Also, regardless of making them const, you might want to consider encapsulating these members in a class as private members. 同样,无论使它们成为const,您都可能要考虑将这些成员封装为私有成员。 It's not much more typing, but it does improve maintainability. 它的类型不多,但确实提高了可维护性。

Edit: 编辑:

To define const pointer you do: 要定义const指针,请执行以下操作:

Type * const member;

not

const Type* member;

the second syntax is pointer to const type, not const pointer. 第二种语法是指向const类型的指针,而不是const指针。

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? 然后,我可以将此实例导出为const dweDMPair * ptrToVal,以便客户端无法再调用delete-,因为这对指向const实例的指针不起作用吗?

Delete can be called on pointers to const instances and it can be called on objects which contain const members. Delete可以在指向const实例的指针上调用,也可以在包含const成员的对象上调用。 This use of const will not help you reach your goal. const这种使用不会帮助您达到目标。

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). 您可能需要进一步澄清最终目标(可能会在另一个问题中是否会过多地改变这一目标)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM