我在这个头文件的 FileDir 类中有一个 operator== 类成员:
#include <sstream>
class FileDir {
public:
FileDir(std::string nameVal, long sizeVal = 4, bool typeVal = false);
FileDir(const FileDir &obj);
~FileDir(); // destructor
long getSize() const;
std::string getName() const;
bool isFile() const;
std::string rename(std::string newname);
long resize(long newsize);
std::string toString();
bool operator== (const FileDir &dir1);
private:
std::string name;
long size;
bool type;
};
Run Code Online (Sandbox Code Playgroud)
这是实现:
bool operator== (const FileDir &dir1) {
if (this->name == dir1.name && this->size == dir1.size && this->type == dir1.type)
return true;
else
return …Run Code Online (Sandbox Code Playgroud) 我试图operator ++在我的 Set 迭代器上定义来调用方法next(),所以它会增加迭代器中的位置。
template<typename TElement>
class SetIterator {
private:
Set<TElement>& set;
int poz;
public:
SetIterator(Set<TElement>& set, int poz) : set{ set }, poz{ poz } {
while (set.elems[this->poz] == EMPTY || set.elems[this->poz] == DELETED)
this->poz++;
};
SetIterator(const SetIterator& other) = default;
~SetIterator() = default;
bool valid() {
return poz < set.capacity;
};
void next() {
poz++;
while (set.elems[poz] == EMPTY || set.elems[poz] == DELETED)
poz++;
};
SetIterator<TElement>& operator ++ () {
next();
return *this;
}; …Run Code Online (Sandbox Code Playgroud)
我应该使用delete重载或del()函数或其他东西来释放类成员吗?
class Field
{
private:
size_t* square = new size_t[5];
public:
void del()
{
delete[] square;
}
void operator delete (void* p)
{
delete[] reinterpret_cast<Field*>(p)->square;
}
};
int main()
{
Field f;
delete &f;
//or
f.del();
}
Run Code Online (Sandbox Code Playgroud) c++ operator-overloading user-defined-functions class-members
我正在尝试[]在向量上重载运算符.我试图让这个运算符在Matlab或Python中工作,用于负索引或大于矢量长度的索引.我的问题不是得到正确的结果,而是实际重载运算符,知道我将在重载代码中使用非重载运算符.我需要它用于特定类的向量,但如果它适用于任何类型它会更好vector.
现在我的代码是:header:
MyClass std::vector::operator[](std::vector<MyClass> const vector,int const idx) const;
Run Code Online (Sandbox Code Playgroud)
资源:
Myclass vector::operator[](vector<MyClass> const vector,int const idx) const {
n=vector.size()
if((idx>=0) && (idx<n)){
return vector[idx];
}
else if(idx<0){
return vector[n+idx%n]
}
else{
return vector[idx%n]
}
}
Run Code Online (Sandbox Code Playgroud)
我得到错误:
error: 'template<class _Tp, class _Alloc> class std::vector' used without template parameters
error: non-member function 'MyClass operator[](std::vector<MyClass>, int)' cannot have cv-qualifier
error: 'MyClass operator[](std::vector<MyClass>, int)' must be a nonstatic member function
Run Code Online (Sandbox Code Playgroud)
如果这个问题已经讨论过了,我很抱歉,但是我找不到它......非常感谢你提前得到答案!
所以我的代码有问题,我想重载运算符<<,所有功能都在抽象类Employee中,所以
friend std::ostream &operator<<(std::ostream &os, const Employee &employee) {
os<<employee.print();
return os;
}
Run Code Online (Sandbox Code Playgroud)
这是一个函数打印:
virtual const std::string& print() const {
return "description: "+this->description+ " id: "+ std::to_string(this->getID()); }
Run Code Online (Sandbox Code Playgroud)
描述和ID只是Employee类中的一个变量
而且它不起作用,并且出现异常E0317,我理解它就像打印返回的不是字符串一样。另外,如果我将返回类型更改为
std::basic_string<char, std::char_traits<char>, std::allocator<char>>
Run Code Online (Sandbox Code Playgroud)
它神奇地起作用,但是我不明白为什么我不能使用标准字符串。
我有一个名为"Criterion"的类,我想实现==运算符,但我正在努力解决以下问题:
当我实现==运算符时,我正在检查我的一个或两个实例是否为空,但是当我这样做时,它会导致递归调用==然后我得到"StackOverflow"(他)异常.
从技术上讲,我可以实现Equals运算符而不是覆盖==,但如果我实现了==运算符,代码将更具可读性.
这是我的代码:
public static bool operator == (Criterion c1, Criterion c2)
{
if (null == c1)
{
if (null == c2)
return true;
return false;
}
if (null == c2)
return false;
if ((c1.mId == c2.mId) && (c1.mName == c2.mName))
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud) 我已经定义了一个Player类来进行一些操作,所以我可以方便地重载一些基本操作符.具体来说,我想使用<用于Player对象之间的比较.因此,我在课堂上有以下内容:
bool operator<(const Player& rhs) const {return (*this < rhs );}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这导致了问题.后来,当我尝试在main函数中输出包含特定元素的向量时,编译器让我知道<< operand没有匹配,并且它需要std :: ostream << Player.以下是导致问题的一行:
vector<Player> playerVec(6);
for (int i = 0; i < 6; i++) {
cout << playerVec[i];
}
Run Code Online (Sandbox Code Playgroud)
请注意,我实际上并不想直接输出任何Player对象流,所以我认为我不需要重载<<.
我对发生的事情有一些了解,因为编译器采用了我的特定定义<然后不打算寻找更一般的情况.我的问题是,我现在需要重载<<运算符以返回其一般功能,还是有更简单的解决方案?
感谢您提供的任何帮助!
有人可以解释下面的程序如何成为"AabAabAab .."的infinte循环.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Base {
public:
Base(int j=1):i(j)
{cout<<"B";}
private:
int i;
};
class Case{
public:
Case(int j=1):i(j) {cout<<"A";}
operator Base() { cout<<"ab"; return *(new Case); }
private:
int i;
};
int main()
{
Base obj = Case();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我试图operator<<在几个子类中重载.我有一个名为Question的超类,它有一个枚举值类型和一个字符串问题.该类的子类是TextQuestion,ChoiceQuestion,BoolQuestion和ScaleQuestion.TextQuestion没有其他数据字段.ChoiceQuestion有一个字符串向量,用于存储多项选择的可能性.BoolQuestion没有其他数据字段.ScaleQuestion有两个int值,low_和high_,用于比例.
class Question {
public:
enum Type{TEXT, CHOICE, BOOL, SCALE};
Question():
type_(), question_() {}
Question(Type type, std::string question):
type_(type), question_(question) {}
friend std::ostream& operator<<(std::ostream& out, const Question& q);
virtual void print(std::ostream& out) const;
virtual ~Question();
private:
Type type_;
std::string question_;
};
class TextQuestion: public Question {
public:
TextQuestion():
Question() {}
TextQuestion(Type type, std::string question):
Question(type, question) {}
void print(std::ostream& out) …Run Code Online (Sandbox Code Playgroud) 我在nullable int之间定义了这样的加法:
public static class Utils
{
public static int? operator +(int? value1, int? value2)
{
if (!value1.HasValue && !value2.HasValue)
return null;
int myValue1 = value1.HasValue ? value1.Value : 0;
int myValue2 = value2.HasValue ? value1.Value : 0;
return myValue1 + myValue2;
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以在.NET 3.5中使用此运算符?
是否可以在早期版本的.NET中使用?
PS.
显然实际上没有(?)方法使用扩展方法重载运算符,因为扩展方法必须在静态类中,而静态类不能有操作符重载....
PPS.
"已解决"使用
public static int? Add(this int? value1, int? value2)
Run Code Online (Sandbox Code Playgroud) c++ ×8
class ×3
c# ×2
.net ×1
constructor ×1
inheritance ×1
operators ×1
ostream ×1
overloading ×1
stdvector ×1
string ×1
subclass ×1
virtual ×1