让我举几个例子:想象一下,你有一个可以有子窗口的窗口类.每个子窗口都有一个指向其父窗口的弱指针,每个窗口都有一个与其子窗口共享的ptrs列表.现在我的情况是,如果子窗口被破坏,我不知道它是否被破坏,因为父窗口已关闭,或者因为子窗口本身已关闭.因此,我提出的解决方案是在try {} catch {}块中取消引用子窗口析构函数中的父窗口弱指针,以查看父窗口是否仍然存在(伪代码):
//we try to retain the parent window here
try
{
//creates a shared pointer from the weak pointer, throws BadReferenceCounterException on fail
WindowPtr parent = m_parentWindow.retain();
//check if there is a parent at all, otherwise 0
if(parent)
{
parent->removeChildWindow(this);
}
}
catch(const BadReferenceCounterException & _ec)
{
std::cout<<"Catched expected exception, if Parent was closed before me!"<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我想不出另一种方法来解决这个问题,因为无法知道调用析构函数的上下文.这是一个愚蠢的事情,因为我在某种预期的情况下使用异常?我知道这不是你想要在性能关键代码中做的事情,所以这不是问题的关键.
谢谢!
我正在使用CoreText CTFontGetGlyphsForCharacters获取与Unicode字符对应的字形,即UniChar。现在,我想检索连字字符,例如fi。有什么方法可以从CoreText中检索连字形(例如,通过应合并的一系列单字符)?
谢谢,摩卡
我想更改const函数参数的成员.想象我正在处理的纹理类的这个片段:
template<>
void mySuperTexture::loadPixels<float>(uint32 _width, uint32 _height, float * _pixels, const Settings & _settings)
{
if (_settings.pixelDataType == PixelDataType::Auto) {
_settings.pixelDataType = PixelDataType::Float32;
// ^ obviously this does not work on the const variable
}
// ...
m_pimpl->loadPixels(_width, _height, _pixels, _settings);
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以将参数const设为非,但是这不允许我为settings参数指定默认参数,或者使用temp变量调用该函数.
我想我可以struct在loadPixels函数中复制一下Settings ,但我想避免这种情况.还有其他想法吗?
编辑:
好吧,我决定使用mutable关键字.-这就是为什么:在我的场景中,Settings结构基本上只在构造纹理/加载像素时使用,之后不存储.-因此,mutable不会改变任何关于用户的常量,因为它根本无法访问.重载似乎是一种痛苦在我situtation,因为我实现多种类型(如无符号的字符,短整型,整型......),因此只是超载会增加源代码的量loadPixels方法很多.顺便说一句.在实际工作中设置得到保存一个内部PIMPL对象(其loadPixel功能没有模板,因此不知道类型的任何东西)这就是为什么我想直接在常量设置结构的工作.
我目前正在研究 IPv6 类,并使用 inet_pton 从字符串中检索 IP 的实际二进制表示形式,即:
AdressV6::AdressV6(const String & _ip)
{
int result = inet_pton(AF_INET6, _ip.c_str(), &(m_nativeAdress));
if(result <= 0)
//throw...
//How can I retrieve the sope ID from that?
}
Run Code Online (Sandbox Code Playgroud)
有一个通用的方法可以做到这一点吗?您是否只是手动解析字符串并查找听起来不太安全的“%”:(
谢谢你!
我暂时尝试了手动解析,这似乎有效。不过,如果有更好的方法请告诉我:
//retrieve scope ID
uint32 scopeId = 0;
size_t pos = _ip.find("%");
if(pos != String::npos)
{
String theId = _ip.substr(pos+1);
scopeId = atoi(theId.c_str());
}
m_scopeId = scopeId;
Run Code Online (Sandbox Code Playgroud)