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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:24:20+00:00 2026-05-25T16:24:20+00:00

I have 2 classes and one of them has map<string, vector<string> > I want

  • 0

I have 2 classes and one of them has map<string, vector<string> >I want to be able to use this in other class. Here is my code:

class a
{
    map<string, vector<string> > m;
    public:
    const map<string, vector<string> > & get()
    {
        return m;
    }
};

class b
{
    a obj;
    public:
    void test()
    {
        map<string, vector<string> > m= obj.get();
        // and then print
    }
};

int main(int argc, char ** argv)
{
    b bobj;
    bobj.test();
    return 0;
}

Is the way I returned reference to map in class a correct? It worked,but I just want to confirm if it was done properly / I got lucky / any other comments about the code.

Thank you for your help.

  • 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-25T16:24:21+00:00Added an answer on May 25, 2026 at 4:24 pm

    If you do not want to change the map in b::test(), you should not make a copy of it:

    const map<string, vector<string> >& m = obj.get(); // note the const &
    

    My objections:

    1. Major: a::get() should be const:

      const map<string, vector<string> > & get() const // note the const at the end
      
    2. Minor: I would create an alias for the map’s type using typedef.

      typedef map<string, vector<string> > my_map_t;
      
    3. Minor: I can’t see at all what b is for.

    Given these, my code would look like this:

    class a
    {
        public:
          typedef map<string, vector<string> > my_map_t;
    
          const my_map_t & get() const {return m;}
    
        private:
          my_map_t m;
    };
    
    void test(const a& obj)
    {
        const a::my_map_t& m = obj.get();
        // and then print
    }
    
    int main()
    {
        a obj;
        test(obj);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, and want to include a static instance of one class
If I have two classes, and one of them has a function that I
I have 2 classes, and each one of them has a launcher for a
I have two classes one of which inherits from the other. Im trying to
First I want to describe my situation briefly. I have two classes, one MainClass
I have got two classes, one a subclass of the other (say Animal and
I have one abstract class and many child classes. In child classes are from
I have these classes that I use to create objects that I want to
i have a java class used as an entity that has 2 classes that
I have written two simple Java classes (one of them containing main(), and the

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.