标签: reference

重载C++虚函数的分辨率 - 引用与指针

我对C++重载分辨率的行为感到困惑.我有两个班,A和B,A <:B.A具有虚函数f,B应该覆盖该函数.

但是,当我使用引用调用虚函数时,虚函数似乎不像虚函数那样工作.起初我认为这是由于对象被分配在堆栈而不是堆上,但现在我发现即使我使用对堆上分配的对象的引用也会发生这种奇怪的行为.

对此有何解释?

#include<iostream>

using namespace std;

class A{
public:
    virtual void foo(){ cout << "A::foo" << endl; }
};

class B : public A{
public:
    virtual void foo(){ cout << "B::foo" << endl; }
};

void test_pt(A* pt){
    cout << "test_pt " << (int)pt << " ";
    pt->foo();
}

void test_ref(A ref){
    cout << "test_ref " << (int)&ref << " ";
    ref.foo();
}

int main(int argc, char* argv[]){
    // pointers to objects allocated on heap
    A* heap_pt_a = …
Run Code Online (Sandbox Code Playgroud)

c++ virtual-functions overloading reference overload-resolution

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

为什么std :: forward()不推断类型?

这是下面的代码.为什么我typename remove_reference<S>::type&S&它替换它不会很好用?(我的意思是一种类型会被推断错误)

如果我传递一个rvalue(let int是int),那么S将推断为int,a's type is int&,forward()returns int&&(rvalue).

如果我传递一个lvalue(int)S将被推断为int&,as的类型是int& & ->int&,forward()返回int& &&->int&.一切顺利,为什么我们需要remove_reference呢?

template<class S>
S&& forward(typename remove_reference<S>::type& a) noexcept
{
  return static_cast<S&&>(a);
} 
Run Code Online (Sandbox Code Playgroud)

c++ reference rvalue c++11

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

C++对简单类型(Vector3,Matrix等)的rvalue引用构造函数的好处

如果传递的对象没有复杂数据,使用右值引用是否有任何优势?例如,以下是否比使用引用有任何优势?

class Vector3
{
public:
    float X, Y, Z;

    Vector3(const Vector2& vector2, float z)
        : X(vector2.X), Y(vector2.Y), Z(z)
    {

    }

    // Is this any better than the above?
    Vector3(Vector2&& vector2, float z)
        : X(std::move(vector2.X), Y(std::move(vector2.Y), Z(z)
    {

    }
};
Run Code Online (Sandbox Code Playgroud)

c++ reference rvalue

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

对于级联成员函数调用,为什么我需要返回引用?为什么不只是这个指针足够了?

#include <iostream>

using namespace std;

class armon {
    int a;
    int b;

public:
    armon(int newA, int newB) : a(newA), b(newB) {}

    armon setA(int newA) {
        a = newA;
        return *this;
    }

    armon setB(int newB) {
        b = newB;
        return *this;
    }

    void print(void) { cout << a << endl << b; }
};

int main() {
    armon s(3, 5);

    s.setA(8).setB(9);

    s.print();
}
Run Code Online (Sandbox Code Playgroud)
  1. 为什么我不能只用这个指针返回对象来进行级联函数调用?
  2. 为什么我需要返回对象的引用?
  3. 甚至会做什么?

c++ reference function cascading this-pointer

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

C++距离函数保持返回-1

我创建了一个计算两点之间距离的程序,并找到了斜率.

1)我如何将程序更改为严格指针?

2)无论输入什么,距离函数都返回"-1".我100%肯定我的算法是正确的,但似乎出现了问题.

 // This program computes the distance between two points
 // and the slope of the line passing through these two points.

    #include<iostream>
    #include<iomanip>
    #include<cmath>
    using namespace std;

    struct Point
    {
        double x;
        double y;
    };

    double distance(Point &p1, Point &p2);
    double slope(Point &p1, Point &p2);

    int main()
    {
        Point p1, p2;
        char flag = 'y';
        cout << fixed << setprecision(2);
        while(flag == 'y' || flag == 'Y')
        {
            cout << "First x value: "; cin >> p1.x; …
Run Code Online (Sandbox Code Playgroud)

c++ pointers reference distance

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

这个和.this的区别?

是什么区别this.this调用函数时?而且,当使用thisthis.使用时会发生什么?

例:

class reference
{
   public void object()
   {
      reference obj = new reference();
      this.obj();
   }
}
Run Code Online (Sandbox Code Playgroud)

java methods reference function

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

为什么接下来的两个代码序列表现不同?

我不明白为什么第一个代码序列创建一个元素重叠的QWidget,而第二个代码序列行为正确.唯一的区别是,在第一个中有一个QVBoxLayout指针,而在第二个中它是一个对象.它是关于通过引用传递vs通过指针传递?我真的没有得到微妙的区别.

第一:

QVBoxLayout vbox;
vbox.setSpacing(2);

QPushButton* quitButton = new QPushButton("Qsdfsuit");
QFont fnt = quitButton->font();
fnt.setPointSize(18);
fnt.setBold(true);
fnt.setFamily("Arial");
quitButton->setFont(fnt);

connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

QLCDNumber* lcd = new QLCDNumber(2);

QSlider* slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 99);
slider->setValue(0);

connect(slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));

vbox.addWidget(quitButton);
vbox.addWidget(lcd);
vbox.addWidget(slider);

this->setLayout(&vbox);
Run Code Online (Sandbox Code Playgroud)

第二个:

QVBoxLayout* vbox = new QVBoxLayout();
vbox->setSpacing(2);

QPushButton* quitButton = new QPushButton("Qsdfsuit");
QFont fnt = quitButton->font();
fnt.setPointSize(18);
fnt.setBold(true);
fnt.setFamily("Arial");
quitButton->setFont(fnt);

connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

QLCDNumber* lcd = new QLCDNumber(2);

QSlider* slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 99);
slider->setValue(0); …
Run Code Online (Sandbox Code Playgroud)

