相关疑难解决方法(0)

何时通过引用传递以及何时通过C++中的指针传递?

常见情况:

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

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

类似地,将所有内容作为指针似乎都很好,但是我必须担心指针可能指向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;
}
Run Code Online (Sandbox Code Playgroud)

谢谢Ajay

c++

46
推荐指数
4
解决办法
2万
查看次数

C++指针参数在'ret'指令之前设置为NULL

我有一个奇怪的问题.我以为我知道一定数量的C++,但我必须忽略一些细微差别?

我有一些模板化的类,基本上所有发生的事情都是基于某些条件,一个对象正从一个std :: list移动​​到另一个.

这里关注的方法是doAndGet(T*t).我的目的是让T对来作为NULL(或什么的,我真的不在乎,但它应该是NULLnullptr,如果我结束了移动T元素*t,从myListmyOtherList,我想T *t以指向已移动的项目.

现在也许有一些std::list我不知道的事情,但是当我踩过我的代码时,t得到了正确的分配 - 原来我想在返回的方法之后,但后来发现 - 就在ret指令之前,参数T *t被设置回NULL

任何一位大师都可以对这个问题有所了解吗?对我来说这真是令人头疼的事......

template<typename T, typename C>
class Base : IsDerivedFrom<T, C>
{
public:
    typedef std::list<T*> SpecialList;
    virtual bool doAndGet( T* t ) = 0;

protected:

    SpecialList myList;
};

template<typename T>
class Derived : public Base<T, Other>, public Other
{
public:
    Derived( void );
    virtual …
Run Code Online (Sandbox Code Playgroud)

c++ templates pointers arguments

4
推荐指数
1
解决办法
1259
查看次数

在函数C ++中删除对象

我有一个简单的问题。

考虑下面的代码:

#include <iostream>

struct Node {
    int data;
    Node *left;
    Node *right;
    Node(int pData) : data(pData), left(nullptr), right(nullptr) {}
};

void delete_node(Node *node) {
    delete node;
    node = nullptr;
}

int main() {
    Node *node1 = new Node(1);
    Node *node2 = new Node(2);
    delete_node(node1);
    delete node2;
    node2 = nullptr;
    if (node1) std::cout << "node1: " << node1->data << std::endl;
    if (node2) std::cout << "node2: " << node2->data << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

产生输出:

node1: -572662307
Run Code Online (Sandbox Code Playgroud)

自从我设置node1 = nullptr …

c++

3
推荐指数
1
解决办法
64
查看次数

指针通过后结果的变化

我对以下两种情况感到困惑。这两个函数都是通过指针传递的。一个导致主电源的改变,而另一个则没有。我认为在调用函数后,通过指针传递确实不起作用,因为它会在函数中产生指针的本地副本。任何提示表示赞赏

#include <vector>
#include <iostream>
using namespace std;
//
class A
{
        public:
                int b;
                A() {;}
};

//
void  test1(A *a)
{
    A t;
    t.b = 200;
    a = &t;
}

//
void  test2(A *a)
{
    a->b = 200;
}

//
int main()
{
    A a;
    a.b = 10;

    test1(&a);
    cout<<"a.b value is NOT changed"<<endl;
    cout<<a.b<<endl;

    test2(&a);
    cout<<"a.b value is changed"<<endl;
    cout<<a.b<<endl;
}


//.. the output is: 
//a.b value is NOT changed
//10
//a.b value is changed
//200
Run Code Online (Sandbox Code Playgroud)

c++ pointers

2
推荐指数
1
解决办法
98
查看次数

C++:作为函数参数的指针真的被复制了吗?

根据答案/sf/answers/828970971/,如果你这样编码,函数参数Bubble * targetBubble将被复制到函数内部。

bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
    targetBubble = bubbles[i];
}
Run Code Online (Sandbox Code Playgroud)

然而,我做了一个测试,发现作为函数参数的指针将与外部的相同,直到我更改它的值:

// c++ test ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "c++ test ConsoleApplication2.h"
using namespace std;
#include<iostream>




int main()
{
    int a= 1;
    int* pointerOfA = &a;

    cout << "address of pointer is" << pointerOfA << endl;
    cout << *pointerOfA << endl;
    func(pointerOfA);
    cout << *pointerOfA << endl;





}


void …
Run Code Online (Sandbox Code Playgroud)

c++ pointers

0
推荐指数
1
解决办法
2792
查看次数

标签 统计

c++ ×5

pointers ×3

arguments ×1

templates ×1