小编mic*_*t38的帖子

if-指令宏比较

为什么#if满足以下代码中的条件:

#include <iostream>
#define VALUE foo    

int main() {    
#if VALUE == bar
    std::cout << "WORKS!" << std::endl;
#endif // VALUE
}
Run Code Online (Sandbox Code Playgroud)

c++ c-preprocessor

27
推荐指数
3
解决办法
808
查看次数

std::list 是循环的吗?

std::list 是一个循环双向链表吗?使用它我可以执行以下操作:

#include <list>
int main(){
    std::list<int> l = {10, 11, 12};
    std::list<int>::iterator it = l.end();
    it++;
    std::cout << *it << std::endl; // prints 10
}
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
2
解决办法
219
查看次数

静态分配对象的值初始化

我有这样的课:

class Car {
    int x;
public:
    Car() {cout << "Init car" << endl;}
    Car(const Car & c) { cout << "Copy car" << endl;}
    Car(const Car && c) { cout << "Move car" << endl;}
};
Run Code Online (Sandbox Code Playgroud)

当我想对 class 的对象进行值初始化时Car

Car c = Car();
Run Code Online (Sandbox Code Playgroud)

仅调用默认构造函数。为什么没有调用复制构造函数或移动构造函数,因为存在赋值?

c++ initialization

4
推荐指数
1
解决办法
41
查看次数

临时对象的复制构造函数调用

我有以下代码:

#include <iostream>
using namespace std;
struct A
{
    A() {}
    A(const A&)  { cout << "copy const" << endl; }
    A(A&)  { cout << "copy non const" << endl; }
};
A f(A a)
{
    return a;
}
int main() {
  A a1 = f(A());
}
Run Code Online (Sandbox Code Playgroud)

A(A&)拷贝构造函数被调用。为什么A(const A&)不调用,因为我传递了一个临时对象?当我注释掉A(const A&)复制构造函数时,程序不会编译。

c++

4
推荐指数
1
解决办法
81
查看次数

算术操作数类型转换

为什么在下面的代码中:

short a = 4;
char b = 2;
cout << sizeof(a/b);
Run Code Online (Sandbox Code Playgroud)

sizeof(a/b) 是 4?为什么不是 2 作为短的尺寸?

c++

4
推荐指数
1
解决办法
67
查看次数

map 是否将元素存储为 std::pair?

std::map 是否将元素存储为std::pair?迭代地图看起来像这样:

#include <map>
int main() {
    std::map<int, char> m;
    m[1] = 'A';
    m[2] = 'B';
    m[3] = 'C';

    std::map<int, char>::iterator it;
    for(it = m.begin(); it != m.end(); it++)
    {
       std::cout << "Key: "    << it->first;
       std::cout << " Value: " << it->second;
       std::cout << std::endl;
    } 
}
Run Code Online (Sandbox Code Playgroud)

c++ internals stdmap std-pair

4
推荐指数
1
解决办法
518
查看次数

非 POD 的零初始化

为什么在下面的非 POD 类中 x 被初始化为零?

class test {
public:
    void print() {
        cout << x << endl;
    }
private:
    int x;
};

int main(int argc, char** argv)
{
    test * tst = new test();
    tst->print();
    cout << is_pod<test>::value << endl;
}
Run Code Online (Sandbox Code Playgroud)

tst->print() 和 is_pod() 都返回 0

c++

3
推荐指数
1
解决办法
121
查看次数

在不同的编译器上包含文件

将要

#include "filename"
Run Code Online (Sandbox Code Playgroud)

如果在当前目录中找不到或某些编译器可能找不到,则始终在标准包含目录中搜索文件?

是否可以使用此指令关闭在标准包含目录中搜索文件,例如 gcc?

c++

3
推荐指数
1
解决办法
48
查看次数

运算符继承

我有以下代码:

class Rectangle
{
protected:
    int a, b;
public:
    Rectangle(int a, int b) : a(a), b(b) {}
    int area() { return a*b; }
    Rectangle operator+(const Rectangle & other)
    {
        Rectangle temp(0, 0);
        temp.a = a + other.a;
        temp.b = b + other.b;
        return temp;
    }
    void operator=(const Rectangle & other)
    {
        a = other.a;
        b = other.b;
    }
};

class Square : public Rectangle
{
public:
    Square(int a) : Rectangle(a, a) {}
};

int main()
{
    Square s1(3);
    Square s2(1); …
Run Code Online (Sandbox Code Playgroud)

c++

3
推荐指数
1
解决办法
86
查看次数

用于泛型和元编程的 C++11 特性

我们可以这样说auto并且decltype是泛型编程的一部分吗?

我们可以说这constexpr是元编程的一个特性吗?

c++

-1
推荐指数
1
解决办法
59
查看次数

标签 统计

c++ ×10

c-preprocessor ×1

initialization ×1

internals ×1

std-pair ×1

stdmap ×1