Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6372851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:17:22+00:00 2026-05-25T01:17:22+00:00

I have the following struct dweDMPair { const dweller *occu; const double sqDM; float

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:17:23+00:00Added an answer on May 25, 2026 at 1:17 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following struct in C# class public struct Employee { public const
I have the following code struct Node { int accnumber; float balance; Node *next;
I have the following struct: struct Records { int Number; char Name[20]; float Salary;
I have the following code: struct Message { explicit Message(const std::string& message) : selfMessage(message)
I have two struct s defined as in the following: struct vertex { double
I have following struct to store my vertex data. struct Rz3DContourNode { float x;
i have the following struct: struct Message { Agent *_agent; double _val; }; and
I have the following struct: public struct Declarations { public const string SchemaVersion =
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have the following struct: struct Datastore_T { Partition_Datastores_T cmtDatastores; // bytes 0 to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.