如何在 C++ 中声明一个二维指针数组并用空指针初始化它?
我试图这样做
int *arr[20][30]= nullptr;
Run Code Online (Sandbox Code Playgroud) 我找到了一个 C++ 代码,我们将初始化为最大值的 unsigned int 32 转换为有符号 int 并期望它是-1. 它在经过测试的编译器上运行良好,但它真的可移植吗?
int GetBusinessDataID()
{
u32_t id = ~0;
// Some code that may return a valid ID.
return id; // Here we expect to return -1.
}
Run Code Online (Sandbox Code Playgroud) 我试图从另一个类中声明的数组中获取一些值。该数组具有固定长度和常量元素(我将 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) 如果我有代码:
struct Test
{
int x = 10;
};
int main()
{
std::list<Test> linkedList;
std::cout << linkedList.front().x << std::endl;
}
---
out -> 0
Run Code Online (Sandbox Code Playgroud)
为什么我的 test.x 值是 0?如果我将列表更改为 int 类型,它返回 0。如果我给它一个 char 类型,我什么也得不到(或“”)。
我很好奇它是如何(以及为什么)发生在引擎盖下的。它如何处理返回任何类型的值而不退出程序或需要尝试/捕获?
我试图理解移动构造函数。
我在类的构造函数中分配内存并在析构函数中销毁它。
当我尝试移动类时,我仍然有一个双空闲。
#include <algorithm>
class TestClass
{
public:
TestClass() {a_ = new int[1];}
TestClass(TestClass const& other) = delete;
TestClass(TestClass && other) noexcept // = default;
{
this->a_ = std::move(other.a_);
}
~TestClass() {delete[] a_;}
private:
int* a_ = nullptr;
};
int main( int argc, char** argv )
{
TestClass t;
TestClass t2 = std::move(t);
}
Run Code Online (Sandbox Code Playgroud)
为什么std::move不改为 nullptr other.a_?
如果移动构造函数是默认的,我也有同样的问题。
我发现了以下问题,但我仍然不知道为什么移动运算符不将源变量更改为默认值。
以下是我正在尝试的玩具代码...我理解第一个和第二个。第一个将所有权授予_p. 第二个复制p到_p. 但第三个我没看懂……
std::move的是什么const shared_ptr &意思?谢谢。
class P { };
class A {
public:
// first one
A(std::shared_ptr<P> &p, int) : _p(std::move(p))
{
std::cout << "1st Ctor: "
<< p.use_count() << ", " << _p.use_count() << std::endl;
}
// second one
A(const std::shared_ptr<P> &p, std::string) : _p(p)
{
std::cout << "2nd Ctor: "
<< p.use_count() << ", " << _p.use_count() << std::endl;
}
// third one
A(const std::shared_ptr<P> &p) : _p(std::move(p)) …Run Code Online (Sandbox Code Playgroud) 我需要实现一个接受任何 STL 容器的模板函数。并基于什么样的容器来执行某些操作。
例子:
template <class Container, class T>
void func(Container<T> container) {
if (container == std::map) {
...
} else {
...
}
}
int main() {
std::vector<int> v1;
func(v1); // ok
std::vector<double> v2;
func(v2); // ok
std::map<int, double> m1;
func(m1); // ok
std::list<int> l1;
func(l1); // ok
}
Run Code Online (Sandbox Code Playgroud) 有没有办法在没有基类名称和范围解析运算符的情况下引用基类模板的成员变量?
template<typename D>
struct B0 {
int value;
};
struct D0: B0<D0> {
D0() {
B0<D0>::value = 1; // OK.
value = 1; // OK without `B0<D0>::`.
}
};
template<typename T>
struct B1 {
T value;
};
template<typename T>
struct D1: B1<T> {
D1() {
B1<T>::value = 1; // OK.
// value = 1; // Compile error without `B1<T>::`.
// Compile error: use of undeclared identifier 'value'
// `B1<T>::` is tedious everywhere `value` is referenced.
}
};
template<typename T, typename …Run Code Online (Sandbox Code Playgroud) 如题
#include <iostream>
int main() {
auto* a = new float[1000000];
auto* b = new float[10]();
for(auto i=0; i<1000000; i++){
std::cout << "a" << a[i] << std::endl;
}
for(auto i=0; i<10; i++){
std::cout << "b" << b[i] << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有什么不同?我试过两个输出都是零。
另外关于智能指针,如何确保它可以零初始化。
std::unique_ptr<int[]> p = std::make_unique<int[]>(100);
Run Code Online (Sandbox Code Playgroud) 我不明白在这种情况下参数推导规则是如何工作的。我有以下简单的代码片段:
template<typename T>
void fn(T const &&t) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
std::cout << typeid(decltype(t)).name() << std::endl;
}
int main() {
int const *ar = nullptr;
std::cout << typeid(ar).name() << std::endl;
fn(std::move(ar));
}
Run Code Online (Sandbox Code Playgroud)
我得到的结果如下:
PKi
void fn(const T &&) [T = const int *]
PKi
Run Code Online (Sandbox Code Playgroud)
我不明白的是为什么T被推断为const int *. 为什么const没有获得模式匹配?
c++ ×10
c++11 ×4
templates ×3
stdmove ×2
arrays ×1
base-class ×1
class ×1
constants ×1
constexpr ×1
name-lookup ×1
new-operator ×1
null ×1
pointers ×1
shared-ptr ×1
stdlist ×1
template-argument-deduction ×1
unique-ptr ×1
unsigned ×1