In Mixing C and C++ Code in the Same Program the following example (slightly abbreviated here to the relevant parts) is given. Assume buf.h
contains the following:
在将C和c++代码混合到同一个程序中时,给出了下面的示例(此处对相关部分略作简化)。假设缓冲区。h包含以下:
struct buf {
char* data;
unsigned count;
};
// some declarations of existing C functions for handling buf...
It is then recommended to use
然后建议使用它
extern "C" {
#include "buf.h"
}
class mybuf : public buf {
public:
mybuf() : data(0), count(0) { }
// add new methods here (e.g. wrappers for existing C functions)...
};
in order to use the struct within C++ with added features.
为了在c++中使用带有附加特性的结构体。
However, this clearly will produce the following error:
但是,这显然会产生以下错误:
error: class `mybuf' does not have any field named `data'
error: class `mybuf' does not have any field named `count'
The reasons for this are explained in How can I initialize base class member variables in derived class constructor?, C++: Initialization of inherited field, and Initialize parent's protected members with initialization list (C++).
原因是如何在派生类构造函数中初始化基类成员变量?, c++:初始化继承字段,用初始化列表(c++)初始化父级受保护成员。
Thus, I have the following two questions:
因此,我有以下两个问题:
- Is the code provided just plainly wrong or am I missing some relevant aspect? (After all, the article seems to stem from a reputable source)
- 代码是否提供了明显的错误,或者我是否遗漏了一些相关的方面?(毕竟,这篇文章的出处似乎很好)
- What is the correct way to achieve the desired effect (i.e., turning a C struct into a C++ class and adding some convenience methods like, e.g., a constructor, etc.)?
- 达到预期效果的正确方法是什么?,将C struct转换为c++类,并添加一些方便的方法,例如,构造函数等)?
Update: Using aggregation initialization as suggested, i.e.,
更新:按照建议使用聚合初始化,即,
mybuf() : buf{0, 0} {}
works, but requires C++11. I therefore add the following question:
工作,但是需要c++ 11。因此,我增加了以下问题:
-
Using C++03, is there a better way to achieve the desired outcome than using the following constructor?
使用c++ 03,是否有比使用以下构造函数更好的方法来实现所需的结果?
mybuf() { data = 0; count = 0; }
4 个解决方案
#1
8
If you can use a c++11 compatible compiler then this would be a perfect use case for an initializer list using aggregate initialization.
如果您可以使用与c++11兼容的编译器,那么对于使用聚合初始化的初始化器列表来说,这将是一个完美的用例。
mybuf() : buf{0, 0}
{}
#2
3
One "correct" way, if your compiler is C++11 capable, is to use e.g.
一个“正确”的方法,如果你的编译器是c++ 11,它是用例。
mybuf() : buf{0, 0} {}
#3
3
This has nothing to do with mixing C and C++. You're trying to initialise members that don't exist; that they exist in a base class isn't enough. You need to initialise the base itself.
这与混合C和c++没有任何关系。你试图初始化不存在的成员;它们存在于基类中是不够的。你需要初始化基地本身。
In this case, use aggregate initialisation:
在这种情况下,使用聚合初始化:
class mybuf : public buf
{
public:
mybuf() : buf{0, 0} {}
};
#4
2
class mybuf : public buf {
public:
mybuf();
// add new methods here (e.g. wrappers for existing C functions)...
};
const buf init = {0,0};
mybuf::mybuf() : buf(init) {};
will work.
将工作。
I have seen this work with some compilers, but don't have a standard handy to check if it is standard or an extension.
我已经看到了一些编译器的工作,但是没有一个标准来检查它是标准的还是扩展的。
class mybuf : public buf {
public:
mybuf() : buf(init) { }
// add new methods here (e.g. wrappers for existing C functions)...
private:
const buf init = {0,0};
};
#1
8
If you can use a c++11 compatible compiler then this would be a perfect use case for an initializer list using aggregate initialization.
如果您可以使用与c++11兼容的编译器,那么对于使用聚合初始化的初始化器列表来说,这将是一个完美的用例。
mybuf() : buf{0, 0}
{}
#2
3
One "correct" way, if your compiler is C++11 capable, is to use e.g.
一个“正确”的方法,如果你的编译器是c++ 11,它是用例。
mybuf() : buf{0, 0} {}
#3
3
This has nothing to do with mixing C and C++. You're trying to initialise members that don't exist; that they exist in a base class isn't enough. You need to initialise the base itself.
这与混合C和c++没有任何关系。你试图初始化不存在的成员;它们存在于基类中是不够的。你需要初始化基地本身。
In this case, use aggregate initialisation:
在这种情况下,使用聚合初始化:
class mybuf : public buf
{
public:
mybuf() : buf{0, 0} {}
};
#4
2
class mybuf : public buf {
public:
mybuf();
// add new methods here (e.g. wrappers for existing C functions)...
};
const buf init = {0,0};
mybuf::mybuf() : buf(init) {};
will work.
将工作。
I have seen this work with some compilers, but don't have a standard handy to check if it is standard or an extension.
我已经看到了一些编译器的工作,但是没有一个标准来检查它是标准的还是扩展的。
class mybuf : public buf {
public:
mybuf() : buf(init) { }
// add new methods here (e.g. wrappers for existing C functions)...
private:
const buf init = {0,0};
};