我是新手在C++ 11中移动语义,我不太清楚如何处理unique_ptr
构造函数或函数中的参数.考虑这个引用自身的类:
#include <memory>
class Base
{
public:
typedef unique_ptr<Base> UPtr;
Base(){}
Base(Base::UPtr n):next(std::move(n)){}
virtual ~Base(){}
void setNext(Base::UPtr n)
{
next = std::move(n);
}
protected :
Base::UPtr next;
};
Run Code Online (Sandbox Code Playgroud)
这是我应该如何编写unique_ptr
参数的函数?
我需要std::move
在调用代码中使用吗?
Base::UPtr b1;
Base::UPtr b2(new Base());
b1->setNext(b2); //should I write b1->setNext(std::move(b2)); instead?
Run Code Online (Sandbox Code Playgroud) 我用C++和Java编写了一些状态机,但从未使用像Ocaml这样的函数式语言
问题是我不知道我是否可以只调整对象语言版本的代码,因为在Ocaml中,记录和变体比类更强大;
所以,我需要一个事件驱动的有限状态机(像UML一样分层),可以轻松配置
在该领域有经验的人可以发布一个简单的样本吗?只是为了避免最常见的陷阱
谢谢 :)
编辑 16/03:没有可变状态可以做到吗?我想以"FSM"的名称正确封装它,我应该选择一个模块还是一个类?
在gcc 4.5.1(Ubuntu 10.04,intel core2duo 3.0 Ghz)下考虑这个代码. 这只是2个测试,在第一个我直接调用虚拟fucnion,在第二个我通过Wrapper类调用它:
TEST.CPP
#define ITER 100000000
class Print{
public:
typedef Print* Ptr;
virtual void print(int p1, float p2, float p3, float p4){/*DOES NOTHING */}
};
class PrintWrapper
{
public:
typedef PrintWrapper* Ptr;
PrintWrapper(Print::Ptr print, int p1, float p2, float p3, float p4) :
m_print(print), _p1(p1),_p2(p2),_p3(p3),_p4(p4){}
~PrintWrapper(){}
void execute()
{
m_print->print(_p1,_p2,_p3,_p4);
}
private:
Print::Ptr m_print;
int _p1;
float _p2,_p3,_p4;
};
Print::Ptr p = new Print();
PrintWrapper::Ptr pw = new PrintWrapper(p, 1, 2.f,3.0f,4.0f);
void test1() …
Run Code Online (Sandbox Code Playgroud) 我的意思是这个模块:选项
我找不到它,open Option
给我Error: Unbound module Option
并且没有'option.cma'文件
它在标准库中吗?它被命名为'option.cma'吗?
假设我有一个模块M
参数化的模块F
:
module M (F : sig type id type data end) =
struct
type idtype = F.id
type datatype = F.data
type component = { id : idtype; data : datatype }
let create id data = { id; data }
let get_comp_data comp = comp.data
let get_comp_id comp = comp.id
end
Run Code Online (Sandbox Code Playgroud)
所以我这样使用它:
module F1 = struct type id = int type data = float end
module MF1 = M(F1)
let comp = MF1.create 2 5.0 …
Run Code Online (Sandbox Code Playgroud) 假设我们有一个基类及其两个派生类; 基类拥有一个方法execute,每个派生类使用不同类型和数量的参数实现此方法的不同版本; 我不能使用虚方法,因为签名应该对每个派生类完全相同; 我的目标是提供一个基本执行方法,它接受任何类型的参数,减去它们的类型,并将它们分派到右派生类中的正确方法; 我看了一下Visitor模式,但我正在寻找一个更灵活,更优雅的解决方案;
编辑:我想将这些类存储在一个向量中,所以我需要一个基类
这是我的尝试(我不知道在基本执行的主体中放入什么)在gcc 4.5下:
class Base {
public:
Base();
~Base();
template<typename ...Args>
void execute(Args... arg)
{
//calls the right method
//execute(int i) or execute(int i, float f)
//as Args are int or int and float
}
};
class DerivedA : public Base
{
public:
DerivedA();
~DerivedA();
void execute(int i){ /*do something with i*/}
};
class DerivedB : public Base
{
public:
DerivedB();
~DerivedB();
void execute(int i, float f){/*do something with i and f*/} …
Run Code Online (Sandbox Code Playgroud) 我需要构建一个调用共享对象的Ocaml/C++模块(linux下的.so)
只要编译一个简单的Ocaml/C++存根是一个问题,我管理的东西,但是当我需要将.so与ocamlmklib或ocamlopt链接时,它会失败
我在gcc 4.5(c ++ 0x)下工作
共享对象的文件:
hello.hpp
#include <iostream>
#include <string>
using namespace std;
class HelloApplication
{
public :
HelloApplication();
~HelloApplication();
void say(string s);
};
typedef HelloApplication *(*create_hello)();
Run Code Online (Sandbox Code Playgroud)
hello.cpp:
#include "hello.hpp"
HelloApplication::HelloApplication(){}
HelloApplication::~HelloApplication(){}
void HelloApplication::say(string s)
{
cout << "Hello : " << s << endl;
}
extern "C"
{
HelloApplication *create()
{
return new HelloApplication();
}
}
Run Code Online (Sandbox Code Playgroud)
编译事物的CMake.txt文件:
cmake_minimum_required(VERSION 2.6)
project(testHello_proj)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Release" FORCE)
#set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Debug" FORCE)
set(LIBRARY_OUTPUT_PATH lib/${CMAKE_BUILD_TYPE})
## Compiler flags …
Run Code Online (Sandbox Code Playgroud) 让这个类型=
type intC = int;;
type boolC = bool;
type stringC = string;;
type component = A of intC | B of boolC | C of stringC;;
Run Code Online (Sandbox Code Playgroud)
如果我想在组件A的类型a上应用函数,我是否需要系统地解构组件?
例如,我必须这样做:
let add comp =
match comp with
| A i -> Some (i + 2) (*only A interests me, I return i + 2*)
| _ -> None (*otherwise I return nothing*)
Run Code Online (Sandbox Code Playgroud)
然后对组件A的任何功能?有什么意思可以避免你的冗余吗?
可能的重复:
在构造函数内调用虚函数
我有一个 Shape 类及其子类 Sphere :
//Shape :
class Shape
{
public:
Shape(const string& name);
virtual ~Shape();
virtual string getName();
protected:
string mName;
};
Shape::Shape(const string& name) : mName(name)
{
/*Some stuff proper to Shape*/
/*Some stuff proper to subclass (sphere)*/
/*Some stuff proper to Shape*/
}
Shape::~Shape(){}
string Shape::getName(){ return mName; }
//Sphere :
class Sphere : public Shape
{
public:
Sphere(const string& name, const float radius);
virtual ~Sphere();
virtual string getRadius();
protected:
float mRadius;
}
Sphere::Sphere(const string& …
Run Code Online (Sandbox Code Playgroud) 让两种变体类型:
type typeA =
| A1
| A2
;;
type typeB =
| B1 of typeA
| B2 of typeA
;;
Run Code Online (Sandbox Code Playgroud)
和类型检查功能:
let isA1 = function A1 -> true | _ -> false;;
let isA2 = function A2 -> true | _ -> false;;
let isB1 = function B1 e -> true | _ -> false;;
let isB2 = function B2 e -> true | _ -> false;;
Run Code Online (Sandbox Code Playgroud)
我想创建一个列表来检查A或B类型的元素
因为他们是不同类型,我需要多态变体,我得到:
type filterA =
{
handleA : typeA -> bool;
};; …
Run Code Online (Sandbox Code Playgroud) ocaml ×6
c++ ×5
c++11 ×3
virtual ×3
module ×2
arguments ×1
constructor ×1
fsm ×1
function ×1
functor ×1
interop ×1
linux ×1
list ×1
overriding ×1
performance ×1
polymorphism ×1
state ×1
unique-ptr ×1
variant ×1