layout qt pointers reference overlapping

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

使用数组和函数时如何避免"未定义引用..."?

我正在学习如何通过制作执行以下操作的程序将数组用作函数参数:

  1. 从用户获取四个测试分数并将其存储在数组中
  2. 计算数组中得分的总和
  3. 从总和中减去最低的测试分数,并给出调整后的总和
  4. 计算调整后的总和的平均值,并打印出平均值

我在一本名为"用c ++开始"的书中找到了这个程序.我似乎已经按照它提供的源代码来解决这个问题,但是发生了错误,我不明白问题出在哪里.

这是我不完整的源代码:(我正在使用代码块)

错误出现在第20行:

对'GetTotal(double const*,int)'的未定义引用

#include <iostream>
#include <iomanip>
using namespace std;

void GetScores(double[], int);
double GetTotal(const double[], int);
double GetLowest(const double[], int);

int main()
{
    const int SIZE = 4;
    double scOres[SIZE],//array holds scores
        total, // to store total test scores
        lowestScore, // to store lowest test score
        average;// to store average value

    GetScores(scOres , SIZE); //gets test scores from user
    GetTotal(scOres , SIZE); //sums up test scores
    GetLowest(scOres , SIZE); //gets lowest …
Run Code Online (Sandbox Code Playgroud)

c++ arrays reference

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

为什么Ruby类允许通过方法返回来访问实例变量?

我有一个同事的Ruby代码:

class Leaky

   def initialize
      @internalOnly = [[1, 2, 3], [3, 4, 5]]
   end

   def foo
      if 5 > 4
         temp = @internalOnly
      end
   end

   def printInternal
      puts " here is my @internalOnly: #{@internalOnly}"
   end

end

a = Leaky.new
a.printInternal

b = a.foo  # 1) Gets Leaky:@internal!
b[1] = 666 # 2) Modifies it!

a.printInternal
Run Code Online (Sandbox Code Playgroud)

它产生这个输出:

 here is my @internalOnly: [[1, 2, 3], [3, 4, 5]]
 here is my @internalOnly: [[1, 2, 3], 666]
Run Code Online (Sandbox Code Playgroud)

在语句中# 1),Ruby显然返回了对Leaky实例变量的引用 …

ruby encapsulation reference instance-variables

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

右值不起作用参考

我正在研究引用,我正在尝试一个程序将rvalue传递给函数作为引用参数,就像这样.

#include<iostream>
using namespace std;

int fun(int &x)
{
    return x;
}
int main()
{
    cout << fun(10);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用,当我试图通过左值时,它起作用了.

#include<iostream>
using namespace std;

int fun(int &x)
{
    return x;
}
int main()
{
    int x=10;
    cout << fun(x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

谁能解释我为什么会这样?

c++ reference rvalue lvalue

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