请参阅下面的main()和两个非常简单的类.然后根据Boost序列化(以及显示的内容),我的问题是:
1)B类是否需要定义正常的重载流插入运算符'<<'和'>>'?目前在我的真实代码中它没有这些.
2)store()和load()方法中的类A是否需要明确地迭代map和multimap容器,明确地存储/加载它们的key:value对?例如:
void A::store(const char* filename){
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
std::map< std::string, B >::iterator it;
BMap.size();
oa << BMap.size();
for( it = BMap.begin(); it != BMap.end(); it++ ){
oa << it->first;
oa << it->second;
}
//similar for strMultimap
}
Run Code Online (Sandbox Code Playgroud)
我认为我不需要这样做,但我不确定.
3)假设B类只显示了两个数据成员,是否需要明确包含默认的构造函数?(与隐式默认构造函数相对)
4)B是否需要为比较运算符'>'覆盖?我认为它不是因为这是一个非常简单的类.
最后,对于我未能涵盖的任何内容的任何其他评论表示赞赏!
上述问题的示例代码:
//includes ommitted
int main() {
std::string file("test.dat");
A * pA = new A;
pA->store(file.c_str());
pA->fillMaps();
//release data
pA->load(file.c_str());
return 0;
}
//includes ommitted
class A
{
friend class boost::serialization::access;
public:
std::map< std::string, B > BMap; …Run Code Online (Sandbox Code Playgroud) 我有这个程序
#include <iostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std ;
#if 0
namespace skg
{
template <class T>
struct Triplet ;
}
template <class T>
ostream& operator<< (ostream& os, const skg::Triplet<T>& p_t) ;
#endif
namespace skg
{
template <class T>
struct Triplet
{
// friend ostream& ::operator<< <> (ostream& os, const Triplet<T>& p_t) ;
private:
T x, y, z ;
public:
Triplet (const T& p_x, const T& p_y, const T& p_z)
: x(p_x), y(p_y), z(p_z) …Run Code Online (Sandbox Code Playgroud) 我有:
class Foo;
class Bar {
Foo foo;
Bar(): foo(foo) {};
}
Bar bar;
Run Code Online (Sandbox Code Playgroud)
在这一点上,是
bar.foo // <--- how is this initialized?
Run Code Online (Sandbox Code Playgroud)
[这个问题来自一个错误的重新计数指针实现; 我本可以发誓我确保每个指针都指向非空的东西; 但我最终得到了一个指向NULL的指针.
我目前正在执行以下操作,编译器(MSVC2008 /以及2010)没有抱怨它,但我不确定这是不是一个坏主意:
#ifndef FOO_H_
#define FOO_H_
// note, FOO_H_ is not a comment:
#endif FOO_H_
Run Code Online (Sandbox Code Playgroud)
我以前总是写它,#endif // FOO_H_但我发现自己今天不这样做,并认为它很奇怪,因为显然我暂时没有做评论方法.
这是不好的做法,我应该通过我的所有标题和修复(它是一个跨平台的应用程序)或者是否可以保持它的方式?
有时,我觉得方法超载可能会造成混乱.
class a {
public:
void copy(float f);
void copy(double d);
};
Run Code Online (Sandbox Code Playgroud)
a me;
me.copy(1.2); // Not obvious at the first sight on which version we are calling.
Run Code Online (Sandbox Code Playgroud)
对此的解决方法是.
class a {
public:
void copyFloat(float f);
void copyDouble(double d);
};
Run Code Online (Sandbox Code Playgroud)
但是,拥有不同名称的方法,执行相同的功能似乎也不是一个好主意.我可以知道,您考虑什么,在方法重载或具有不同命名的方法中进行选择?
这里的gdb用户对使用模板和STL调试代码的能力有何看法?
你是否使用任何技巧使调试变得更简单?也许是一些Python脚本?或者你对gdb目前的方式感到满意(版本6.x,还没试过7.x)?
谢谢.
我正在使用Microsoft Visual Studio 2010.当我向项目添加文件时,它们有一个.cpp扩展名.要使用C,我必须手动将文件重命名为.c.
有没有办法直接添加C文件,而无需重命名任何东西?
我最近偶然发现了这个C++/Lua错误
int function_for_lua( lua_State* L )
{
std::string s("Trouble coming!");
/* ... */
return luaL_error(L,"something went wrong");
}
Run Code Online (Sandbox Code Playgroud)
错误是luaL_error使用longjmp,因此堆栈永远不会解开并且s永远不会被破坏,泄漏内存.还有一些Lua API无法解开堆栈.
一个显而易见的解决方案是在C++模式下编译Lua,但有例外.然而,我不能像Luabind那样需要标准的C ABI.
我目前的想法是编写我自己的函数,模仿Lua API的麻烦部分:
// just a heads up this is valid c++. It's called a function try/catch.
int function_for_lua( lua_State* L )
try
{
/* code that may throw Lua_error */
}
catch( Lua_error& e )
{
luaL_error(L,e.what());
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:function_for_lua堆栈是否正确解开.可能会出错吗?
为什么这个左值调用不明确?我可以有AA和AA&编译器将知道使用AA&.但是,当我添加第三个选项时,我得到一个错误.显然AA &&是一个更好的重载然后其他像int这样的int更好然后很长.为什么这个含糊不清?有没有办法我可以保持所有3个重载并明确我想要哪一个?(Typecasting (AA&&)不会这样做).
struct AA{
void*this_;
AA() { this_=this; }
//not valid, use AA&, AA(AA a){ this_=this; }
AA(AA&a){ this_=this; }
AA(AA&&a){ this_=a.this_; }
};
void movetest(AA s) {}
void movetest(AA& s) {}
//This gets me the ambiguous error void movetest(AA&& s) {}
AA&& movetest() { return AA(); }
void MyTestCode2(){
AA a;
AA b(a);
AA c = movetest();
movetest(AA());
}
Run Code Online (Sandbox Code Playgroud) c++ ×9
templates ×2
ambiguous ×1
boost ×1
c ×1
c++11 ×1
constructor ×1
debugging ×1
destructor ×1
friend ×1
gdb ×1
linux ×1
lua ×1
map ×1
oop ×1
outputstream ×1
overloading ×1
rvalue ×1
sorting ×1
stl ×1