我编写了一个重载的赋值运算符,类perform复制所有变量值.例如:在Exp.cpp中
class perform
{
LOG *ptr;
int a;
//constructor
//destructor
perform operator=(const perform & rhs){
ptr = rhs.ptr; a=rhs.s;
return * this;}
};
Run Code Online (Sandbox Code Playgroud)
在另一个类中output,我已经声明了一个指针abc.
perform * ptr = StatCol::CreateCol(frm);
abc = ptr; //this line should invoke assignment overloaded.
//but in my case it's not invoked.
Run Code Online (Sandbox Code Playgroud) 假设我有两个类,在两个不同的标题中,称为:
class TestA
{
public:
int A;
};
class TestB
{
public:
int B;
};
Run Code Online (Sandbox Code Playgroud)
而且我想给彼此一个赋值运算符,所以它就像:
class TestB; //Prototype of B is insufficient to avoid error with A's assignment
class TestA
{
public:
int A;
const TestA &operator=(const TestB& Copy){A = Copy.B; return *this;}
};
class TestB
{
public:
int B;
const TestB &operator=(const TestA& Copy){B = Copy.A; return *this;}
};
Run Code Online (Sandbox Code Playgroud)
如何在避免因尚未定义的调用/使用类TestB而导致的明显错误的同时执行上述操作?
我很困惑......为什么我的任务操作员不会在这里被调用?
template<typename This>
struct mybase
{
This& operator =(const This &other)
{
__debugbreak(); // The debugger should break here, but doesn't.
return static_cast<This &>(*this):
}
};
struct myderived : mybase<myderived>
{
int x;
};
int main()
{
myderived a = myderived(); // And yes, I know it's redundant...
myderived b = myderived();
a = b;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在阅读http://hyperpolyglot.org/scripting并偶然发现了// = assignemnt运算符.我以前从未见过它.它有什么作用?
我有一个名为*graph1的Graph指针,并且已经为它分配了内存(注意:不是问题的一部分,但Graph是模板类).我还创建了另一个名为graph2的Graph实例.我这样调用了一个重载赋值运算符
Graph<std::string,std::string> *graph1 = new Graph<std::string,std::string>;
...
... // Called member functions on graph1
Graph<std::string,std::string> graph2;
graph2 = *graph1;
Run Code Online (Sandbox Code Playgroud)
赋值运算符正常工作,但由于某种原因,在调用赋值运算符后,Graph的析构函数也会被调用.这是正常的还是我没有正确实现赋值运算符?
这是我实现赋值运算符的方式:
template <typename VertexType, typename EdgeType>
Graph<VertexType, EdgeType> Graph<VertexType, EdgeType>::operator=(const Graph<VertexType, EdgeType> &source)
{
std::cout << "\tAssignment Operator called\n\n";
if(this == &source)
return *this;
this->vecOfVertices = source.vecOfVertices;
this->orderPtr = source.orderPtr;
this->count = source.count;
return *this;
}
Run Code Online (Sandbox Code Playgroud) 我有一个类A,它在构造函数中为一个整数(由类memeber指出_pPtrMem指向)动态分配内存,并在析构函数中释放相同的内容.为了避免Shallow复制,我重载了赋值运算符和复制构造函数.赋值运算符重载的广泛使用方式如下:
A& operator = (const A & iToAssign)
{
if (this == & iToAssign) // Check for self assignment
return *this;
int * pTemp = new int(*(iToAssign._pPtrMem)); // Allocate new memory with same value
if (pTemp)
{
delete _pPtrMem; // Delete the old memory
_pPtrMem = pTemp; // Assign the newly allocated memory
}
return *this; // Return the reference to object for chaining(a = b = c)
}
Run Code Online (Sandbox Code Playgroud)
实现相同的另一种方法可能是
A& operator = (const A & iToAssign)
{
*_pPtrMem= *(iToAssign._pPtrMem); …Run Code Online (Sandbox Code Playgroud) 我有一个结构,Foo,指针数组为Bar.
struct Foo
{
Bar* _BarList;
Foo()
{
_BarList = new Bar[1];
_BarList[0] = Bar(10, 20);
}
Foo& operator=(const Foo& foo)
{
Bar* tmpBarList = new Bar[1];
tmpBarList[0] = foo._BarList[0];
delete[] _BarList;
_BarList = tmpBarList;
return *this;
}
~Foo()
{
delete[] _BarList;
}
};
Run Code Online (Sandbox Code Playgroud)
我这样称呼它
Foo baz = fooFunc();
Run Code Online (Sandbox Code Playgroud)
(似乎)如果我在函数(fooFunc)中创建一个Foo(f)实例并返回它,则在返回值之前调用析构函数,因为我丢失了_BarList的内容.
这在函数中创建是有意义的.这是一个例子.
Foo fooFunc()
{
Foo f = Foo();
return f;
}
Run Code Online (Sandbox Code Playgroud)
如果我在返回时直接返回Foo的实例,则直到调用了equals运算符之后才调用该项的析构函数(通过调用行上的equals语句).
Foo fooFunc()
{
return Foo();
}
Run Code Online (Sandbox Code Playgroud)
我想这是有道理的,因为我在对象中创建Foo,并且在返回对象之前清除了一些东西.
我想我可以通过这样做返回来解决这个问题(在编写一个新的构造函数来获取Foo之后):
Foo fooFunc()
{
Foo f = Foo();
return Foo(f);
} …Run Code Online (Sandbox Code Playgroud) 我正在创建一个程序,只有在命名为carley和zack时才会问候这个人.
string name1 = "Carley";
string name2 = "Zack";
Console.Write("What's your name? : ");
string name = Console.ReadLine();
if (name == name1)
{
Console.WriteLine("Glad to meet you " + name + "!");
}
else if (name == name2)
{
Console.WriteLine("Glad to meet you " + name + "!");
}
else
{
Console.WriteLine("press any key to exit.");
}
Console.Read();
Run Code Online (Sandbox Code Playgroud)
在这里,我希望通过尝试将输入的评估放在"if"的一个语句中来简化代码:
if (name == name1 && name2)
{
Console.WriteLine("Glad to meet you " + name + "!");
}
else
{
Console.WriteLine("press any …Run Code Online (Sandbox Code Playgroud) 我弄清楚为什么我似乎无法让std :: vector :: erase使用我自己的类对象的向量.下面的代码抛出一个"没有可行的重载'='"错误,并且无法弄清楚为什么经过一些广泛的搜索溢出/ tutorialspoint/...
我的类定义'MyClass.hpp':
#include <string>
#include <vector>
class node;
class graph{
public:
graph();
std::vector<node> nodeList;
};
class node{
public:
node(std::string name) : name(name){};
void operator=(node& rhs);
std::string name;
std::vector<node> adjacent;
};
void node::operator=(node& rhs){
name = rhs.name;
adjacent = rhs.adjacent;
}
Run Code Online (Sandbox Code Playgroud)
和我的主文件:
#include "MyClass.hpp"
int main(int argc, const char * argv[]) {
node node1("root"), node2("leaf");
node1.adjacent.push_back(node2);
node2.adjacent.push_back(node1);
node1.adjacent.erase(node1.adjacent.begin());
graph myGraph;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我了解如果我的类具有智能的唯一指针,则无法将该类分配给另一个实例,因为不能复制唯一指针。我知道我可以使唯一指针成为共享指针,这样就可以解决问题。但是,如果我不想共享指针的所有权怎么办?是否可以创建一个赋值运算符来移动唯一指针并复制其他变量?
我读过您可以std::move用来转让所有权。
#include <iostream>
#include <memory>
struct GraphStructure { };
class test {
int a;
std::vector<int> vector;
std::unique_ptr<GraphStructure> Graph_;
};
int main () {
test t1;
auto t2 = t1;
}
Run Code Online (Sandbox Code Playgroud)