我想知道你们是否可以帮助我.
这是我的.h:
Class Doctor {
const string name;
public:
Doctor();
Doctor(string name);
Doctor & Doctor::operator=(const Doctor &doc);
}
Run Code Online (Sandbox Code Playgroud)
我的主要人物:
int main(){
Doctor d1 = Doctor("peter");
Doctor d2 = Doctor();
d2 = d1;
}
Run Code Online (Sandbox Code Playgroud)
我想做operator = function.谁能帮我?请注意Doctor上的const成员.
************编辑:*********我的主要问题是我希望另一个类有一个属性,就像一个医生,像一个Pacient有一个医生.但我希望能够改变我的医生.就像我看到医生A,但我想看医生B.这将在我的其他课程(Pacient)中使用setDoctor函数完成.如果是我在做代码我会说这样的话:
class Patient{
Doctor &d;
};
Run Code Online (Sandbox Code Playgroud)
然后更改指针.但是我正在使用由其中一位老师制作的基本代码,它的类定义如下:
class Patient{
Doctor d;
}
Run Code Online (Sandbox Code Playgroud)
但我认为这是不可能的,因为在Patient类中使用setDoctor()我会复制或改变varable本身.第一个没有任何区别,第二个是不可能的,因为const.我对吗?
c++ const operator-overloading operators conversion-operator
好的,所以我有一个类'弱键入'IE它可以存储许多不同的类型定义为:
#include <string>
class myObject{
public:
bool isString;
std::string strVal;
bool isNumber;
double numVal;
bool isBoolean;
bool boolVal;
double operator= (const myObject &);
};
Run Code Online (Sandbox Code Playgroud)
我想像这样重载赋值运算符:
double myObject::operator= (const myObject &right){
if(right.isNumber){
return right.numVal;
}else{
// Arbitrary Throw.
throw 5;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我可以这样做:
int main(){
myObject obj;
obj.isNumber = true;
obj.numVal = 17.5;
//This is what I would like to do
double number = obj;
}
Run Code Online (Sandbox Code Playgroud)
但当我这样做时,我得到:
error: cannot convert ‘myObject’ to ‘double’ in initialization
Run Code Online (Sandbox Code Playgroud)
在任务.
我也尝试过:
int main(){
myObject obj; …Run Code Online (Sandbox Code Playgroud) c++ operator-overloading operators type-conversion conversion-operator
我有点困惑.让我们假设我有一个帮助类Data
class Data
{
public:
Data(const QVariant &value) : m_Variant(value) { }
operator QString() const { return m_Variant.toString(); }
private:
QVariant m_Variant;
};
Run Code Online (Sandbox Code Playgroud)
那时当我这样做:
Data d("text");
QString str = d; //str becomes "text"
Run Code Online (Sandbox Code Playgroud)
它有效,但当我继续做:
Data d2("text2");
str = d2; //does not compile
Run Code Online (Sandbox Code Playgroud)
抱怨失败:
ambiguous overload for 'operator=' (operand types are 'QString' and 'Data')
candidates are:
...
QString &operator=(const QString &);
QString &operator=(QString &&);
QString &operator=(const char*); <near match>
no known conversion from Data to const char*
QString &operator=(char);
Run Code Online (Sandbox Code Playgroud)
但即使提供 …
c++ qt conversion-operator overload-resolution implicit-conversion
请考虑以下示例:
#include <string>
#include <sstream>
struct Location {
unsigned line;
template<typename CharT, typename Traits>
operator std::basic_string<CharT, Traits>() const {
std::basic_ostringstream<CharT, Traits> ss;
ss << line;
return ss.str();
}
};
int main()
{
using namespace std::string_literals;
Location loc{42};
std::string s1 = "Line: "s.append(loc) + "\n"s; // fine
//std::string s2 = "Line: "s + loc + "\n"s; // error
}
Run Code Online (Sandbox Code Playgroud)
注释行会导致编译错误:no match for 'operator+'.为什么?我最初的想法是,它首先用于operator std::string转换然后执行调用operator+,就像它所做的那样.append.
它只是隐式转换的一个级别,所以它应该被执行并且应该被考虑在内,不是吗?
请考虑以下代码段:
template <typename>
struct dependent_false { static constexpr auto value = false; };
struct foo
{
foo() { }
template <typename T>
foo(const T&) { static_assert(dependent_false<T>::value, ""); }
};
struct proxy
{
operator foo() { return foo{}; }
};
int main()
{
(void) foo{proxy{}};
}
Run Code Online (Sandbox Code Playgroud)
编译时-std=c++17:
clang++ (trunk)成功编译代码;
g++(trunk)无法编译代码 - 它实例化foo(const T&).
编译时-std=c++11,两个编译器都拒绝代码.C++ 17中新的prvalue实现规则可能会影响此处的行为.
这里的正确行为是什么?
标准是否保证foo::foo(const T&)将(或不会)实例化?
标准是否保证隐式转换运算符优先于调用foo::foo(const T&),而不管它是否被实例化?
在 C++20 中,有一条规则,非正式地称为“当返回变量时,首先尝试移动,然后才复制”。
\n更正式地说是[class.copy.elision]/3
\n\n隐式可移动实体是具有自动存储持续时间的变量,它可以是非易失性对象,也可以是对非易失性对象类型的右值引用。在以下复制初始化上下文中,可能会使用移动操作而不是复制操作:
\n\n
\n- 如果
\nreturn([stmt.return])orco_\xc2\xadreturn([stmt.return.coroutine])语句中的表达式是一个(可能带括号的)id 表达式,它命名在最内层封闭函数或 lambda 表达式的主体或参数声明子句中声明的隐式可移动实体,...首先执行重载决策以选择要复制的构造函数或
\nreturn_\xc2\xadvalue要调用的重载,就好像表达式或操作数是右值一样。...
让我们考虑一下函数模板
\ntemplate<typename T>\nT f() {\n struct {\n T x;\n operator T&&() && { return static_cast<T&&>(x); }\n } s;\n return s;\n}\nRun Code Online (Sandbox Code Playgroud)\n它返回一个自动存储持续时间的变量s。并且有一个采用右值引用的转换运算符&&,可用于构造类型的函数返回值T。
我预计由于上述规则,它适用于任何类型T。然而它并没有:
struct A{};\n\nint main() {\n // ok in GCC and Clang\n f<A>();\n // error everywhere\n //f<int>();\n}\nRun Code Online (Sandbox Code Playgroud)\n使用 …
c++ conversion-operator language-lawyer move-semantics c++20
我一直在查看源代码,试图了解有关C++的更多信息,但我遇到了一些令人困惑的代码.我无法通过玩它来弄清楚它的用途.
请问某人可以解释操作符浮动*()的作用以及如何使用它?
class Vector
{
public:
float x,y,z;
Vector() : x(0), y(0), z(0){
}
Vector( float x, float y, float z ) : x(x), y(y), z(z){
}
operator float*(){
return &x;
}
operator const float *(){
return &x;
}
Run Code Online (Sandbox Code Playgroud)
我已经搜索了StackOverflow,它看起来像是一个转换运算符,但我仍然不确定它实际上是做什么以及为什么它有用.
亲切的问候,
考虑以下类,就像一个简单的例子:
#include <iostream>
#include <string>
using namespace std;
class point {
public:
int _x{ 0 };
int _y{ 0 };
point() {}
point(int x, int y) : _x{ x }, _y{ y } {}
operator string() const
{ return '[' + to_string(_x) + ',' + to_string(_y) + ']'; }
friend ostream& operator<<(ostream& os, const point& p) {
// Which one? Why?
os << static_cast<string>(p); // Option 1
os << p.operator string(); // Option 2
return os;
}
};
Run Code Online (Sandbox Code Playgroud)
是否应该直接调用转换运算符,或者只是调用static_cast …
在C++ Primer 第 5 版中。, 第 14 章讨论转换运算符:
在该标准的早期版本中,想要定义到 bool 的转换的类面临一个问题:因为 bool 是一种算术类型,转换为 bool 的类类型对象可以在任何需要算术类型的上下文中使用。
这种转换可能以令人惊讶的方式发生。特别是,如果 istream 转换为 bool,则以下代码将编译:
Run Code Online (Sandbox Code Playgroud)int i = 42; cin << i; // this code would be legal if the conversion to bool were not explicit!该程序尝试在输入流上使用输出运算符。没有
<<定义 foristream,所以代码几乎肯定是错误的。然而,这个代码可以使用布尔转换操作符转换cin到bool。然后将生成的 bool 值提升为 int 并用作左移运算符的内置版本的左侧操作数。提升的布尔值(1 或 0)将左移 42 个位置。
输入流可以转换为表示流内部状态(成功或失败)的布尔值。我们曾经这样做:
while(std::cin >> str)...
Run Code Online (Sandbox Code Playgroud)
那么为什么不应该编译呢?
int x = 0;
std::cin << x;
Run Code Online (Sandbox Code Playgroud)
如果我使用显式转换,它会起作用:
(bool)cin << 5; // works although bad
Run Code Online (Sandbox Code Playgroud) 我正在编写一个轻量级解析器组合库(类似于 Boost::Spirit)作为业余项目。
有一件事我想要做的,是自动能够转换Result<std::tuple<char>>,Result<std::tuple<char, char>>等成std::string。
同样,如果有例如 aResult<std::tuple<int, int>>我希望能够将其转换为 aResult<std::vector<int>>或更一般地对于包含零个或多个相同类型元素的任何元组,T我希望能够将其自动转换为 a Result<Container<T>>。
一个人如何处理这样的事情?我尝试例如:
template<typename Container>
Result<decltype(std::make_from_tuple<Container>(std::declval<T>()))> () const {
Result{Container{std::make_from_tuple<std::initializer_list<typename std::tuple_element<0, T>::type>> (std::get<T>(*this))}};
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为事实证明不可能像这样以编程方式创建初始化列表。
c++ conversion-operator template-meta-programming stdtuple c++17