相关疑难解决方法(0)

我必须在何处以及为何要使用"模板"和"typename"关键字?

在模板,在那里,为什么我必须把typenametemplate上依赖的名字呢?究竟什么是依赖名称?我有以下代码:

template <typename T, typename Tail> // Tail will be a UnionNode too.
struct UnionNode : public Tail {
    // ...
    template<typename U> struct inUnion {
        // Q: where to add typename/template here?
        typedef Tail::inUnion<U> dummy; 
    };
    template< > struct inUnion<T> {
    };
};
template <typename T> // For the last node Tn.
struct UnionNode<T, void> {
    // ...
    template<typename U> struct inUnion {
        char fail[ -2 + (sizeof(U)%2) ]; // Cannot be instantiated for any …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++-faq dependent-name typename

1061
推荐指数
8
解决办法
15万
查看次数

这个typedef语句是什么意思?

在C++参考页面中,他们提供了一些typedef示例,我试图理解它们的含义.

// simple typedef
typedef unsigned long mylong;


// more complicated typedef
typedef int int_t, *intp_t, (&fp)(int, mylong), arr_t[10];
Run Code Online (Sandbox Code Playgroud)

所以我理解的是简单的typedef(第一个声明).

但是他们用第二个宣告什么(下面重复)?

typedef int int_t, *intp_t, (&fp)(int, ulong), arr_t[10];
Run Code Online (Sandbox Code Playgroud)

特别是什么(&fp)(int, mylong)意思?

c++ typedef

76
推荐指数
3
解决办法
3734
查看次数

复制具有多个参数的构造函数

我正在学习C++,正在从中读取复制构造函数C++: The Complete Reference.书上写着

允许复制构造函数具有其他参数,只要它们具有为其定义的默认参数即可.但是,在所有情况下,第一个参数必须是对执行初始化的对象的引用.

但我很困惑,我们将如何通过这些额外的参数?我相信应该有一些方法没有在书中给出,我无法弄清楚.谁能帮我吗?

编辑: 也可以在所有三种情况下传递这些额外的参数,即

  • 当一个对象显式初始化另一个对象时,例如在声明中
  • 当使对象的副本传递给函数时
  • 生成临时对象时(最常见的是作为返回值)

c++ copy-constructor

9
推荐指数
1
解决办法
2502
查看次数

初始化由类的构造函数内的向量组成的矩阵

我正在尝试构建一个具有字符矩阵的游戏.我正在尝试使用向量向量来构建我的矩阵.我game.h有这个:

#ifndef GAME_H
#define GAME_H
// includes
using namespace std;
class Game 
{
  private:
    int row;
    int col;
    vector<vector<char>>* matrix;
    // other atributtes

  public:
    Game();
    ~Game(){}
    // some functions
};
#endif
Run Code Online (Sandbox Code Playgroud)

在我的game.cpp:

Game::Game()
{
    this->col = 20;
    this->row = 20;
    // Initialize the matrix
    this->matrix = new vector<vector<char>>(this->col);
    for(int i = 0 ; i < this->col ; i++)
       this->matrix[i].resize(this->row, vector<char>(row));
    // Set all positions to be white spaces
    for(int i = 0 ; i <  this->col; …
Run Code Online (Sandbox Code Playgroud)

c++ class stdvector

6
推荐指数
1
解决办法
314
查看次数