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 9109483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:03:14+00:00 2026-06-17T03:03:14+00:00

I have a relationship between two classes and some additional functional code illustrated in

  • 0

I have a relationship between two classes and some additional functional code illustrated in the below example. The MiddleMan class holds a couple of containers of pointers to DataObject class instances. I want to enforce read-only access to the data objects in one container, while allowing write access to the data containers in the other. Users of MiddleMan should never be able to modify the containers themselves directly (the ptr_vector members).

I guess there is const promotion happening to the DataObject pointers when accessed through the MiddleMan const member function. How can I avoid this? In my object relationship, MiddleMan does not logically own the DataObject instances. Thus, I do not want the DataObject instances const-protected.

I have used the boost::ptr_vector container as I understand the STL containers can be more problematic for this sort of thing. Didn’t solve my problem though.
I am happy to use const_cast in MiddleMan, but I don’t see how to.

// const correctness access through middleman example
// g++ -I/Developer/boost  ptr_container_ex.cc
#include <boost/ptr_container/ptr_vector.hpp>

struct DataObject
{
    DataObject(size_t n) : _data(new float[n]) {}
    ~DataObject() {delete _data;}
    float * dataNonConst() { return _data; }
    const float * dataConst() const { return _data; }
    float * _data;
};


struct MiddleMan
{

    typedef boost::ptr_vector<DataObject> containerVec_t;

    const containerVec_t & inputVars() const { return _inputVars; }
    const containerVec_t & outputVars() const { return _outputVars; }

    void addInputVar(  DataObject * in  ) { _inputVars.push_back( in ); }
    void addOutputVar( DataObject * out ) { _outputVars.push_back( out ); }

    containerVec_t _inputVars, _outputVars;

};

// just an example that the DataObject instances are managed externally
DataObject g_dataInstances[] = {DataObject(1), DataObject(2), DataObject(3)};

MiddleMan theMiddleMan;


int main()
{
    theMiddleMan.addInputVar( &g_dataInstances[0]);  // this is just setup
    theMiddleMan.addOutputVar( &g_dataInstances[1]);

    const MiddleMan & mmRef = theMiddleMan; // I actually only have a const ref to work with

    // read data example
    const MiddleMan::containerVec_t & inputs = mmRef.inputVars();
    float read = inputs[0].dataConst()[0];

    // write data example
    const MiddleMan::containerVec_t & outputs = mmRef.outputVars();
    float * data_ptr = outputs[0].dataNonConst(); // COMPILER ERROR HERE:

    return 0;
}

I’m getting the compiler output:

ptr_container_ex.cc: In function ‘int main()’:
ptr_container_ex.cc:49: error: passing ‘const DataContainer’ as ‘this’ argument of ‘float* DataContainer::dataNonConst()’ discards qualifiers
  • 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-06-17T03:03:15+00:00Added an answer on June 17, 2026 at 3:03 am

    One working scenario uses const_cast in accessing the outputVars. I’m not able to return a reference to the whole container, but I can get the functionality I need returning begin and end iterators.

    // const correctness access through middleman example
    // g++ -I/Developer/boost  ptr_container_ex.cc
    #include <boost/ptr_container/ptr_vector.hpp>
    
    struct DataObject
    {
        DataObject(size_t n) : _data(new float[n]) {}
        ~DataObject() {delete _data;}
        float * dataNonConst() { return _data; }
        const float * dataConst() const { return _data; }
        float * _data;
    };
    
    
    struct MiddleMan
    {
        typedef boost::ptr_vector<DataObject> containerVec_t;
    
        containerVec_t::iterator outputVarsBegin() const { return const_cast<containerVec_t&>(_outputVars).begin(); }
        containerVec_t::iterator outputVarsEnd() const { return const_cast<containerVec_t&>(_outputVars).end(); }
    
        const containerVec_t & inputVars() const { return _inputVars; }
    
        void addInputVar(  DataObject * in  ) { _inputVars.push_back( in ); }
        void addOutputVar( DataObject * out ) { _outputVars.push_back( out ); }
    
        containerVec_t _inputVars, _outputVars;
    
    };
    
    // just an example that the DataObject instances are managed externally
    DataObject g_dataInstances[] = {DataObject(1), DataObject(2), DataObject(3)};
    
    MiddleMan theMiddleMan;
    
    
    int main()
    {
        theMiddleMan.addInputVar( &g_dataInstances[0]);  // this is just setup
        theMiddleMan.addOutputVar( &g_dataInstances[1]);
    
        const MiddleMan & mmRef = theMiddleMan; // I actually only have a const ref to work with
    
        // read data example
        const MiddleMan::containerVec_t & inputs = mmRef.inputVars();
        float read = inputs[0].dataConst()[0];
    
        // write data example
        float * data_ptr2 = mmRef.outputVarsBegin()->dataNonConst(); // WORKS
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ManyToMany relationship between two classes, for instance class Customer and class
I have a relationship between two base classes: public abstract class RecruiterBase<T> { //
I have a simple one-to-many (models.ForeignKey) relationship between two of my model classes: class
I have the following relationship between two domain classes: class Emp { String name
I have a one-to-many relationship between two classes for this situation. I have a
In my model, I have two tables with a 1:0..1 relationship between them: (Example
I have a bidirectional one-to-many relationship between two classes (Protocol and History). While searching
I have a many-to-many relationship between two classes (Lesson and Student), with an intermediary
I have two domain classes with a many-2-many relationship between them, e.g. User and
I have defined a many-to-many relationship between my two entity classes User and Permission.

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.