我在C++中创建一个简单的线程服务器应用程序,事实是,我使用libconfig ++来解析我的配置文件.好吧,libconfig不支持多线程,因此我使用两个包装类来完成"支持".点是,其中一个失败:
class app_config {
friend class app_config_lock;
public:
app_config(char *file) :
cfg(new libconfig::Config()),
mutex(new boost::mutex())
{
cfg->readFile(file);
}
private:
boost::shared_ptr<libconfig::Config> cfg;
boost::shared_ptr<boost::mutex> mutex;
};
Run Code Online (Sandbox Code Playgroud)
从我的main.cpp文件调用时失败可怕:
app_main::app_main(int c, char **v) : argc(c), argv(v) {
// here need code to parse arguments and pass configuration file!.
try {
config = app_config("mscs.cfg");
} catch (libconfig::ParseException &e) {
cout << "Parse error at line " << e.getLine() << ": " << e.getError() << endl;
throw;
} catch (libconfig::FileIOException &e) {
cout << "Configuration …Run Code Online (Sandbox Code Playgroud) c++ construction exception-handling member-variables function-try-block
这段代码有效吗?
int foo()
{
std::vector<std::string>& v = std::vector<std::string>(5, "X");
// Do something silly...
return 42;
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我认为临时std::vector对象(从赋值标记开始)应该在构造之后被破坏(从而使引用无效).
然而,调试证明我错了,而且,我意识到我不太明白为什么在函数返回时会破坏临时变量.
我想我对一些基本的东西有很强的误解,所以请赐教:)
我正在使用以下(简化的)工厂设计来创建一些继承层次结构的对象,不应该是任何特殊的:
// class to create
class Class
{
public:
Class(Type type, Foo foo);
};
// Simple creator class.
// Used in practice to do some runtime checks about whether or not construction is allowed.
class Creator
{
public:
Class* create( Type type, Foo foo ) const
{
return new Class( type, foo );
}
};
class Factory
{
public:
Factory
{
// fill object creator map on construction
_map[ "name" ] = new Creator<Class>;
}
Class* create( const std::string& name, …Run Code Online (Sandbox Code Playgroud) 我经常Empty向C++对象添加一个方法,以使用类似于以下的代码清除内部状态.
class Foo
{
private:
int n_;
std::string str_;
public:
Foo() : n_(1234), str_("Hello, world!")
{
}
void Empty()
{
*this = Foo();
}
};
Run Code Online (Sandbox Code Playgroud)
这似乎比在构造函数中复制代码更好,但我想知道*this = Foo()在想要清除对象时是否是一种常见的方法?这有什么问题等着咬我的背面?有没有其他更好的方法来实现这种事情?
我有一对看起来像这样的课程;
public abstract class Class1 {
//...
public Class1() {
//...
function2();
//...
}
protected abstract void function2();
}
public class Class2 implements Class1 {
private final OnSomethingListener mOnSomethingListener = new OnSomethingListener() {
@Override
onSomething() {
doThatOtherThing();
}
}
protected void function2() {
//uses mOnSomethingListener
//however mOnSomethingListener is null when this function is called from super()
//...
}
public Class2() {
super();
}
}
Run Code Online (Sandbox Code Playgroud)
我假设监听器是null因为我有效地引用它super()并且它还没有实例化.但是,我想成功,final因为它确实如此.我可以让这个字段(监听器)及时初始化而不将它放在超类中(它不会使用监听器)吗?
我有一组点需要构造为一组三角形面以形成实体网格。我看过Delaunay三角剖分,但都没有道理。关于从哪里开始的任何建议?我没有处理复杂的形状,它们最多包含200个顶点。
注意:这些点在x,y,z空间中。
给出以下代码
import std.datetime: Clock, SysTime, Duration;
SysTime[] times;
const n = 3;
foreach (i; 0..n) times ~= Clock.currTime;
Run Code Online (Sandbox Code Playgroud)
是否有一个更简单,可能是功能更高的模式来实现相同的目标?
在可能的情况下,奖励将通过一些就地构造模式最小化元素的复制.
另见:http://forum.dlang.org/thread/yofbijaejfyftpcjdcvd@forum.dlang.org#post-yofbijaejfyftpcjdcvd:40forum.dlang.org
更新:
好的,这是我到目前为止的尝试:
enum arityMin0(alias fun) = __traits(compiles, fun());
auto apply(alias fun, N)(N n) if (isCallable!fun &&
arityMin0!fun &&
!is(ReturnType!fun == void) &&
isIntegral!N)
{
import std.range: iota, map;
return n.iota.map!(n => fun);
}
Run Code Online (Sandbox Code Playgroud)
例如,被称为
import std.datetime: Clock;
auto times = 3.apply!(Clock.currTime).array;
Run Code Online (Sandbox Code Playgroud)
剩下一个细节.限制
arity!fun == 0
Run Code Online (Sandbox Code Playgroud)
评估为false中
auto times = 3.apply!(Clock.currTime).array;
Run Code Online (Sandbox Code Playgroud)
因为arity在这里实际上是0和1. …
使用oracle java 1.8.0_25
我有以下结构
URL url = new URL(new URL(new URL("http://localhost:4567/"), "123"), "asd")
Run Code Online (Sandbox Code Playgroud)
根据https://docs.oracle.com/javase/tutorial/networking/urls/creatingUrls.html中的文档,
它应该生成http://localhost:4567/123/asd
但是它产生的URLhttp://localhost:4567/asd
文件说明
此代码段使用URL构造函数,该构造函数允许您从另一个URL对象(基础)和相对URL规范创建URL对象.这个构造函数的一般形式是:
URL(URL baseURL, String relativeURL)
第一个参数是一个URL对象,它指定新URL的基础.第二个参数是一个String,它指定相对于base的资源名称的其余部分.如果baseURL为null,则此构造函数将relativeURL视为绝对URL规范.相反,如果relativeURL是绝对URL规范,则构造函数会忽略baseURL.
这是正确的行为吗?
我目前正在c#中构建一个DHCPMessage类.
RFC可在此处获取:http://www.faqs.org/rfcs/rfc2131.html
伪
public object DHCPMessage
{
bool[8] op;
bool[8] htype;
bool[8] hlen;
bool[8] hops;
bool[32] xid;
bool[16] secs;
bool[16] flags;
bool[32] ciaddr;
bool[32] yiaddr;
bool[32] siaddr;
bool[32] giaddr;
bool[128] chaddr;
bool[512] sname;
bool[1024] file;
bool[] options;
}
Run Code Online (Sandbox Code Playgroud)
如果我们想象每个字段都是固定长度的位数组,那么:
将此表示为一个类的方式???
或者..你会怎么写这个?:)
在C++中,我引用了一个想要指向其所有者的对象,但是我无法在包含类的构造期间设置指针,因为它没有完成构造.所以我想尝试做这样的事情:
class A {
public:
A() : b(this) {}
private:
B b;
};
class B {
public:
B(A* _a) : a(_a) {}
private:
A* a;
};
Run Code Online (Sandbox Code Playgroud)
有没有办法确保B总是用A*初始化而没有A拿着指向B的指针?
谢谢
在很多C++ API(基于COM的那些)中为你创造了一些东西,指向构造对象的指针通常需要作为**指针(并且函数将为你构造和初始化)
您通常会看到以下签名:
HRESULT createAnObject( int howbig, Object **objectYouWantMeToInitialize ) ;
Run Code Online (Sandbox Code Playgroud)
- 但您很少看到新对象作为返回值传递.
除了想要查看错误代码的人,这是什么原因?是否更好地使用**模式而不是返回的指针来实现更简单的操作,例如:
wchar_t* getUnicode( const char* src ) ;
Run Code Online (Sandbox Code Playgroud)
或者这更好地写成:
void getUnicode( const char* src, wchar_t** dst ) ;
Run Code Online (Sandbox Code Playgroud)
我能想到的最重要的事情就是记住释放它,并且由于**某种原因,这种方式往往会提醒我,我也必须解除它.
我有3个类,如ClassVersion1,ClassVersion2和ClassVariables.ClassVariables适用于Form中其他类的到达变量.
我认为是这些;
if(version == 1)
{
ClassVersion1 clss = new ClassVersion1();
}
else
{
ClassVersion2 clss = new ClassVersion2();
}
clss.vars.variable1 = 3;
clss.vars.variable2=5;
clss.DoSomething();
Run Code Online (Sandbox Code Playgroud)
但是我必须将函数和变量调用到if条件中(两个具有相同名称,不同类的对象).我想将对象创建为条件并使用条件.
我怎样才能做到这一点?