How to pass std::map as a default constructor parameter

我是研究僧i 提交于 2019-12-06 18:41:01

问题


I haven't been able to figure this out. It's easy to create two ctors but I wanted to learn if there's an easy way to do this.

How can one pass a std::map as the default parameter to a ctor, e.g.

Foo::Foo( int arg1, int arg2, const std::map<std::string, std::string> = VAL)

I've tried 0, null, and NULL as VAL, none of the work because they are all of type int, g++ complains. What is the correct default to use here?

Or is this kind of thing not a good idea?


回答1:


The correct expression for VAL is std::map<std::string, std::string>(). I think that looks long and ugly, so I'd probably add a public typedef member to the class:

class Foo {
public:
  typedef std::map<std::string, std::string> map_type;
  Foo( int arg1, int arg2, const map_type = map_type() );
  // ...
};

And by the way, did you mean for the last constructor argument to be a reference? const map_type& is probably better than just const map_type.




回答2:


You create a value-initialized temporary. For example:

Foo::Foo(int arg1,
         int arg2,
         const std::map<std::string, std::string>& the_map =
             std::map<std::string, std::string>())
{
}

(A typedef might help to make this more readable in your code)




回答3:


Since C++11 you can use aggregate initialization:

void foo(std::map<std::string, std::string> myMap = {});

Example:

#include <iostream>
#include <map>
#include <string>

void foo(std::map<std::string, std::string> myMap = {})
{
    for(auto it = std::begin(myMap); it != std::end(myMap); ++it)
        std::cout << it->first << " : " << it->second << '\n';
}

int main(int, char*[])
{
    const std::map<std::string, std::string> animalKids = {
        { "antelope", "calf" }, { "ant", "antling" },
        { "baboon", "infant" }, { "bear", "cub" },
        { "bee", "larva" }, { "cat", "kitten" }
    };

    foo();
    foo(animalKids);

    return 0;
}



回答4:


First, and tangentailly, you are passing the map by const value, which is pointless and probably not what you really want. You probably want to pass by const reference, so that you don't make a copy of the map, and you ensure that your function doesn't modify the map.

Now, if you want your default parameter to be an empty map, you do it by constructing it, like so:

Foo::Foo( int arg1, int arg2, const std::map<std::string, std::string>& = std::map<std::string, std::string>())


来源:https://stackoverflow.com/questions/4128776/how-to-pass-stdmap-as-a-default-constructor-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!