我已经声明了一个类型为 ref 数据的类型。所以它看起来像这样
my_type type ref to data.
Run Code Online (Sandbox Code Playgroud)
然后我声明一个内部表,我想将它分配给 my_type。
Data:
ref_data type my_type.
itable type it_table.
ref_data = itable.
Run Code Online (Sandbox Code Playgroud)
为什么我不能将 itable 分配给 ref_data,数据的 ref 不是通用数据类型并且可以分配给任何东西吗?
C/C++ 三元运算符实际上与赋值运算符具有相同的优先级吗?
Luchian Grigore 的回答说,类似的情况
a ? b : c = d
Run Code Online (Sandbox Code Playgroud)
总是会被推断为
a ? b : ( c = d )
Run Code Online (Sandbox Code Playgroud)
因为 = 和 ?: 从右到左关联,所以
在 C++ 中
k = 21 > 3 ? j = 12 : j = 10;
Run Code Online (Sandbox Code Playgroud)
和
k = 1 > 3 ? j = 12 : j = 10;
Run Code Online (Sandbox Code Playgroud)
两者都很好。
在C中
k = 21 > 3 ? 12 : j = 10
Run Code Online (Sandbox Code Playgroud)
返回错误
invalid lvalue in assignment.
Run Code Online (Sandbox Code Playgroud)
上面不应该被推断为(并且不返回错误)
k= 21 > …Run Code Online (Sandbox Code Playgroud) c c++ conditional-operator associativity assignment-operator
在我正在处理的项目的现有类中,我遇到了一些奇怪的代码:赋值运算符调用了复制构造函数.
我添加了一些代码,现在赋值运算符似乎会造成麻烦.如果我只使用编译器生成的赋值运算符,它工作正常.所以我找到了解决方案,但我仍然很想知道为什么这不起作用.
由于原始代码是数千行,因此我创建了一个更简单的示例供您查看.
#include <iostream>
#include <vector>
class Example {
private:
int pValue;
public:
Example(int iValue=0)
{
pValue = iValue;
}
Example(const Example &eSource)
{
pValue = eSource.pValue;
}
Example operator= (const Example &eSource)
{
Example tmp(eSource);
return tmp;
}
int getValue()
{
return pValue;
}
};
int main ()
{
std::vector<Example> myvector;
for (int i=1; i<=8; i++) myvector.push_back(Example(i));
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); ++i)
std::cout << ' ' << myvector[i].getValue();
std::cout << '\n';
myvector.erase (myvector.begin(),myvector.begin()+3);
std::cout …Run Code Online (Sandbox Code Playgroud) 我必须实现基本相同的功能,但不同的大小.具体来说就是......
type& operator=(unsigned int);
type& operator=(unsigned long int);
type& operator=(unsigned long long int);
type& operator=(int);
type& operator=(long int);
type& operator=(long long int);
type& operator=(short int);
//so on and so forth...
Run Code Online (Sandbox Code Playgroud)
他们必须做同样的事情...(除了我应该考虑到不同的大小),主要的想法是"如果类型是最广泛使用任务的代码...否则执行转换和执行代码".是否可以通过仅使用一种方法来避免所有这样的重复代码?(我只是不希望编译器在编译时给我一些警告......).
谢谢
我在使用无法更改的第 3 方代码时发现了一个问题。我需要制作对象成员的副本。我不能严格执行此操作,因为内部成员之一具有私有赋值运算符。我找到的唯一解决方案很棘手,所以我想问你是否看到任何可能影响我的程序的红灯。
这是我正在处理的简化代码(请记住,我无法更改它!):
#include <iostream>
#include <algorithm>
class MBool
{
public:
MBool() {};
MBool(const MBool& arg) {}
private:
MBool& operator=(const MBool& arg);
};
class InnerContent {
private:
int* pBuffer;
public:
InnerContent() {
pBuffer = new int[20];
std::cout << "InnerContent()" << std::endl;
}
InnerContent(const InnerContent& otherInnerContent) {
pBuffer = new int[20];
std::copy(otherInnerContent.pBuffer, otherInnerContent.pBuffer + 20, pBuffer);
std::cout << "InnerContent(const InnerContent&)" << std::endl;
}
~InnerContent() {
std::cout << "~InnerContent()" << std::endl;
delete [] pBuffer;
pBuffer = nullptr;
}
virtual …Run Code Online (Sandbox Code Playgroud) 我有点迷失在 C++ 运算符中。我想为两个不同的类强制执行赋值运算符,即这样可以相互分配一个:
class A {
public:
virtual A &operator =(const A &a) = 0;
};
class B : public A {
public:
virtual A &operator =(const A &a) override {
std::cout << "B" << std::endl;
return *this;
}
};
class C : public A {
public:
virtual A &operator =(const A &a) override {
std::cout << "C" << std::endl;
return *this;
}
};
int main(int argc, char *argv[])
{
B b;
C c;
b = c;
// leads to …Run Code Online (Sandbox Code Playgroud) 考虑以下C++代码,我试图避免偏向非模板复制和移动构造函数和赋值运算符:
template<typename T> class A {
public:
A() { /* implementation here */ }
// Remove from the overloads the default copy&move constructors and assignment operators
A(const A&) = delete;
A& operator=(const A&) = delete;
A(A&&) = delete;
A& operator=(A&&) = delete;
// I want these to be used e.g. by std::vector
template<typename U> A(const A<U>& fellow) { /* implementation here */ }
template<typename U> A& operator=(const A<U>& fellow) { /* implementation here */ }
template<typename U> A(A<U>&& …Run Code Online (Sandbox Code Playgroud) c++ templates copy-constructor default-constructor assignment-operator
我正在学习 C++,我有一个关于赋值运算符的问题。
根据这里写的内容https://en.cppreference.com/w/cpp/language/copy_assignment,似乎
...一个类可以有多个复制赋值运算符,例如 T& T::operator=(const T &) 和 T& T::operator=(T)。
我试图用两个运算符创建一个类,但我看不出我错在哪里,因为我从编译器中得到了这个:
错误 C2593:“运算符 =”不明确*
这是课程:
class Point2D
{
public:
Point2D(); // default constructor
Point2D(double xValue, double yValue); // overloaded constructor
Point2D(const Point2D& ref); // copy constructor const
Point2D(Point2D& ref); // copy constructor for copy and swap
Point2D(Point2D&& moveRef); // move constructor
~Point2D(); // destructor
Point2D& operator=( const Point2D& other ); // copy assignment operator const
Point2D& operator=( Point2D other ); // copy assignment operator for copyAndSwap
private: …Run Code Online (Sandbox Code Playgroud) c++ operator-overloading assignment-operator language-lawyer
我有以下代码:
#include <vector>
#include <iostream>
std::vector <int> a;
int append(){
a.emplace_back(0);
return 10;
}
int main(){
a = {0};
a[0] = append();
std::cout << a[0] << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该函数append()的副作用是将向量大小增加一。由于向量的工作方式,当超出其容量时,可能会触发其内存的重新分配。
因此,在执行 时a[0] = append(),如果发生重新分配,则 thena[0]无效并指向向量的旧内存。因此,您可以预期向量最终会变为,{0, 0}而不是{10, 0},因为它分配给旧向量a[0]而不是新向量。
让我感到困惑的是,这种行为在 C++14 和 C++17 之间发生了变化。
在 C++14 上,程序将打印 0。在 C++17 上,它将打印 10,这意味着a[0]实际上分配了 10。所以,我有以下问题,但我找不到答案:
a[0]在评估赋值表达式的 RHS 之后评估 的内存地址?C++14 之前是否对此进行了评估,这就是它发生变化的原因?volatile int lhs = 1;
int rhs = 2;
int x = 3;
x = lhs = rhs;
Run Code Online (Sandbox Code Playgroud)
赋值运算符是否返回 (typeof lhs)rhs ?或者它是否返回新的,只是读取 lhs 的值?这对我来说很重要,因为 lhs 是不稳定的,并且它可以在分配之间发生变化。
我在 cppreference 找不到答案。