为什么以下代码会异常引发异常(在createObjects调用中map::at),代码(及其输出)可以在这里查看
有趣的是,如果注释行与microsoft和gcc编译器一起取消注释(参见此处),代码将按预期工作,这甚至可以将initMap用作普通静态变量而不是静态getter.
我可以想到的唯一原因是静态registerHelper_ object(factory_helper_)和std::mapobject(initMap)的初始化顺序是错误的,但我不知道如何发生这种情况,因为map对象是在第一次使用时构建的,那就是在factory_helper_构造函数中,所以一切都应该没问题呢?我更为惊讶的是那些doNothing()行修复了这个问题,因为无论如何都会在关键部分(当前失败)之后调用doNothing().
编辑:调试显示,没有调用factory_helper_.doNothing(),从不调用factory_helper_的构造函数.
#include <iostream>
#include <string>
#include <map>
#define FACTORY_CLASS(classtype) \
extern const char classtype##_name_[] = #classtype; \
class classtype : FactoryBase<classtype,classtype##_name_>
namespace detail_
{
class registerHelperBase
{
public:
registerHelperBase(){}
protected:
static std::map<std::string, void * (*)(void)>& getInitMap() {
static std::map<std::string, void * (*)(void)>* initMap = 0;
if(!initMap)
initMap= new std::map<std::string, void * (*)(void)>();
return *initMap;
}
};
template<class TParent, const char* ClassName>
class registerHelper_ …Run Code Online (Sandbox Code Playgroud) 我需要在C++中实现工厂类,但是当我考虑这个时,我发现了一个我无法解决的大问题,并且我发现,所有工厂实现示例都以同样的方式存在缺陷.我可能是那个错了,但请告诉我原因.
所以这里是简单的"典型"工厂实现,它允许我在不更改Factory类的情况下注册新对象.
//fruit.h
class Fruit
{
protected :
int count;
public :
Fruit(int count) : count(count) {}
virtual void show() = 0;
};
// factory.h
/** singleton factory */
class Factory
{
typedef Fruit* (*FruitCreateFunction)(int);
static Factory* factory;
std::map<std::string, FruitCreateFunction> registeredFruits;
public :
static Factory& instance()
{
if (factory == NULL)
factory = new Factory();
return *factory;
}
bool registerFruit(const std::string& name, Fruit* (createFunction)(int))
{
registeredFruits.insert(std::make_pair(name, createFunction));
return true;
}
Fruit* createFruit(const std::string& name, int count)
{
return registeredFruits[name](count);
} …Run Code Online (Sandbox Code Playgroud) 我正在实现一个看起来如下的工厂模式.
public class FeedFactory
{
#region Singleton Pattern
//..
#endregion
private static Feed[] _factory = new Feed[(int)FeedType.Total];
public void RegisterFeed(FeedType feedType,Feed feed)
{
if (_factory[(int)feedType] == null)
{
_factory[(int)feedType] = feed;
}
else
{
// already registered
}
}
public Feed GetFeed(FeedType feedType)
{
return _factory[(int)feedType];
}
}
Run Code Online (Sandbox Code Playgroud)
这Feed是一个抽象类,不同的类从中继承.如何注册不同的课程?是否有可能从他们的构造函数中做到这一点?
我需要一个对象B,但是当我执行'B.GetByID()'时我得到一个对象A.
public class A
{
public A()
{
}
public static A GetSelf()
{
return new A();
}
public static A GetByID()
{
return GetSelf();
}
}
public class B extends A
{
public B()
{
super();
}
public static B GetSelf()
{
return new B();
}
}
B.GetByID(); //returns A, i need B
Run Code Online (Sandbox Code Playgroud) 假设我有一个工厂bean:
<bean id="myFactory" class="com.company.MyFactory" lazy-init="true">
<property name="myProperty" ref="propA">
</bean>
Run Code Online (Sandbox Code Playgroud)
假设propA是由工厂方法中使用的IOC注入的bean.我从这个工厂生成了2个bean:
<bean id="bean1" factory-bean="myFactory" factory-method="instance"/>
<bean id="bean2" factory-bean="myFactory" factory-method="instance"/>
Run Code Online (Sandbox Code Playgroud)
如何在不使用其他工厂方法的情况下使bean2使用与bean1不同的myProperty?或者,如何将propA作为参数传递给bean1或bean2配置中的factory-method?
我试图通过Angular服务将遗留的javascript函数中的消息广播到Angular控制器中.在Angular中调用时,广播工作正常,但不在外部.见http://jsfiddle.net/yh3Ds/24/
从工厂返回指针的最佳方法是什么?它应该是一个std::unique_ptr或者std::shared_ptr只是一个原始指针?
另外,有人告诉我,std::unique_ptr如果有遏制,std::shared_ptr如果有聚集,应该去.这是正确的方法吗?
在工厂函数中,我有时只想返回一个新创建的空关联数组.
一种方法是:
auto make_dict()
{ int[char] dict; return dict; }
Run Code Online (Sandbox Code Playgroud)
有没有办法避免声明局部变量dict?有点像
auto make_dict()
{ return int[char]; }
Run Code Online (Sandbox Code Playgroud)
要么,
auto make_dict()
{ return int[char](); }
Run Code Online (Sandbox Code Playgroud)
要么,
auto make_dict()
{ return new int[char]; }
Run Code Online (Sandbox Code Playgroud)
由于与需要如何声明关联数组相关的原因,这些都不起作用.有办法吗?
app.service('situacao', function($log, $q, $http, $rootScope){
var situacao = this;
situacao.lista = {};
situacao.getAllSituacao = function(){
var defer = $q.defer();
console.log("php/getAll.php");
$http.get($rootScope.endPoint + "php/getAll.php")
.success(function(res) {
console.log(res);
situacao.lista = res;
defer.resolve(res);
}).error(function(err, status){
defer.reject(err);
});
return defer.promise;
};
return situacao;});
app.controller('listCtrl',['$scope', '$uibModal', '$log', '$http', function(situacao, $scope, $modal, $log, $http) {
$scope.init = function(){
$scope.getAll();
}
$scope.getAll = function(){
situacao.getAllSituacao().then(function(res){
//sucess
$scope.dispSituacao = situacao.lista;
}, function(err){
//error
})
};
$scope.init(); }]);
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用"服务"但导致错误:situacao.getAllSituacao不是一个函数.Nãainconsigo utilizar o service semper mostra a mensagem de erroquenãoéumamação
怎么了?
我跑到命令之下
php artisan make:factory AddressFactory
我的结果低于结果
$factory->define(Model::class, function (Faker $faker) {
但我想得到如下结果
$factory->define(App\Address::class, function (Faker $faker) {
我该怎么办 ?