小编Ste*_*ski的帖子

受保护的成员与重载运算符冲突

我有以下课程:

class Base {
protected:
    int myint;        
};

class Derived : public Base {
public:
    bool operator==(Base &obj) {
        if(myint == obj.myint)
            return true;
        else
            return false;
    }
};
Run Code Online (Sandbox Code Playgroud)

但是当我编译它时,它会出现以下错误:

int Base::myint 在这种情况下受到保护

我认为受保护的变量可以在公共继承下从派生类访问.导致此错误的原因是什么?

c++ inheritance class protected

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

访问struct成员时valgrind的读/写错误无效

我试图在C中实现一个纯粹作为练习的链表.我有这样的结构:

typedef struct node {
    int data;
    struct node* next;
} 
node;

typedef struct list {
    size_t size;
    node* head;
}
list;
Run Code Online (Sandbox Code Playgroud)

现在,valgrind抱怨的功能是这些:

创建()

list* create() {
    // alocate memory for a new list
    list* list = malloc(sizeof(list));

    if (list != NULL) {
        list->head = NULL; // this is line 65
        list->size = 0;
    }

    // return pointer to the allocated memory
    return list;
}
Run Code Online (Sandbox Code Playgroud)

插入()

void insert(int data, list* list) {
    if (list == NULL)
        return;

    // allocate memory …
Run Code Online (Sandbox Code Playgroud)

c memory struct valgrind

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

标签 统计

c ×1

c++ ×1

class ×1

inheritance ×1

memory ×1

protected ×1

struct ×1

valgrind ×1