我有一个关于嵌套模板和赋值运算符重写的问题.假设我想要一个引用计数类模板_reference.这个_reference现在只是保存一个指向ref-counting对象的指针.现在的问题是,只要我使用简单的类或结构进行此操作,这一切都可以正常工作.例如._reference ...,
但是现在我想创建一个类模板,它是对std-vector的引用,它转发了它所拥有的类.
不,我只是发布代码:(它现在没有做引用和那些东西,它只是提取我遇到的问题)
template <typename T>
class _reference
{
private:
T* p_;
public:
// !!! this assignment seems only to work, when T is no class template already...
void operator= (T* r)
{
p_ = r;
}
// WHILE this ALWAYS works as well...
void simplySetIt (T* r)
{
p_ = r;
}
};
template <typename T>
class _ref_vector : public _reference<vector<T> >
{
};
void test2 ()
{
_reference<vector<long> > ref_ptr2;
_ref_vector<long> ref_ptr3;
ref_ptr2 = new vector<long>; // …Run Code Online (Sandbox Code Playgroud) 我做了以下运算符重载测试:
#include <iostream>
#include <string>
using namespace std;
class TestClass
{
string ClassName;
public:
TestClass(string Name)
{
ClassName = Name;
cout << ClassName << " constructed." << endl;
}
~TestClass()
{
cout << ClassName << " destructed." << endl;
}
void operator=(TestClass Other)
{
cout << ClassName << " in operator=" << endl;
cout << "The address of the other class is " << &Other << "." << endl;
}
};
int main()
{
TestClass FirstInstance("FirstInstance");
TestClass SecondInstance("SecondInstance");
FirstInstance = …Run Code Online (Sandbox Code Playgroud) c++ class operator-overloading operators assignment-operator
在C++中,operator=在一个应该是不可变的类上定义的惯用方法是什么.例如,它的所有成员变量都是const.
typedef unsigned char byte;
class Binary
{
protected:
const unsigned long size;
const byte* bytes;
public:
Binary(const unsigned long size);
Binary(const Binary &b);
~Binary(void);
Binary& operator=(const Binary &b);
};
Run Code Online (Sandbox Code Playgroud)
where bytes是指向malloc运行时编辑的内存块的指针.
我是否定义了一个空赋值运算符或让它使用自动生成的运算符明显失败?
我试图在几个选择类上实现和强制执行单一赋值语义.
可能重复:
测试NA并根据结果选择值
假设你有一个向量 - 你对向量进行计算 - 许多元素返回"NA" - 你如何识别这些"NA"并将它们更改为某个可用的整数
class sample
{
private:
int radius;
float x,y;
public:
circle()
{
}
circle(int rr;float xx;float yy)
{
radius=rr;
x=xx;
y=yy;
}
circle operator =(circle& c)
{
cout << endl<<"Assignment operator invoked";
radius=c.radius;
x=c.x;
y=c.y;
return circle(radius,x,y);
}
}
int main()
{
circle c1(10,2.5,2.5);
circle c1,c4;
c4=c2=c1;
}
Run Code Online (Sandbox Code Playgroud)
在重载'='函数中的语句
radius=c.radius;
x=c.x;
y=c.y;
Run Code Online (Sandbox Code Playgroud)
本身使所有c2的数据成员都等于c1,那么为什么需要返回?类似地,在c1 = c2 + c3中,使用重载+运算符添加c2和c3,并将值返回到c1,但不会变为c1 =,因此我们不应该使用another =运算符来分配总和c2和c3到c1?我糊涂了.
c++ return operator-overloading inner-classes assignment-operator
假设我有这个:
struct coor
{
int x;
int y;
COORD operator=(coor c)
{
COORD C={c.x,c.y}
return C;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要这样做:
coor c={0,0};
COORD C=c;
Run Code Online (Sandbox Code Playgroud)
我可以添加运算符重载coor,但是你怎么做到返回左侧?
我有一个类方法,它与一个对象的副本一起工作(确切地说是*).泄漏发生在重载的赋值运算符中 - 无论如何,这就是Visual Leak Detector所说的.我正在做的是使用副本,如果完成的工作是令人满意的,我将新创建的对象复制回来.我还实现了自定义析构函数,复制构造函数和赋值运算符,因为问题出现在动态分配的内存中.我使用C++的经验非常有限,因此代码中可能存在一些恶意内容.
如果需要,我会提供更多信息.
问题方法:
bool Grid::SurroundShipSquares(int top, int bottom, int left, int right)
{
// copying itself
Grid gridCopy(*this);
Square** squaresCopy = gridCopy.GetSquares();
for (int i = top; i <= bottom; ++i)
{
for (int j = left; j <= right; ++j)
{
if (squaresCopy[i][j].GetState() != SquareState::Vacant)
return false;
(squaresCopy[i][j]).SetState(SquareState::Unoccupiable);
}
}
// the problem occurs here
*this = gridCopy;
return true;
}
Run Code Online (Sandbox Code Playgroud)
复制构造函数:
Grid::Grid(const Grid& source)
{
_position = source._position;
_size = source._size;
int dimensions = …Run Code Online (Sandbox Code Playgroud) c++ memory-leaks operator-overloading copy-constructor assignment-operator
我想知道,>>>=运营商可以应用什么?
它适用于以下情况:
int >>>= int
long >>>= long
long >>>= int
long >>>= short
short >>>= long
short >>>= short
int >>>= byte
但不包括任何东西,包括float或double.
这是为什么,以及如何使之成为工作float和double?
谢谢!
java types variable-assignment assignment-operator operator-keyword
让我们假设我的类没有明确的复制构造函数.是否可以禁止为此类分配或复制对象的操作?例如:
class A
{
// data, methods, but no copy constructor and no overloaded assignment operator
};
A object1;
A object2;
object1 = object2; // make compiler error here
A object3 = object1; // or here
Run Code Online (Sandbox Code Playgroud) 下面我的复制构造函数工作正常,但我不明白我的复制赋值运算符有什么问题.
#include <iostream>
template <typename... Ts> class foo;
template <typename Last>
class foo<Last> {
Last last;
public:
foo (Last r) : last(r) { }
foo() = default;
foo (const foo& other) : last(other.last) { }
foo& operator= (const foo& other) {
last = other.last;
return *this;
}
};
template <typename First, typename... Rest>
class foo<First, Rest...> : public foo<Rest...> {
First first;
public:
foo (First f, Rest... rest) : foo<Rest...>(rest...), first(f) { }
foo() = default;
foo (const foo& other) …Run Code Online (Sandbox Code Playgroud)