标签: reference

具有常量引用的函数作为输入

我正在尝试创建一个函数,该函数将字符串的常量引用作为输入,并在字符串的每个字符向右旋转1个位置后返回字符串.使用引用和指针仍然让我困惑,我不知道如何从常量引用中获取字符串.

string rotate(const string &str){
     string *uno =  &str;
     string dos = rotate(uno.rbegin(), uno.rbegin() + 1, uno.rend());
     return dos;}
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所得到的,但它没有编译.任何有关如何从常量引用中正确获取字符串的提示将不胜感激.

c++ string reference

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

返回静态成员变量c ++的引用

返回静态成员变量的引用是否有任何问题?(参考避免复制成本)

Vector接受4个参数(x,y,z,w);

.H

class MyClass
{
private:
   static const Vector POS;
}
Run Code Online (Sandbox Code Playgroud)

的.cpp

const Vector MyClass::POS(100,300,0,0);

const Vector& MyClass::GetVector()
{
 return POS;
}
Run Code Online (Sandbox Code Playgroud)

c++ static reference

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

链链接类

我已经创建了长链类,每个链接(类)只知道下一个和上一个链接.当索引不重要时,我认为它优于Arrays.

public class ChainLink
{
    public ChainLink previousLink, nextLink;

    // Accessors here
}
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 这种技术到底叫什么?(我不知道要搜索什么)
  2. 有一个.Net类做同样的事情吗?
  3. 对阵列或列表是否有明显的性能影响?

我使用的访问器示例

分配给链:

    public ChainLink NextLink {
        get{ return _nextLink;}
        set {
            _nextLink = value;
            if (value != null)
                value._previousLink = this;
        }
    }

    public void InsertNext (ChainLink link)
    {
        link.NextLink = _nextLink;
        link.PreviousLink = this;
    }
Run Code Online (Sandbox Code Playgroud)

缩短链条:

如果我取消分配链的下一个链接,让剩余的链接不被主程序引用,垃圾收集器将为我处理数据.

