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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:17:34+00:00 2026-06-15T17:17:34+00:00

Possible Duplicate: How do I remove code duplication between similar const and non-const member

  • 0

Possible Duplicate:
How do I remove code duplication between similar const and non-const member functions?

In the following example :

template<typename Type, unsigned int Size>
class MyClass
{
    public: inline Type& operator[](const unsigned int i) 
    {return _data[i];}

    public: inline const Type& operator[](const unsigned int i) const
    {return _data[i];}   

    protected: Type _data[Size];
};

the const and non-const operator[] are implemented independently.

In terms of design is it better to have :

  • 1) two independant implementations like here
  • 2) one of the two function calling the other one

If solution 2) is better, what would be the code of the given example ?

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

    You couldn’t have either implementation calling the other one without casting away constness, which is a bad idea.

    The const method can’t call the non-const one.

    The non-const method shouldn’t call the const one because it’d need to cast the return type.

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

Sidebar

Related Questions

Possible Duplicate: How do I remove code duplication between similar const and non-const member
Possible Duplicate: How to remove the underline for anchors(links)? In the following code below,
Possible Duplicate: MATLAB: How To Efficiently Remove NaN Elements from Matrix I got code
Possible Duplicate: Multiple select options in one row I have the following code but
Possible Duplicate: How to remove all characters between “<” and “>” including “<” and
Possible Duplicate: Display Zend_Form_Element_Radio on one line I hv the following code to generate
Possible Duplicate: Remove handlers on disposing object I have some code which dynamically adds
Possible Duplicate: PHP code to remove everything but numbers i have a loop that
Possible Duplicate: dynamically add/remove style in javascript It's required to write javascript functions to
Possible Duplicate: How do I remove the visibility of spaces between inline elements? I

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.