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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:35:34+00:00 2026-05-22T17:35:34+00:00

The title is a bit vague but I can’t come up with a better

  • 0

The title is a bit vague but I can’t come up with a better wording, here’s the deal:

class A
{
public:
  A();
  A( const PropertyB& b );
  PropertyA GetPropertyA();
  PropertyB GetPropertyB();
  SetPropertyA( const PropertyA& b );
  SetPropertyB( const PropertyB& b );
  //assignment not allowed
};

Suppose I want to use an std::vector< A >, but, it only makes sense to have a vector of A if all it’s elements have the same value for PropertyB. The current solutions is to supply a contrcutor-like method to create such array which guarantees all elements of the returned array have the same value for PropertyB, and a method that checks if that is the case:

Array MakeArray( size_t, const PropertyB& );
bool CheckIfArrayIsSane( const Array& );

So users can still call SetPropertyB() on the elements, but have a utility to check it and bail out if someone did:

Array x( MakeArray( 3, someValue ) );
x.SetPropertyA( aaa );             //should be allowed
x.SetPropertyB( someOtherValue );  //should NOT be allowed
//somewhat further in the program
if( !CheckIfArrayIsSane( x ) )
  throw InsaneArrayExcpetion();

While this works, it’s error-prone since it is hard to force this check everywhere and not forget it, and clutters the code with checks.

Approcahes that do not work:

  • Making SetPropertyB() private, and making MakeArray a friend function: then SetPropertyB() is not accessible anymore for users that simply want to use A without caring about the array.
  • Wrapping std::vector< A > in a seperate class and only returning const A& references: this would mean the other setters like SetPropertyA() can also not be called, but users should be able to call them. It’s only SetPropertyB() that should be disallowed.

One more intrusive approach that would work but feels a bit unelegant, and needs extra functions to convert between the two etc:

class AWithFixedPropertyB
{
public:
  A( const PropertyB& b );
  PropertyA GetPropertyA();
  PropertyB GetPropertyB();
  SetPropertyA( const PropertyA& b );
};

class A : public AWithFixedPropertyB
{
public:
  //contrcutors etc
  SetPropertyB( const PropertyB& b );
};

//ok, users cannot modify property B in this Array
typedef std::vector< AWithFixedPropertyB > Array;

What would be the most elegant solution for this problem?

  • 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-22T17:35:35+00:00Added an answer on May 22, 2026 at 5:35 pm

    This doesn’t make sense, from an OO design viewpoint. Remember the Liskov Susbsitution Principle: A is-a B whenever an A object can be used in the place of a B object? Per that rule, the elements of your proposed std::vector<A> fail the is-an-A test, since you can’t set their B property. Yet std::vector<A> is supposed to be a container of A objects.

    In C++ we have private inheritance, and this makes explicit that the derived class does not have an is-a relationship with its parent. You can use that as follows:

    class A_fixed_B : private A 
    {
        A_fixed_B(A const& src) : A(src) {}
        A const& asA() const { return *this; } // Base Conversion is OK inside class.
        using A::GetPropertyA;
        using A::GetPropertyB;
        using A::SetPropertyA;
     };
    

    You can now create a std::vector<A_fixed_B> which behaves as you’d probably expect.

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

Sidebar

Related Questions

Ok, the title might sound a bit vague but I really can't think of
The title might have been a bit confusing but I can clarify here. I'm
The title is a bit vague, but let me tell you what I want
The title is a bit wonky but it's the best I could come up
I'll admit the title is a bit confusing but it was hard to come
Ok, the title is a bit confusing, but here's my predicament. I've made a
The title may be a bit ambiguous, but I couldn't think of a better
The title is a bit awkward but I couldn't found a better one. My
Forgive me if the title (and my SQL knowledge) seems a bit vague but
I know the question title isn't amazing, but I can't think of a better

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.