代码之家  ›  专栏  ›  技术社区  ›  user855

何时通过引用,何时在C++中传递指针?

c++
  •  44
  • user855  · 技术社区  · 14 年前

    常见情况:

    1. 将std::string传递给函数foo(std::string*)或foo(std::string&);
    2. 将tr1::shared \u ptr传递给函数foo(tr1::shared \u ptr*ptr)或foo(tr1::shared \u ptr&ptr);

    一般来说,什么是好的做法。我总是感到困惑。首先,将所有内容作为引用传递似乎是一致的,但是不可能将文本作为引用传递或将空值作为引用传递。

    类似地,将所有内容都作为指针似乎很好,但是如果这样,我就不得不担心指针可能指向NULL,并在函数的开头检查这些条件。

    你认为下面的片段好吗?

    #include <iostream>
    #include <vector>
    #include <map>
    #include <string>
    #include <tr1/memory>
    #include <algorithm>
    using namespace std;
    using namespace std::tr1;
    
    int main(){
            map<string, shared_ptr<vector<string> > > adjacencyMap;
            vector<string>* myFriends = new vector<string>();
            myFriends->push_back(string("a"));
            myFriends->push_back(string("v"));
            myFriends->push_back(string("g"));
            adjacencyMap["s"] = shared_ptr<vector<string> >(myFriends);
            return 0;
    }
    

    谢谢

    8 回复  |  直到 14 年前
        1
  •  27
  •   David Thornley    14 年前

    您不使用常量引用的文本有问题吗?不能将临时引用(由文本生成)绑定到非常量引用,因为更改临时引用毫无意义。可以将一个绑定到常量引用。

    指针很有用,因为它们有一个可以测试的保证无效值。有时这是无关紧要的,有时这是非常重要的。当然,通常不能通过指针传递文本,除非(在字符串文本的情况下)它已经是了。

    一些编码标准规定,不应该通过非常量引用传递任何内容,因为它在调用点不提供参数可能被函数更改的指示。在这种情况下,需要通过指针传递。我不赞成这样做,尤其是编程工具使获取函数签名变得越来越容易,因此您可以看到函数是否可能更改参数。然而,在团队或企业中工作时,风格一致性比任何个人风格元素都更重要。

        2
  •  27
  •   Lucas    14 年前

    一个很好的经验法则:“当你可以的时候使用引用,当你必须的时候使用指针”。

        3
  •  6
  •   user180326 user180326    14 年前

    • 路过 const
    • 如果需要读写访问,则通过指针传递

        4
  •  5
  •   Matthieu M.    14 年前

    我真的不明白你为什么要这么麻烦:

    std::map < std::string, std::vector<std::string> > adjacencyMap;
    std::vector<std::string>& sFriends = adjacencyMap["s"];
    sFriends.push_back("a");
    sFriends.push_back("v");
    sFriends.push_back("g");
    

    shared_ptr 在这里?情况当然不需要这样!

        5
  •  3
  •   ronag    14 年前

    可能不是问题的答案。只是亲吻。

    int main()
    {
            multimap<string, string> adjacencyMap;
            adjacencyMap.insert(std::make_pair("s", "a"));
            adjacencyMap.insert(std::make_pair("s", "v"));
            adjacencyMap.insert(std::make_pair("s", "g"));
            return 0;
    }
    
        6
  •  2
  •   Sebastian    14 年前

    你可以浏览 http://www.cplusplus.com/forum/beginner/3958/ 一些见解。 也很有用: http://www.velocityreviews.com/forums/t284603-pointers-vs-references-a-question-on-style.html

    我个人比较喜欢推荐信,但我还是建议你去读那些帖子,好好想想。

        7
  •  2
  •   Zoli    14 年前

    一般来说,总是尝试通过引用const来传递参数。传递指针可能会导致所有权问题以及一系列其他可能的细微错误。

    空的目的是什么?指示无效的指针/对象。如果要将无效对象传递给函数,那么所要做的就是有一个检查对象有效性的方法。例如:

    void myfunc(const obj& myobj)
    {
      if(myobj.valid())
        // DO SOMETHING
    }
    

    std::string const char* 尽可能多的使用C样式的字符串。当然,如果您必须使用C字符串,那么您别无选择,只能使用指针,但是所有的引用都应该是最好的选择。

    哦,为了真正的例外安全,尽量避免这种情况:

    vector<string>* myFriends = new vector<string>();
    ...
    adjacencyMap["s"] = shared_ptr<vector<string> >(myFriends);
    

    相反,请执行以下操作:

    shared_ptr<vector<string> > myFriends(new vector<string>());
    

    查看RAII和异常安全,了解为什么这是首选方法。

        8
  •  1
  •   Steve Townsend    14 年前

    我更喜欢

        map<string, shared_ptr<vector<string> > > adjacencyMap;
        shared_ptr<vector<string> > myFriends(new vector<string>());
        myFriends->push_back(string("a"));
        myFriends->push_back(string("v"));
        myFriends->push_back(string("g"));
        adjacencyMap["s"] = myFriends;
        return 0;
    

    我真的不明白这是怎么解决你的问题,这是关于优点的参考与ptr,虽然。在你引用的两个例子中,我希望使用第二个(ref)形式。