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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:29:15+00:00 2026-05-27T10:29:15+00:00

Here’s an overview of my problem: I have a templated class called TWMatrix. I’ve

  • 0

Here’s an overview of my problem:
I have a templated class called TWMatrix. I’ve overloaded the () operator such that it can take in a pair of TWMatrix and extract the corresponding entries. I want the “extraction” to produce 2 distinct results for 2 distinct cases. This has worked before when taking a pair of ints, but I’m a very stuck getting it to work for this slightly different problem:

  • In one case, I want the operator to copy the entries into another TWMatrix and output it.
  • In another case, I want the operator to copy the addresses of these entries into an object of type TWDataPointer, output that object.

In the first case, the method does not break constness. Indeed, the entries themselves are neither modified nor vulnerable to future modifications. In the latter, the whole point is that the TWDataPointer object can be used to modify those entries of the matrix. And so the corresponding bit of code is:

TWDataPointer<T> operator () (const TWMatrix<int> & I1, const TWMatrix<int> & I2)
{
     return TWDataPointer<T>(*this,I1,I2);
}

TWMatrix<T> operator () (const TWMatrix<int> & I1, const TWMatrix<int> & I2) const 
{
    return SubMat(I1,I2);
}

SubMat is a method that creates a matrix with the corresponding entries. It works just fine, and what values it fetches isn’t very relevant here. The issue is that this second operator seems to never get called. So for example if, in my main, I write:

TWMatrix<double> test = D5(I1,I2);

The compiler complains saying:

error: conversion from ‘TWDataPointer<double>’ to non-scalar type ‘TWMatrix<double>’ requested

This is very unlike:

T operator () (const int & i, const int & j) const 
{
    return data[j+i*nc];
}

T& operator () (const int & i, const int & j) 
{
    return data[j+i*nc];
}

Which works exactly as expected, and returns either a T or T& depending on the situation. From what I understand of overloading operators, it’s the const that lets the compiler distinguish which to use in which situation. So why won’t this work?

Thanks in advance for the help, and feel free to ask for any extra pieces of code you may need.

PS: I already have a workaround in mind for this, and though it’s not immensely ugly, it’s nowhere near as simple, elegant and robust as this would be if I could get it to work.

edit: Thanks for the help so far, and I’m still looking forward to hearing your answers. In particular, I’m still confused about this:

“Going back to the int example, why does a = test(0,0) use the const version of the corresponding operator even if test is not declared const? Indeed, I’ve checked that this is the case using cout statements in both versions of the () operators.”

  • 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-27T10:29:15+00:00Added an answer on May 27, 2026 at 10:29 am

    Is there a specific reason that you need to have only a single const of the second method? From what it appears to me in this code, and the possible use-scenarios, while your second method is technically a const method, it doesn’t have to be declared as-such. In other word it’s a method that can be used by both const and non-const versions of your class instance, and you’re obviously coming up with scenarios where your class instance requires the method when it’s not a const object. Thus the use of labeling your method as a const method just because it doesn’t change the state of the instance that calls the method doesn’t mean that you must label your method as const.

    In order to work with both scenarios, I would generate two versions of operator() that return the TWMatrix<T> type … one that is declared const, and one that is declared as a non-const method so that it can be utilized by both const and non-const class instances. The downside though is you would have to create a separate method for returning the TWDataPointer<T> type since you can’t overload based on just the return-type.

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

Sidebar

Related Questions

Here's the problem....I have three components...A Page that contains a User Control and a
Here's the situation, i want to have a user that can enter time on
Here is my problem...I have a page that loads a list of clients and
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see
Here's my problem I have this javascript if (exchRate != ) { function roundthecon()
Here is another spoj problem that asks how to find the number of distinct
Here's the scenario: I have a local git repository that mirrors the contents of

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.