具体来说,我正在使用Linux命令:
$ find . -regextype posix-extended -regex '<some regex>' ...
我只是想确保我使用的POSIX类型是Perl使用的类型,因为这是迄今为止我最熟悉的类型.
所以,我使用boost :: shared_ptr来提供它提供的所有各种引用计数的好处 - 显然是对启动器的引用计数,还有复制,分配和存储在STL容器中的能力.
问题是,如果我将它传递给一个"恶意"函数或对象,该对象可以保存ptr,然后我将永远无法在没有外部函数或对象很好地放弃其所有权的情况下解除分配它.
最终,我试图保持对象所有权的明确.我通过让所有者将唯一的shared_ptr保留到对象来完成此操作,而"guest"对象仅将weak_ptrs存储到对象.
我真的不想要shared_ptr的"共享"部分,但我需要使用shared_ptr才能生成weak_ptrs.我想使用scoped_ptr,但它非常有限,因为你无法复制它.您无法将其存储在容器中,也无法从中借出weak_ptrs,也无法将所有权转移给新经理.
解决方案是什么?
为什么
class A;
template<typename T> class B
{
private:
A* a;
public:
B();
};
class A : public B<int>
{
private:
friend B<int>::B<int>();
int x;
};
template<typename T>
B<T>::B()
{
a = new A;
a->x = 5;
}
int main() { return 0; }
Run Code Online (Sandbox Code Playgroud)
造成
../src/main.cpp:15:错误:无效使用构造函数作为模板
../src/main.cpp:15:注意:使用'B :: B'而不是'B :: class B'来在限定名称中命名构造函数
尚未改变friend B<int>::B<int>()到friend B<int>::B()结果
../src/main.cpp:15:错误:没有在类'B'中声明的'void B :: B()'成员函数
完全删除模板
class A;
class B
{
private:
A* a;
public:
B();
};
class A : public B …Run Code Online (Sandbox Code Playgroud) 代码:
my $compare = List::Compare->new(\@hand, \@new_hand);
print_cards("Discarded", $compare->get_Lonly()) if ($verbose);
Run Code Online (Sandbox Code Playgroud)
print_cards期望(标量,对数组的引用).
get_Lonly返回数组.将它转换为引用的语法是什么,所以我可以将它传递给print_cards? \@{$compare->getLonly()}例如,不起作用.
谢谢!
我有一个名称空间,我使用了大量的符号,但我想覆盖其中一个:
external_library.h
namespace LottaStuff
{
class LotsOfClasses {};
class OneMoreClass {};
};
Run Code Online (Sandbox Code Playgroud)
my_file.h
using namespace LottaStuff;
namespace MyCustomizations
{
class OneMoreClass {};
};
using MyCustomizations::OneMoreClass;
Run Code Online (Sandbox Code Playgroud)
my_file.cpp
int main()
{
OneMoreClass foo; // error: reference to 'OneMoreClass' is ambiguous
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何解决"模糊"错误而无需使用"使用命名空间LottaStuff"替换千位"使用xxx"; 声明?
编辑:另外,说我不能编辑my_file.cpp,只能编辑my_file.h.因此,如下所示,用MyCustomizations :: OneMoreClass替换OneMoreClass是不可能的.
假设我有一个alpha.h文件:
class Alpha {
public:
template<typename T> void foo();
};
template<> void Alpha::foo<int>() {}
template<> void Alpha::foo<float>() {}
Run Code Online (Sandbox Code Playgroud)
如果我包括多个CPP文件alpha.h与GCC 4.4编译时,报告说有多种定义foo<int>,并foo<float>在多个目标文件.对我有意义,所以我将最后两行更改为:
template<> extern void Alpha::foo<int>() {}
template<> extern void Alpha::foo<float>() {}
Run Code Online (Sandbox Code Playgroud)
但GCC说:
显式模板专门化不能有存储类
好的......所以我该如何正确地做到这一点?我担心C++不会允许我首先尝试做的事情,在这种情况下是否有一个很好的成语可以完成同样的事情?
我有一组多态类,例如:
class Apple {};
class Red : public Apple {};
class Green : public Apple {};
Run Code Online (Sandbox Code Playgroud)
以及比较它们的免费功能:
bool operator==(const Apple&, const Apple&);
bool operator< (const Apple&, const Apple&);
Run Code Online (Sandbox Code Playgroud)
我正在设计一个可复制的包装类,它允许我在STL映射中使用类Red和Green键,同时保留它们的多态行为.
template<typename Cat>
class Copy
{
public:
Copy(const Cat& inCat) : type(inCat.clone()) {}
~Copy() { delete type; }
Cat* operator->() { return type; }
Cat& operator*() { return *type; }
private:
Copy() : type(0) {}
Cat* type;
};
Run Code Online (Sandbox Code Playgroud)
我希望这种Copy<Apples>类型尽可能地互换Apples.还有一些我必须添加到Copy上面的类中的函数,但是现在我正在使用一个自由函数operator==,如下所示:
template<typename …Run Code Online (Sandbox Code Playgroud) HI!任何人都知道如何chug(derlist);在下面的代码中使用"?" 行?
#include <iostream>
#include <list>
using namespace std;
class Base
{
public:
virtual void chug() { cout << "Base chug\n"; }
};
class Derived : public Base
{
public:
virtual void chug() { cout << "Derived chug\n"; }
void foo() { cout << "Derived foo\n"; }
};
void chug(list<Base*>& alist)
{
for (list<Base*>::iterator i = alist.begin(), z = alist.end(); i != z; ++i)
(*i)->chug();
}
int main()
{
list<Base*> baselist;
list<Derived*> derlist;
baselist.push_back(new Base);
baselist.push_back(new Base);
derlist.push_back(new …Run Code Online (Sandbox Code Playgroud) 具体来说,我想知道我应该写些:
{
shared_ptr<GuiContextMenu> subMenu = items[j].subMenu.lock();
if (subMenu)
subMenu->setVisible(false);
}
Run Code Online (Sandbox Code Playgroud)
要么:
{
if (items[j].subMenu.lock())
items[j].subMenu.lock()->setVisible(false);
}
Run Code Online (Sandbox Code Playgroud)
我不需要遵循任何风格指南.优化后,我认为这两种选择都不会对性能产生影响.什么是一般的首选风格,为什么?
编辑:项目类型[j] .subMenu是boost :: weak_ptr.lock()从中创建一个shared_ptr.上面两个版本实际上有一个模糊的区别,关于临时shared_ptr持续多长时间,所以我将我的两个例子包装在{braces}中以解决那里的歧义.
我有一个GUI架构,其中元素像这样触发事件:
guiManager->fireEvent(BUTTON_CLICKED, this);
Run Code Online (Sandbox Code Playgroud)
每个被触发的事件都将'this'传递给事件的调用者.从未有一个时间,我不希望通过"这个",而且,没有指针,除了为"本"应该永远传递.
这给我带来了一个问题:我如何断言fireEvent永远不会被赋予除'this'以外的指针,我如何简化(和均质化)对fireEvent的调用:
guiManager->fireEvent(BUTTON_CLICKED);
Run Code Online (Sandbox Code Playgroud)
在这一点上,当你写这样的东西时,我想起了一个相当常见的编译器错误:
class A {
public:
void foo() {}
};
class B {
void oops() { const A* a = new A; a->foo(); }
};
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译这将给你
在成员函数'void B :: oops()'中:错误:将'const A'作为'void A :: foo()'的'this'参数传递,丢弃限定符
因为成员函数将'this'作为隐藏参数传递.
"啊哈!" 我说.这(没有双关语)正是我想要的.如果我能以某种方式访问隐藏的'this'指针,它将解决我之前提到的两个问题.问题是,据我所知你不能(可以吗?),如果可以的话,会有一些"但它会破坏封装!" 除了我每次都已经传递'这个',所以它还能破坏它.
那么,有没有办法访问隐藏的'this',如果没有,是否有任何习惯用法或替代方法比每次传递'this'更优雅?
class Message {};
class BuildSandCastle : public Message {};
class DigHole : public Message {};
Run Code Online (Sandbox Code Playgroud)
给定一个任意Message*对象,如何在doMessage()不诉诸切换逻辑的情况下调用具有相同名称的函数,或者为每个消息名称创建一个具有"do"函数的MessageHandler类?
编辑:例如:
class Sandbox
{
public:
void play(Message* m)
{
// call doBuildSandCastle
// or doDigHole based on m's type
}
void doBuildSandCastle();
void doDigHole();
};
Run Code Online (Sandbox Code Playgroud)
对不起我以前不清楚.
编辑:
有人可以删除这个问题的火车残骸吗?我真的不希望所有这些高中课程都是多态的.
Boost.org 为fusion :: transform提供的示例如下:
struct triple
{
typedef int result_type;
int operator()(int t) const
{
return t * 3;
};
};
// ...
assert(transform(make_vector(1,2,3), triple()) == make_vector(3,6,9));
Run Code Online (Sandbox Code Playgroud)
然而,我并没有"得到它".它们的示例中的向量包含所有相同类型的元素,但使用融合的主要点是异构类型的容器.如果他们使用了make_vector(1, 'a', "howdy")怎么办?
int operator()(int t)
需要成为
template<typename T> T& operator()(T& const t)
但是我怎么写result_type?template<typename T> typedef T& result_type当然不是有效的语法,即使它是没有意义的,因为它不依赖于函数.
c++ ×10
templates ×3
boost ×2
perl ×2
boost-fusion ×1
callback ×1
constructor ×1
containers ×1
find ×1
friend ×1
gcc ×1
linux ×1
messages ×1
namespaces ×1
ownership ×1
polymorphism ×1
posix ×1
reference ×1
regex ×1
return-value ×1
shared-ptr ×1
stl ×1
styles ×1
syntax ×1
temporaries ×1
this ×1