小编hlx*_*007的帖子

c ++ Singleton by reference use

我已经阅读了 StackOverflow 中的一些 wiki,并为我的 Randomizer 类编写了以下头文件:

class Randomizer
{
    public:
        static Randomizer& instance(void);
        int nextInt(int);
        int nextInt(int, int);
        int die(int);
        double nextDouble(void);
        char randomChar(const std::string&);
    private:
        Randomizer(void) {};
        /* No implementation of the following methods */
        Randomizer(Randomizer const&);
        void operator= (Randomizer const&);
};
Run Code Online (Sandbox Code Playgroud)

我还在类内部实现了一些方法,例如 nextInt 等。

我不确定如何创建这个 Singleton 类的实例,即如何在 main() 中编写测试驱动器?

我试过:

int main()
{
     Randomizer r;
     r = Randomizer::instance();
}
Run Code Online (Sandbox Code Playgroud)

编译器说几个错误:

In file included from Randomizer.cpp:11:0:
Randomizer.h: In function ‘int main(int, char**)’:
Randomizer.h:22:9: error: ‘Randomizer::Randomizer()’ is private
         Randomizer(void) {}; …
Run Code Online (Sandbox Code Playgroud)

c++ singleton

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

为什么我们需要stdin作为输入函数的文件版本?

在C中,stdin是一个有效的文件指针,因此如果我们需要或者需要,我们可以使用stdin(以及另外两个)和输入函数的"file"版本.

我们为什么需要(而不仅仅是从shell中输入)?有人能提出一些例子吗?

c

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

C++:我的新节点在哪里?

我试图使用二叉树在集合中添加元素:

bool TreeSet::add(const string &str)
{
    if (treesize == 0)
    {
        TreeNode->data = str;
        treesize++;
        return true;
    }
    else
    {
        if (str < TreeNode->data)
            return insert(TreeNode->left, str);
        else if (str > TreeNode->data)
            return insert(TreeNode->right, str);
        else
            return false;
    }
    return false;
}

bool TreeSet::insert(TREE *node, const string &str) //private
{
    if (node == NULL)
    {
        node = new TREE;
        node->data=str;
        node->left = NULL;
        node->right = NULL;
        treesize++;
        return true;
    }
    else
    {
        if (str < node->data)
            return insert(node->left, str);
        else …
Run Code Online (Sandbox Code Playgroud)

c++ binary-tree set

0
推荐指数
1
解决办法
69
查看次数

如何在C中确定i +++ - j

在C中,

int i = 20;
int j = 5;
int k = i+++--j;
Run Code Online (Sandbox Code Playgroud)

为什么k = 24?

根据我的理解,k =(i)++ +( - j)所以它是(20 + 4)++ = 25.

好.这是我为测试编写的一个小程序,是的,在分配k后完成后增量.

#include <stdio.h>
int main()
{
    int i = 20;
    int k = i++;
    printf("%d\n", k);
    printf("%d\n", i);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

20
21
Run Code Online (Sandbox Code Playgroud)

有谁能告诉我为什么投票?我不确定这是因为我是C的新成员.

c

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

标签 统计

c ×2

c++ ×2

binary-tree ×1

set ×1

singleton ×1