标签: reference

参考字符串参数导致分段错误?

在Code :: Blocks上使用GCC编译器,我收到一个错误:

Segmentation fault (core dumped)

Process returned 139 (0x8B)
...
Run Code Online (Sandbox Code Playgroud)

输入输入后询问.这是我的测试程序:

#include <iostream>
#include <string>

using namespace std;

string getInput(string &input, string prompt)
{
    cout << prompt;
    getline(cin, input);
}

int main()
{
    string input;
    getInput(input, "What's your name?\n>");
    cout << "Hello " << input << "!" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么,参考参数使用不正确?

c++ parameters reference parameter-passing pass-by-reference

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

java中字符串引用的相等性

你能解释一下为什么我有"假"输出?如果我理解正确,引用指向同一个对象!

public class mainC {
    String str1,str2;
    public static void main(String [] args){
        mainC m=new mainC();
        m.str1="a";
        m.str2="b";
        System.out.print("m.str1 == m.str2: "+m.str1 == m.str2);
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

java string reference

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

如何在Perl中引用哈希?

如果我有这样的事情:

%hash = {foo => 'bar', foo1=>'bar1',};
Run Code Online (Sandbox Code Playgroud)

要么

%hash = (foo => 'bar', foo1=>'bar1',);
Run Code Online (Sandbox Code Playgroud)

要么

$hash = {foo => 'bar', foo1=>'bar1',};
Run Code Online (Sandbox Code Playgroud)

要么

$hash = (foo => 'bar', foo1=>'bar1',);
Run Code Online (Sandbox Code Playgroud)

上述代码有何不同?以及如何访问他们的组件?

perl hash reference

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

C++:如何将对象数组传递给函数?

我有一个指向对象数组的指针.看起来像:

MyClass *myClass[ 10 ];
myClass[ 0 ] = new MyClass(); // init for each of myClass[0..9]
myClass[ 0 ]->field1 = "hello";
Run Code Online (Sandbox Code Playgroud)

如何通过引用将"myClass"传递给函数?我尝试了几个案例,但它没有用.

c++ pointers reference

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

C++ - 参考,参数中的指针

关于"我什么时候使用引用和什么时候指针?"有很多问题.他们让我有点困惑.我认为引用不会占用任何内存,因为它只是地址.

现在我做了一个简单的Date课程,向他们展示了代码审查社区.他们告诉我不要在以下示例中使用该引用.但为什么?
有人告诉我,它将分配指针所分配的相同内存.这与我学到的相反.

class A{
int a;
public:
   void setA(const int& b) { a = b; } /* Bad! - But why?*/
};

class B{
int b;
public:
   void setB(int c) { b = c; } /* They told me to do this */
};
Run Code Online (Sandbox Code Playgroud)

那么我什么时候在参数中使用引用或指针,何时只是一个简单的副本?在我的例子中没有引用,常量是不必要的吗?

c++ pointers arguments reference

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

将带索引的字符串转换为对象引用

我正在解析R公式的左侧.在我的特定情况下,这可以是带索引的变量或对象(类似myvariable[[3]]).我想访问该对象的第三个子对象并将其存储在另一个对象中.以下示例从我拥有索引对象的字符串开始,但我需要引用.

mychars <- c("a", "b", "c")
mystring <- "mychars[2]"
get(mystring)                # does not work
eval(as.name(mystring))      # does not work either
Run Code Online (Sandbox Code Playgroud)

我当然可以使用正则表达式解析数字并使用as.numeric它将其转换为真实索引.但在某些情况下,可能会有名称索引,如mystring["second"].那我怎样才能提取子对象呢?

r reference object formula

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

C++指针和引用

在我的C++类中,我需要分配一个带指针和/或引用的方法.所以我做了这个棘手的事情:

(假设aclass是一个类变量AnotherClass*)

void MyClass::setElem(AnotherClass *pVal)
{
  aclass = pVal;
}

void MyClass::setElem(AnotherClass &refVal)
{
  aClass = &article;
}
Run Code Online (Sandbox Code Playgroud)

但在我看来,听起来并不那么"优雅"......

更好的方法来实现这一目标

c++ pointers reference class

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

.equals()和==之间有什么区别?

我读到.equals()比较对象的值,而==比较引用(即 - 变量指向的内存位置).请参见:Java中== vs equals()之间的区别是什么?

但请注意以下代码:

package main;

public class Playground {

    public static void main(String[] args) {
        Vertex v1 = new Vertex(1);
        Vertex v2 = new Vertex(1);

        if(v1==v2){
            System.out.println("1");
        }
        if(v1.equals(v2)){
            System.out.println("2");
        }
    }
}

class Vertex{
    public int id;

    public Vertex(int id){
        this.id = id;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:(
没什么)

不应该打印2吗?

java equality reference

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

为什么x和&x对于数组是相同的

让我们看一下下面的例子

int x[10];
cout<<x<<endl;
cout<<&x<<endl; //both couts are same.


int x;
cout<<x<<endl;
cout<<&x<<endl; //last two couts are different.
Run Code Online (Sandbox Code Playgroud)

这可能是什么原因?

c++ reference

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

如何>> streams上的operator返回对自身的引用

说我上课了

class A{
A& operator+ (size_t ofst)
{
     //some calculation
     //return
}
};
Run Code Online (Sandbox Code Playgroud)

在这里,我不能写

return this;
Run Code Online (Sandbox Code Playgroud)

因为A*无法转换为A&.那么如何实现这个呢?我想返回引用而不是指针.


作为类比,有一个流类的>>或<<运算符.据我所知,这两个返回对自身的引用.标准库如何实现这一目标?

c++ return reference

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