遍布boost.org以及网络上的其他网站我看过这种形式的代码:
class whatever
{
...
private:
std::vector<std::string> m_name; // exposition only
};
Run Code Online (Sandbox Code Playgroud)
"只有博览会"是什么意思?评论的目的是什么?它试图告诉我什么?
感觉就像我走到了尽头.如果我理解它,那么如果我遵循Demeter法则我永远不会创建一个返回对象的方法然后客户端代码调用它.我只是想着工厂模式总是返回一个对象.是的,有一些返回对象的映射器类.收藏怎么样?
我做了一个constexpr字符串类型,我打电话给StaticString.我从这个网站得到了这个想法.
我有一些奇怪的问题,编译器将变量视为constexpr一行,然后不是constexpr下一行.
这是代码:
constexpr StaticString hello = "hello";
constexpr StaticString hello2 = hello + " ";
constexpr StaticString world = "world";
constexpr StaticString both = hello + " world";
constexpr StaticString both2 = hello2 + world;
//This works fine (world is constexpr?)
//constexpr StaticString both3 = "hello " + world;
//ERROR: "world" is not constexpr
int main(void)
{
static_assert(hello[4] == 'o' ,"ERROR");
static_assert(hello == "hello", "ERROR");
static_assert(both2 == "hello world", …Run Code Online (Sandbox Code Playgroud) 我有一个X类,我在这里提供了一个片段:
class X {
public:
template <typename Iter>
X(Iter begin, Iter end) : mVec(begin, end) {}
private:
vector<Y> const mVec;
};
Run Code Online (Sandbox Code Playgroud)
我现在想为这个类添加一个新的连接构造函数,如:
template <typename Iter1, typename Iter2>
X(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2) : mVec(???) { ??? }
Run Code Online (Sandbox Code Playgroud)
这样的构造函数会将两个范围[begin1,end1]和[begin2,end2]连接到mVec中.挑战是
1)我想在mVec上保留const,因此在X的其他方法中它被认为是常量.
2)如果可能的话,我想避免不必要的副本.也就是说,一种解决方案是使用静态方法将非const临时构造到范围1,插入范围2并返回它,然后将连接构造函数定义为
template <typename Iter1, typename Iter2>
X(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2)
: mVec(concatenate(begin1, end1, begin2, end2)) { }
Run Code Online (Sandbox Code Playgroud)
但我认为,这至少会复制一次所有的价值.
大约六年前,一位名叫Harri Porten的软件工程师写了这篇文章,问了一个问题,"成员函数什么时候应该有const限定符?什么时候不应该?" 我发现这是我能找到的关于这个问题的最好的写作,我最近一直在努力解决这个问题,而且我认为在大多数关于const正确性的讨论中,这个问题都没有得到很好的解释.由于当时不存在像SO这样强大的软件信息共享站点,我想在这里重新提出这个问题.
我有一个涉及建模状态机的问题.
我设法做了一点知识工程和"逆向工程"一组原始确定性规则,确定状态和状态转换.
我想知道最佳做法是什么:
如何严格测试我的状态和状态转换,以确保系统不会最终处于未确定状态.
如何强制执行状态转换要求(例如,应该不可能直接从stateFoo转到StateFooBar,即向每个状态灌输关于它可以转换到的状态的'知识'.
理想情况下,我想尽可能使用基于模式的干净设计和模板.
我确实需要一个地方开始,我会感激任何指针(没有双关语意),这是我发送的方式.
一位朋友告诉我,这样做效率更高
int addNumbers(const int number1, const int number2);
Run Code Online (Sandbox Code Playgroud)
比
int addNumbers(int number1, int number2);
Run Code Online (Sandbox Code Playgroud)
当然假设number1并且number2不会分配新值.这是否会带来显着的性能提升?我应该知道其他任何副作用吗?
有没有人知道任何有关高性能应用中设计模式的最佳实践或理论的网站/书籍/文章?似乎很多模式使用间接/抽象/封装的方式可能会影响计算密集型代码的性能.Head First Design Patterns甚至GoF提到了许多模式的性能可能性,但没有更具体的建议如何处理它.
当我们需要使用Factory模式以及何时使用Singleton模式时,请帮助我理解Factory模式和Singleton模式.
一个优于另一个的主要优点/缺点是什么?
任何建议(解释)都会对我有所帮助.
为了设计我的代码,我正在绘制一些 UML 类图。我有一些共享对象,我想知道应该如何绘制这些对象,因为这些对象的所有权确实是共享的。更具体地说,这里是正在发生的事情的 C++ 示例:
class A
{
public:
A(){
std::shared_ptr<CSharedObj> sharedObj = std::make_shared<CSharedObj>;
mB = B(sharedObj);
}
private:
B mB;
};
class B
{
public:
B(std::shared_ptr<CSharedObj>);
private:
std::shared_ptr<CSharedObj> mSharedObj;
};
class CSharedObj
{
public:
CSharedObj();
};
Run Code Online (Sandbox Code Playgroud)
我如何在类图中表示这 3 个类之间的关系?
c++ ×7
performance ×2
const ×1
constexpr ×1
constructor ×1
iterator ×1
object ×1
oop ×1
php ×1
shared-ptr ×1
state ×1
stl ×1
terminology ×1
uml ×1