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

The Archive Base Latest Questions

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

If I have two classes A and B defined as: class A { map<int,

  • 0

If I have two classes A and B defined as:

class A {
    map<int, int> mMap;
    vector<int> mVec;

    void method() {
      // do something with mMap and mVec 
    }
}

class B {
    map<int, double> mMap; // just an example of a map with a different signature

    ...
}

It is possible somehow to use everything A has in B but with the new map defined in B and without rewriting method ?

This is a simplified example of what I am trying to do: refactoring a big class with a new map.

  • 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-16T03:03:38+00:00Added an answer on June 16, 2026 at 3:03 am

    If you want to use

    std::map<int, Y>
    

    instead of

    std::map<int, X>
    

    with the same code, to replace class A with class B, then there probably exists a strong correlation between the types X and Y.

    In this case, if you cannot use templates on class A, a possible workaround may be to define implicit type conversion operators for both Y and X to X and Y respectively.

    class X {
        operator Y() {
            Y y;
            // Conversion logic
            return y;
        }
    };
    

    By doing so, you don’t need to change the code of method as the insertion or access operations on the map will automatically call the implicit type conversion operators.

    Warning note: the abuse of implicit conversion operators weakens the type safety checkings of the compiler that you may expect in other parts of the code (e.g.: wrong Y parameter passed to f(X &x)).

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

Sidebar

Related Questions

I have two classes defined as such: public class Questionnaire { public int QuestionnaireID
I have a method and two classes defined like this: template<template<class X> class T>
I have two classes, user and role, defined as: public class User : Entity
I have two classes set up as follows: class Point { protected: double coords[3];
I have two classes defined in different h files and each class has some
Suppose I have two classes defined the following way: public class A { public
I have two classes defined as follows. First one: internal class Content { internal
I have two classes, named Post and Question. Question is defined as: public class
I have defined two classes below: public class junk { private BigInteger a =
I have two Model classes: Attendance and Employee. I have defined the Employee class

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.