我试图理解这种行为,但似乎我没有.请看这段代码:
#include <iostream>
using namespace std;
class Base
{
public:
void operator=(const Base& rf)
{
cout << "base operator=" << endl;
this->y = rf.y;
}
int y;
Base() : y(100) { }
};
class Derived : public Base
{
public:
int x;
Derived() : x(100) { }
};
int main()
{
Derived test;
Derived test2;
test2.x = 0;
test2.y = 0;
test.operator=(test2); // operator auto-generated for derived class but...
cout << test.x << endl << test.y << endl;
cin.ignore();
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 fortran 2003 实现一个多项式类,其中包含重载的算术运算和赋值。派生类型维护术语定义和系数的可分配列表,如下所示
type polynomial
private
type(monomial),dimension(:),allocatable :: term
double precision,dimension(:),allocatable :: coef
integer :: nterms=0
contains
...
end type polynomial
interface assignment(=)
module procedure :: polynomial_assignment
end interface
...
contains
elemental subroutine polyn_assignment(lhs,rhs)
implicit none
type(polynomial),intent(???) :: lhs
type(polynomial),intent(in) :: rhs
...
Run Code Online (Sandbox Code Playgroud)
我必须使它成为基本的,因为它旨在用作多项式矩阵。这确实有效,至少在大多数情况下是这样。然而,我不知何故让自己担心这里的自我分配。可以简单地检查指针以查看 C++ 中的内容是否相同,但它似乎不是 Fortran 中的一个选项。但是编译器确实检测到自赋值并给了我一个警告。(gfortran 4.9.0)
当我有lhs 的意图(输出)时, lhs 和 rhs 的可分配条目似乎在进入子例程时被释放,这是有道理的,因为它们都是 p,并且意图(输出)参数将首先被最终确定。
然后我尝试通过意图(inout)避免解除分配,并通过修改 lhs 输出中的一个字段来检查自分配
elemental subroutine polyn_assignment(lhs,rhs)
implicit none
type(polynomial),intent(inout) :: lhs
type(polynomial),intent(in) :: rhs
lhs%nterms=rhs%nterms-5
if(lhs%nterms==rhs%nterms)then
lhs%nterms=rhs%nterms+5
return
end if
lhs%nterms=rhs%nterms …Run Code Online (Sandbox Code Playgroud) 我看过几个复制赋值运算符的示例,但不明白为什么我们需要删除复制赋值运算符内的指针。例如,如果我有以下课程
class MyClass
{
public:
MyClass(int a)
{
x = new int(a);
}
MyClass& operator=(const MyClass& pMyClass)
{
*x = *(pMyClass.x);
// ?????????
// delete x;
// x = new int(*(pMyClass.x));
}
~MyClass()
{
delete x;
}
private:
int* x;
}
Run Code Online (Sandbox Code Playgroud)
*x = *(pMyClass.x) 行有什么问题?我只是复制 pMyClass.x 指向的对象,为什么我需要删除并再次创建它?谁能举例说明这段代码何时会导致内存泄漏?
我目前正在编写一个复杂的类,在其中我基本上需要复制派生类的列表。简化版本如下:我有一个基类,从中派生出几个其他类:
class Base
{
public:
virtual void test(void)
{
cout << "Base" << endl;
}
Base(vector<Base*> *pointer)
{
pointer->push_back(this);
}
virtual Base& operator=(const Base& rhs)
{
cout << "Base=" << endl;
return *this;
}
};
class A : public Base
{
public:
void test(void)
{
cout << "A" << endl;
}
A(vector<Base*> *pointer) : Base(pointer) {}
A& operator=(const A& rhs)
{
cout << "A=" << endl;
return *this;
}
};
class B : public Base
{
public:
void test(void)
{ …Run Code Online (Sandbox Code Playgroud) c++ list operator-overloading derived-class assignment-operator
(注意原来的问题标题有“而不是右值”而不是“而不是常量引用”。下面的答案之一是对旧标题的回应。为了清楚起见,这是固定的)
C 和 C++ 中的一种常见构造是用于链式赋值,例如
int j, k;
j = k = 1;
Run Code Online (Sandbox Code Playgroud)
第二=首先进行,用该表达k=1具有副作用k被设置为1,而表达式本身的值是1。
但是,在 C++ 中合法(但在 C 中不合法)的一种构造如下,它对所有基本类型都有效:
int j, k=2;
(j=k) = 1;
Run Code Online (Sandbox Code Playgroud)
在这里,表达式j=k具有设置j为 2的副作用,并且表达式本身成为对 的引用j,然后将其设置j为 1。据我所知,这是因为表达式j=k返回非- const int&,例如一般来说是左值。
通常也建议将此约定用于用户定义的类型,如Meyers Effective C++中的“Item 10: Have assignment operators return a (non-const) reference to *this”中所述。本书的这一部分并没有试图解释为什么参考文献是非引用的const,甚至没有注意到const顺便提及的非引用性。
当然,这当然增加了功能,但(j=k) = 1;至少可以说这种说法似乎很尴尬。
如果约定改为使用内置赋值返回const引用,那么自定义类也将使用此约定,并且 C 中允许的原始链式构造仍然有效,无需任何无关的副本或移动。例如,以下正确运行:
#include …Run Code Online (Sandbox Code Playgroud) 我喜欢这个程序包,但是我想知道如何从tidyverse样式中更改一个规则:我想保留“ =”而不是“ <-”进行赋值。
我已阅读该说明:http : //styler.r-lib.org/articles/customizing_styler.html#implementation-details
但是我仍然不知道如何简单地更改该规则。我已经尝试过非常幼稚的:
library(styler)
force_assignment_op <- function (pd)
{
to_replace <- pd$token == "LEFT_ASSIGN"
pd$token[to_replace] <- "EQ_ASSIGN"
pd$text[to_replace] <- "="
pd
}
tidyverse_style()$token$force_assignment_op = force_assignment_op
Run Code Online (Sandbox Code Playgroud)
但是出现以下错误:
Error in tidyverse_style()$token$force_assignment_op =
force_assignment_op :
invalid (NULL) left side of assignment
Run Code Online (Sandbox Code Playgroud)
我想以某种方式修改它,之后便可以简单地运行styler插件。
正如我上面所说,这是不好的做法吗?在 ASM 中它会是什么样子?我的意思是,我不知道它是否被翻译成这样:
arr[0] = value;
arr[1] = value;
Run Code Online (Sandbox Code Playgroud)
或者像这样:
arr[1] = value;
arr[0] = arr[1];
Run Code Online (Sandbox Code Playgroud)
其中第二个显然效率更低(imm vs mem)。
提前致谢。
我是 C++ 新手,这是我在计算机科学领域的第一年……我只是想问……有没有办法让我在标题中的内容起作用?
为了进一步解释我的意思,这里有一个例子:
template <class dataType>
class squareMatrix{
private:
int size_side;
dataType *mainPtr;
public:
squareMatrix(int n){
this->size_side = n;
mainPtr = new dataType[n*n];
}
void index(int row, int column, dataType value){
mainPtr[row+(size_side*column)] = value;
}
};
Run Code Online (Sandbox Code Playgroud)
如您所见,我需要使用此方法来操作矩阵中的索引
squareMatrix<int> obj(2); // created a matrix of 2x2 size
obj.index(0,0,10); // here is the method to store the number 10 in the 0,0 index
Run Code Online (Sandbox Code Playgroud)
然后我的问题是,有没有办法把它变成这个?
obj.index(0,0) = 10;
Run Code Online (Sandbox Code Playgroud)
有没有办法使用“=”或赋值运算符来代替向方法添加一个额外的参数?
在这个 C++ 示例中,一个类C有一个默认构造函数、一个复制构造函数和一个赋值运算符:
struct C {
C();
C(const C& c);
C& operator=(const C& c);
};
Run Code Online (Sandbox Code Playgroud)
实现如下,带有一些用于跟踪对象的输出。我在注释中添加了一些示例地址作为对main下面程序的参考。
#include "C.h"
#include <iostream>
using namespace std;
C::C() {
cout << "Standard constructor." << endl;
}
C::C(const C& c) {
cout << "Copy constructor." << endl;
}
C& C::operator=(const C& c) {
cout << "Address of reference argument: &c = " << &c << endl; // F9B4
cout << "Address of this: &*this = " << &*this << endl; // …Run Code Online (Sandbox Code Playgroud) c++ class memory-address assignment-operator copy-assignment
我想知道C++中复合赋值的执行流程。我遇到了一个CodeChef 问题,其中我正在计算 NCR mod p 值并将它们加在一起以获得最终答案:
// correct
for(int i=min1; i<=max1; i+=2){
ans = (ans+ncr_mod_p(n,i))%mod;
}
Run Code Online (Sandbox Code Playgroud)
// incorrect
for(int i=min1; i<=max1; i+=2){
ans+=ncr_mod_p(n,i)%mod;
}
Run Code Online (Sandbox Code Playgroud)
这是由于整数溢出而发生的。
那么,复合赋值的执行顺序是怎样的呢?
比方说,如果我们有一个方程a+=b%c,那么执行顺序是什么:
a = (a+b)%c
// OR
a = a+(b)%c;
Run Code Online (Sandbox Code Playgroud)