测试循环引用:

    public bool IsCircular ()
    {
        ChainLink link = this;
        while (link != null) {
            link = link._nextLink;
            if (link == this)
                return true;
        } …
Run Code Online (Sandbox Code Playgroud)

c# arrays reference

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

(晃来晃去?)从函数返回的引用不"工作"

我按照V. Romeo的实体管理教程(在GitHubYoutube上).

然后我尝试重写类CEntity,CComponent和测试CPosition(主要来自Romeo视频/代码的内存).我遇到的问题是,在我的主要我在堆栈上创建一个CEntity并添加一个组件.当我通过i添加组件时,addComponent()获取对新创建的组件的引用,返回addComponent().

当我现在想要通过返回的引用修改组件时,我所做的更改不会反映回实体(组件).看起来像是对我的悬空参考,但我无法找到我犯的错误.

谁能指出我在这里做错了什么?

掌握我的CEntity课程:

#include <array>
#include <bitset>
#include <memory>
#include <cassert>
#include <stdexcept>

namespace inc
{

using ComponentID = unsigned int;

ComponentID getNewID()
{
    static ComponentID id = 0;
    return id++;
}

template <typename T>
ComponentID getComponentID()
{
    static ComponentID component_id = getNewID();
    return component_id;
}

// Forward declarations used by CEntity:
struct CComponent;

class CEntity
{
public: …
Run Code Online (Sandbox Code Playgroud)

c++ pointers reference unique-ptr

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

返回对类的引用

我有这样的代码:

#include <stdio.h>

class AbstractIterator{
    virtual void do_something() = 0;
};

class AbstractList{};

class Iterator : public AbstractIterator{
public:
   Iterator(const AbstractList & list) : list(list){};

   virtual void do_something() override{
       printf("hello\n");
   };

   const AbstractList & list;
};

class List : public AbstractList{
public:
  Iterator getIterator(){
     return Iterator(*this);
  }
};

int main(int argc, char** argv){
    List list;
    Iterator it = list.getIterator();

    it.do_something();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这工作,但我想"推"getIterator()方法到AbstractList类.为此,需要能够执行以下操作:

/* non const */
AbstractIterator &it = list.getIterator();
it.do_something();
Run Code Online (Sandbox Code Playgroud)

没有动态分配,这可能以某种方式完成吗?

c++ return reference c++11

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

在perl中的子例程之后,哈希键值更改为数组引用

我在同一个哈希上创建子程序外部和内部的键.但是,在子例程之后,调用子例程之前创建的键中的值现在被解释为数组引用.

#!/usr/bin/perl
use module;
use strict;
use warnings;
my %hash;
my $count = 0;

my @array = ("a", "b", "c", "d");

for my $letter (@array) {
    $hash{$letter} = $count;
    $count++;
}

# need "\" to pass in hash otherwise changes
# will get lost outside of subroutine
foreach my $x (sort keys %hash) {
    print "first $hash{$x}\n";
}

module::add_ten(\%hash);

foreach my $p (sort keys %hash) {
    # $hash{$p} is printing array references, but before it was
    # printing the value …
Run Code Online (Sandbox Code Playgroud)

perl hash reference subroutine

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

在C ++中传递类对象

我正在上一门C ++课程,并且被困在类和对象上。简而言之,我正在做一个分配,创建一个带有两个变量的类(假设长度和宽度)。

我已经弄清楚了如何使用get和set函数来做到这一点。但是,然后,我们必须对这些变量使用数学运算。我们应该编写一个函数,该函数将类的一个实例作为参数(因此有两个变量),然后对该对象(作为参数的一个)和调用方法的对象进行数学运算。

我感到困惑的部分原因是语言,我不确定这到底意味着什么。到目前为止,就像我说的那样,我设法能够通过用户输入设置setLength和setWidth变量。我真的非常想尝试将这些值(或该对象?)传递给函数,然后调用另一个对象的方法?

也许有人可以帮助我弄清楚“将一个对象作为参数然后对我称之为方法的对象进行数学运算”是什么意思?或者只是帮助传递物体?

c++ reference object parameter-passing c++11

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

C#对象范围

我试图了解何时回收物品.例如,在类中,我有一个List声明和此类中的方法,通过声明和初始化临时对象然后将此对象添加到列表来填充列表.

我的困惑:由于临时对象是在方法体内声明的,当方法返回时,这些对象不会被回收,因此持有对它们的引用的列表现在会丢失它们的对象的值吗?方法完成后,我的代码仍保留对象值(并且可能是完整引用).

public class CameraTest
{

    private List <Camera> cameraList;
    public CameraTest()
    {
        AddCamera();
    }

    private void AddCamera()
    {
        Camera tempCamera = new Camera();
        tempCamera.Name="Camera1";
        cameraList.Add(tempCamera);
    }

   //Why would cameraList still have the "Camera1" object here?

}
Run Code Online (Sandbox Code Playgroud)

c# reference object recycle

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

通过引用传递const值

假设我有这个程序:

const int width = 4;

void test(int&){}

int main() {
    test(width);
}
Run Code Online (Sandbox Code Playgroud)

这将无法编译.我注意到名称(例如宽度)的常量值(也是枚举常量)不能通过引用传递.为什么会这样?

c++ pointers reference constants

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

理解返回字符串引用的函数中的"const"

我有这个代码:

#include <cstdio>
#include <string>

using namespace std;

const string & func()
{
    static string staticString = "one";
    printf("%s\n", staticString.c_str());
    return staticString;
}

int main( int argc, char ** argv )
{
    string firstString = func();
    firstString = "two";
    printf("%s\n", firstString.c_str());
    func();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是

one
two
one
Run Code Online (Sandbox Code Playgroud)

困惑我的部分是,如果我constfunc()输出中删除是完全相同的.我期望它是:

one
two
two
Run Code Online (Sandbox Code Playgroud)

如果我从中获取字符串引用func()(当它没有const关键字时),为什么在我func()再次调用时它被重置?

c++ reference constants

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