根据最新的C标准,在没有参数的情况下定义函数的正确方法是什么:int main()或int main(void)?
如果目的地和来源相同,memmove仍然"移动"数据(或直接返回)?那怎么样realloc; 如果新尺寸与旧尺寸相同怎么办?
当调用使用varargs的函数时,内部会发生什么?参数本身是否像任何其他参数一样存储在堆上或堆栈中.如果在堆栈中,它是如何工作的?
在C你有"%c"和"%f"的格式标志printf-和scanf式的功能.这两个功能的使用可变长度参数...,它总是转换floats到doubles和chars到ints.
我的问题是,如果这种转换发生,为什么做独立的标志char和float存在吗?为什么不只使用相同的标志int和double?
我正在为我的事件系统创建一个自定义RTTI系统.以下是EventTypeInfo课程.如您所见,它是不可复制的,就像std::type_info.
class EventTypeInfo
{
public:
EventTypeInfo(const EventTypeInfo&) = delete;
EventTypeInfo& operator=(const EventTypeInfo&) = delete;
inline bool operator==(const EventTypeInfo& other) const {
return this == &other;
}
};
Run Code Online (Sandbox Code Playgroud)
我为每个事件类创建这些对象的方式归结为:
template<class EventClass>
const EventTypeInfo& event::type::info()
{
static EventTypeInfo typeinfo;
return typeinfo;
}
Run Code Online (Sandbox Code Playgroud)
鉴于(1)这些对象是静态创建的(这意味着它们将在应用程序的整个持续时间内持续),(2)它们是不可复制的,并且(3)没有办法在EventTypeInfo不诉诸的情况下修改字段const_cast,是它足以让我实现operator==是条款this == &other,还是我错过了什么?
我有一个Event用C++定义的类,我使用Boost向Python公开.我的脚本应该派生自这个类,并且我想在定义新的子类时进行一些初始化.
如何设置公开Event类的元类,以便每当Python脚本派生自此类时,元类都可以执行所需的初始化?
我想避免在脚本中明确使用元类...
class KeyboardEvent(Event): # This is what I want
pass
class KeyboardEvent(Event, metaclass=EventMeta): # This is not a good solution
pass
Run Code Online (Sandbox Code Playgroud)
编辑: 解决方案的一部分
似乎没有办法用Boost.Python设置元类.接下来最好的事情是在定义类之后即兴创建和更改元类.在本机Python中,更改元类的安全方法是执行此操作:
B = MetaClass(B.__name__, B.__bases__, B.__dict__)
Run Code Online (Sandbox Code Playgroud)
在Boost中,它看起来像这样:
BOOST_PYTHON_MODULE(event)
{
using namespace boost::python;
using boost::python::objects::add_to_namespace;
class_<EventMetaClass> eventmeta("__EventMetaClass")
...;
class_<Event> event("Event")
...;
add_to_namespace(scope(), "Event",
eventmeta(event["__name__"], event["__bases__"], event["__dict__"]));
}
Run Code Online (Sandbox Code Playgroud)
问题是我似乎找不到使用Boost.Python定义元类的方法,这就是为什么我打开了如何使用Boost.Python定义Python元类的原因?.
当我使用从C继承的函数时,就像在<cmath>or中那样<cstdlib>,我应该将它们限定为标准命名空间的一部分std::log,还是应该保留在C-scope中并将它们用作全局函数?怎么样size_t?
我正在尝试使用宏在C中创建一个类型安全的通用链表.它应该与模板在C++中的工作方式类似.例如,
LIST(int) *list = LIST_CREATE(int);
Run Code Online (Sandbox Code Playgroud)
我的第一次尝试是#define LIST(TYPE)(我上面使用的宏)定义一个struct _List_##TYPE {...}.但是,这不起作用,因为每次我声明一个新列表时都会重新定义结构.我通过这样做解决了这个问题:
/* You would first have to use this macro, which will define
the `struct _List_##TYPE`... */
DEFINE_LIST(int);
int main(void)
{
/* ... And this macro would just be an alias for the struct, it
wouldn't actually define it. */
LIST(int) *list = LIST_CREATE(int);
return 0;
}
/* This is how the macros look like */
#define DEFINE_LIST(TYPE) \
struct _List_##TYPE \
{ \
... \
}
#define …Run Code Online (Sandbox Code Playgroud) c ×7
c++ ×3
boost-python ×1
containers ×1
idioms ×1
if-statement ×1
macros ×1
memcmp ×1
memmove ×1
metaclass ×1
namespaces ×1
operators ×1
printf ×1
python ×1
realloc ×1
std ×1