类似问题:
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<vector<int> > vvi;
vvi.resize(1);
vvi[0].reserve(1);
vvi[0][0] = 1;
vector<int> vi = vvi[0];
cout << vi[0]; // cout << vvi[0][0]; works
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个段错误,我不知道为什么.
我正在阅读一些c ++的赋值运算符理论.
让我们说吧
class MyClass {
private:
T1 member1;
T2 member2;
public:
// The default copy assignment operator which assigns an object via memberwise copy
MyClass & operator=(const MyClass & rhs) {
member1 = rhs.member1;
member2 = rhs.member2;
return *this;
}
......
}
Run Code Online (Sandbox Code Playgroud)
和
c7 = c6; //member wise copy assignment.
Run Code Online (Sandbox Code Playgroud)
这里我们在赋值操作期间返回对象的引用,并为其指定新对象c7.
但是,如果我的代码有点像这样:
int a=12;
int &b=a;
int c=&b; //error::invalid conversion from ‘int*’ to ‘int’
Run Code Online (Sandbox Code Playgroud)
为什么这与上述情况不同?
我写了下面的类,它重载了赋值运算符.如示例所示,我*this从赋值运算符返回.
class Sample
{
int *p;
int q;
public:
Sample()
{
cout<<"Constructor called"<<endl;
p = new int;
q = 0;
}
Sample& Sample::operator =(const Sample &rhs)
{
cout<<"Assignment Operator"<<endl;
if(this != &rhs)
{
delete p;
p = new int;
*p = *(rhs.p);
}
return *this;
}
void display()
{
cout<<"p = "<<p<<" q = "<<q<<endl;
}
};
Run Code Online (Sandbox Code Playgroud)
当我调用赋值运算符时a = b,就像,a.operator=(b);.
现在我正在调用一个运算符函数,这已经被传递,operator =然后为什么需要从赋值运算符返回它?
考虑以下程序编译并运行正常:
#include <iostream>
#include <string>
using std::string;
struct BB
{
// generic cast
template<typename T>
operator T() const
{
std::cout<<"Generic cast\n";
return 0;
}
// string cast
operator string() const
{
std::cout<<"string cast\n";
return string("hello");
}
};
int main()
{
BB b;
string s = b; // copy constructor
}
Run Code Online (Sandbox Code Playgroud)
但如果我稍微更改main()函数的代码如下:
int main()
{
BB b;
string s;
s = b;
}
Run Code Online (Sandbox Code Playgroud)
编译器给出以下错误消息(请参阅此处的实时演示)
[Error] ambiguous overload for 'operator=' (operand types are 'std::string {aka std::basic_string<char>}' and 'BB') …Run Code Online (Sandbox Code Playgroud) 我一直在使用:
string letters = THESAMELENGTH; // Assign for allocation purposes.
Run Code Online (Sandbox Code Playgroud)
原因是,如果我:
string letters[THESAMELENGTH.length()];
Run Code Online (Sandbox Code Playgroud)
我收到一个非常量表达式投诉。
但如果我:
string letters[12];
Run Code Online (Sandbox Code Playgroud)
如果指南 const 字符串更改大小,我将面临需要更改每个实例的风险。
但是当我不使用这些条目时分配一个字符串似乎很愚蠢,我只希望我新分配的字符串与之前分配的 const 字符串长度相同,然后填充不同的值。
您如何建议我优雅且安全地执行此操作?
是什么的语句之间的差别a = 5,a(5),a{5}以及a[5]在C ++?
我偶尔会看到它们用于诸如在 C++ 程序中为对象赋值之类的事情。它们之间的区别在哪里?
c++ syntax initialization variable-assignment assignment-operator
我使用了大于和小于符号,它给出了输出!它是如何工作的?
int x = 2;
x >= 3;
cout << x; // output is 2
Run Code Online (Sandbox Code Playgroud)
而且输出也是不同的
int x = 2;
x = x > 3;
cout << x; // output is zero !! HOW ??
Run Code Online (Sandbox Code Playgroud) c++ bitwise-operators assignment-operator comparison-operators relational-operators
代码是:
#include<iostream>
using namespace std;
class Integer
{
int num;
public:
Integer()
{
num = 0;
cout<<"1";
}
Integer(int arg)
{
cout<<"2";
num = arg;
}
int getValue()
{
cout<<"3";
return num;
}
};
int main()
{
Integer i;
i = 10; // calls parameterized constructor why??
cout<<i.getValue();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,语句i=10调用了参数化构造函数。你能解释一下吗。
c++ constructor assignment-operator parameterized-constructor
这个逻辑是如何工作的: k+=(x=5,y=x+2,z=x+y);
它将如何给出结果 k == 22。当我初始化 k = 10 的值时
#include <stdio.h>
int main()
{
int x,y,z,k=10;
k+=(x=5,y=x+2,z=x+y);
printf("value of k %d ",k); //why it will show value of k =22
return 0;
}
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main()
{
int i,j;
i=j=(22,23,24);
printf("i:%d",i);
printf("\nj:%d",j);
}
Run Code Online (Sandbox Code Playgroud)
这将 i,j 的输出都设为 24。
#include <stdio.h>
int main()
{
int i,j;
i=j=22,23,24;
printf("i:%d",i);
printf("\nj:%d",j);
}
Run Code Online (Sandbox Code Playgroud)
这使 i,j 都为 22。有人可以解释背后的术语吗。TIA