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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:02:03+00:00 2026-06-01T13:02:03+00:00

It seems to be the prevailing opinion that using protected data members is a

  • 0

It seems to be the prevailing opinion that using protected data members is a bad idea. I’m wondering what a good alternative would be in a specific case.

Take the following class called CModule, which represents a audio module (Amiga-style tracker music). Many different module formats exist but the only difference between them is in file format (Loading) and audio effects handling. CModule holds all the common functionality, the derived classes implement loading and effects for each specific format.

class CModule
{
public: 
        CModule(string Filename); //Song file name to load.

        //Common methods...
        void Play();
        void Stop(); //Etc...

protected:

        //Derived class should implement these with format specific code.
        //Base class code calls these when needed.
        virtual void Load()=0;
        virtual void Effects()=0;

        //Song information/data.
        vector<CInstrument> Instruments;
        vector<CPattern> Patterns;
        //And much, MUCH more...
};

Almost all the data members are protected since the derived class’s Load() function needs to fill them all. This is considered bad since it can break encapsulation if someone derives a class from the derived class. What would be the proper way to solve this problem? I’ve already found that using getters/setters is considered bad too.

Many thanks to anyone taking the time to read this 🙂

  • 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-01T13:02:04+00:00Added an answer on June 1, 2026 at 1:02 pm

    There’s nothing wrong with using protected data members if using private won’t work for your solution, using public data members however is almost never a good idea (however it can be sometimes too).

    I would probably make your vectors private in this case but simply create getter and setter methods. Something along the lines of:

    class CModule
    {
    public: 
            CModule(string Filename); //Song file name to load.
    
            //Common methods...
            void Play();
            void Stop(); //Etc...
    
    protected:
    
            //Derived class should implement these with format specific code.
            //Base class code calls these when needed.
            virtual void Load()=0;
            virtual void Effects()=0;
    
            void AddInstrument(CInstrument instrument)
            {
                Instruments.push_back(instrument);
            }
    
            Instrument GetInstrument(int index)
            {
                return Instruments[index];
            }
    
            int InstrumentCount()
            {
                return Instruments.size();
            }
    private:
            //Song information/data.
            vector<CInstrument> Instruments;
            vector<CPattern> Patterns;
            //And much, MUCH more...
    };
    

    This is a start just for instruments, you’ll also have to take a similar approach with patterns. Alternatively you could also just pass back the vector but this is a little more encapsulated.

    Also note that I am doing this off the top of my head and haven’t tested it against typos of whatever but hopefully it conveys the idea.

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

Sidebar

Related Questions

Seems like this is the kind of thing that would have already been answered
Seems like it would be a good way to introduce some people to unit
Seems that requirements on safety do not seem to like systems that use AI
Seems the Google-recommended way (using the android support library) for a fragment or activity
Seems,that very basic question. Anyway can't get the meaning of next definition from Spring
Seems that there is only doubly linked list (but no singly linked list) in
Seems that web.xml for a servlet has an element called <enabled>false</enabled> that can be
Seems that there are not much work done on Haskell for web. People uses
Seems to me it is a bit wierd that you can do.. Page.ClientScript.RegisterStartupScript(this.GetType(), KeyName,
Seems that the JavaScript debugger is lacking a very basic feature: Next. Next (like

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.