考虑这段代码
string str;
cout << str[0] << str.size();
Run Code Online (Sandbox Code Playgroud)
而我得到的是不是一个运行时错误,但是" 0",一个0下面的空间.为什么这可能?
我的印象是,通过声明返回类型const,您可以防止修改数据结构.但是,我测试了这个,我可以修改数据结构.这是为什么?
例如,以下代码1 2 3 4 5 6在编译时打印--std=c++11:
#include <iostream>
#include <set>
using namespace std;
const set<int> f(void) {
set<int> s = {1, 2, 3, 4, 5};
return s;
}
int main(void) {
set<int> s = f();
s.insert(6);
for (auto elem: s) {
cout << elem << " ";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) http://en.cppreference.com/w/cpp/language/rule_of_three
我几个月前开始使用c ++ 11,并且已经看过五条规则.
所以..我开始在每个具有虚析构函数的类上使用default关键字复制构造函数/复制赋值运算符/移动构造函数/移动赋值运算符.
因为规则告诉我,如果你声明显式析构函数,那么你的类就没有它的隐式移动构造函数和移动赋值运算符了.
所以我认为gcc会向我抱怨由于缺少移动构造函数和移动赋值运算符而导致下面的类.
但它运作良好!发生了什么事??
class Interface {
public:
virtual ~Interface() = default; // implicit destructor
};
class ImplA : public Interface {
public:
virtual ~ImplA() = default; // implicit destructor
};
ImplA first;
ImplA second(first); // copy constructor, OK. understood it.
ImplA third(std::move(first)); // move constructor, it's OK. Why?
second = first; // copy assignment, OK. understood it.
second = std::move(first); // move assignment, it's also OK. Why?
Run Code Online (Sandbox Code Playgroud) 我试图覆盖C ++中的虚函数。在我重写该函数之后,它实际上并未覆盖它,因此使该类成为抽象的。下面的代码将使您对问题有很好的了解。
正如您在下面看到的,该代码对于像int这样的非指针模板也能正常工作,但使用int指针失败。
我以为也许是因为引用指针存在问题,所以我在实现Derived2的过程中取出了&,但并没有解决。
template<class T>
class Base {
virtual void doSomething(const T& t) = 0;
};
class Derived1: public Base<int>{
void doSomething(const int& t) {
} // works perfectly
};
class Derived2: public Base<int*>{
void doSomething(const int*& t) {
}
// apparently parent class function doSomething is still unimplemented, making Derived2 abstract???
};
int main(){
Derived1 d1;
Derived2 d2; // does not compile, "variable type 'Derived2' is an abstract class"
}
Run Code Online (Sandbox Code Playgroud) 为什么bitor与cout运算符一起使用时不起作用
这有效
int a=5,b = 6,d = a bitor b;
cout << d << endl;
Run Code Online (Sandbox Code Playgroud)
这引发错误
int a=5,b=6;
cout << a bitor b << endl;
Run Code Online (Sandbox Code Playgroud)
错误信息:
invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'
cout << a bitor b << endl;
Run Code Online (Sandbox Code Playgroud) 我试图了解移动构造函数和右值引用。所以我在https://www.onlinegdb.com/online_c++_compiler上尝试了此代码。但是结果使我感到困惑。
#include <iostream>
#include <type_traits>
class A {
public:
A() { std::cout << "Constructed" << std::endl; }
A(const A& )= delete;
A(A&&) { std::cout << "Move Constructed" << std::endl; }
};
int
main ()
{
A&& a = A();
A b = a; // error: use of deleted function ‘A::A(const A&)’
//A b = static_cast<decltype(a)>(a); // This works, WTF?
std::cout << std::is_rvalue_reference<decltype(a)>::value << std::endl; // pretty sure a is rvalue reference.
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有以下 C++ 代码。
string getName()
{
return "C++";
}
void printName(const char* name)
{
cout << name << endl;
}
int main()
{
printName(getName().c_str());
}
Run Code Online (Sandbox Code Playgroud)
该函数getName返回一个string. 我将函数c_str的指针传递给函数。我想知道在调用函数之前返回的内容是否会被删除。如果不是那么什么时候删除返回值。stringprintNamestringprintName()
我需要知道以下内容通常是否有效。
string s = "some value";
string v = s.substr(0, 50).c_str();
Run Code Online (Sandbox Code Playgroud)
分配v始终有效吗?由于所传回的物件的生存期会不会出现任何问题substr()?
我试图从另一个类中声明的数组中获取一些值。该数组具有固定长度和常量元素(我将 100% 从不修改其值,所以这就是我将其设为常量的原因)。
但是,当我尝试访问main函数中的第一个元素时,出现编译错误:
basavyr@Roberts-MacBook-Pro src % g++ -std=c++11 main.cc
Undefined symbols for architecture x86_64:
"Vectors::vec1", referenced from:
_main in main-c29f22.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在使用 clang(最新版本)在 macOS Catalina 上进行编译。
[问]:可能是什么问题?先感谢您。
这是代码:
#include <iostream>
class Dimension
{
public:
static constexpr int dim1 = 2;
static constexpr int dim2 = 1;
};
class Vectors
{
public:
static constexpr double vec1[2] = {4.20, 6.9};
};
int main()
{ …Run Code Online (Sandbox Code Playgroud) 在以下 C++20 函数模板中:
template<int i>
void f() {
if constexpr (i == 1)
g();
else if constexpr (i == 2)
h();
else
??? // <--error
}
Run Code Online (Sandbox Code Playgroud)
有什么我们可以写的东西,???这样f<3>()在编译时调用就会失败?
c++ ×10
c++11 ×5
string ×3
constexpr ×2
lifetime ×2
return-value ×2
temporary ×2
c++20 ×1
class ×1
const ×1
if-constexpr ×1
inheritance ×1
overriding ×1
polymorphism ×1
return-type ×1
templates ×1