小编mil*_*aki的帖子

fgetpos()行为取决于换行符

考虑这两个文件:

file1.txt(Windows换行符)

abc\r\n
def\r\n
Run Code Online (Sandbox Code Playgroud)

file2.txt(Unix换行)

abc\n
def\n
Run Code Online (Sandbox Code Playgroud)

我注意到对于file2.txt,获得的位置fgetpos没有正确递增.我在Windows上工作.

让我举个例子.以下代码:

#include<cstdio>

void read(FILE *file)
{
    int c = fgetc(file);
    printf("%c (%d)\n", (char)c, c);

    fpos_t pos;
    fgetpos(file, &pos); // save the position
    c = fgetc(file);
    printf("%c (%d)\n", (char)c, c);

    fsetpos(file, &pos); // restore the position - should point to previous
    c = fgetc(file);     // character, which is not the case for file2.txt
    printf("%c (%d)\n", (char)c, c);
    c = fgetc(file);
    printf("%c (%d)\n", (char)c, c);
}

int main()
{
    FILE *file = …
Run Code Online (Sandbox Code Playgroud)

c windows newline file

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

尽管初始化,模板类的静态成员 std::function 仍为空

考虑下面的类:

template <class T>
struct Test
{
    Test() {
        if (!f) {
            f = []() { std::cout << "it works\n"; };
            initialized = true;
        }
    }
    static void check() {
        if (f) f();
        else std::cout << "f is empty\n";
    }
    T x{};
    inline static std::function<void()> f;
    inline static bool initialized = false;
};
Run Code Online (Sandbox Code Playgroud)

此类的对象,如果全局声明(是的,我知道使用全局变量是一种不好的做法f),则在初始化后似乎会清空该函数。以下示例演示了这一点:

Test<double> t;

int main()
{
    t.x = 1.5;
    std::cout << "t.x = " << t.x << "\n";
    std::cout << std::boolalpha << "initalized: " …
Run Code Online (Sandbox Code Playgroud)

c++ lambda templates std std-function

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

如何使一个类只能访问另一个类的某些私有成员?

假设我们有两个类:

class Base
{
private:
    int x;
public:
    void f();
};

class Foo
{
    // some variables and methods
};
Run Code Online (Sandbox Code Playgroud)

现在每个人都可以打电话Base::f(),但我只Foo希望能够这样做.

为了达到这个效果,我们可以Base::f()私密并宣布Foo为朋友:

class Base
{
private:
    int x;
    void f();
    friend Foo;
};
Run Code Online (Sandbox Code Playgroud)

这种方法的问题是,Foo有同时访问Base::f()Base::x(甚至任何其他私有成员Base).但我想Foo只能访问Base::f().

有没有办法让类(或函数)只授予另一个类的某些私有成员访问权限?或者也许任何人都可以建议更好地解决我的问题?

编辑:

我将尝试指定我需要的访问限制.首先,Base是库中的接口(事实上它是一个抽象类).用户仅使用派生自的类Base.Base::f()只调用Foo库中的另一个类.隐藏Base::f()用户很重要,因为只Foo知道何时调用它.同时,Foo不应该搞乱其他成员Base.

c++ class friend

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

std :: sort在std:指针向量上失败

以下代码在对矢量进行排序时崩溃.

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

struct Foo
{
    int x;
    // int y;
    Foo() : x(0) {}
};

struct Cmp
{
    bool operator() (Foo* p1, Foo *p2) const
    {
        if (p1->x != p2->x) return p1->x < p2->x;
        // if (p1->y != p2->y) return p1->y < p2->y;
        return true;
    }
};

int main()
{
    vector<Foo*> v;
    for (int i=0; i<17; i++) // weird thing, doesn't crash if
                             // I put a number less than 17 !!!
    {
        Foo *ptr …
Run Code Online (Sandbox Code Playgroud)

c++ sorting stl vector stl-algorithm

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

标签 统计

c++ ×3

c ×1

class ×1

file ×1

friend ×1

lambda ×1

newline ×1

sorting ×1

std ×1

std-function ×1

stl ×1

stl-algorithm ×1

templates ×1

vector ×1

windows ×1