是否可以在通用方法中使用其内部构造函数构造对象?
public abstract class FooBase { }
public class Foo : FooBase {
internal Foo() { }
}
public static class FooFactory {
public static TFooResult CreateFoo<TFooResult>()
where TFooResult : FooBase, new() {
return new TFooResult();
}
}
Run Code Online (Sandbox Code Playgroud)
FooFactory与驻留在同一程序集中Foo。类调用工厂方法,如下所示:
var foo = FooFactory.CreateFoo<Foo>();
Run Code Online (Sandbox Code Playgroud)
他们得到编译时错误:
“ Foo”必须是具有公共无参数构造函数的非抽象类型,才能在通用类型或方法“ FooFactory.CreateFoo()”中用作参数“ TFooType”
有什么办法可以解决这个问题?
我也尝试过:
Activator.CreateInstance<TFooResult>();
Run Code Online (Sandbox Code Playgroud)
这会在运行时引发相同的错误。
这些工作:
struct WithString {
WithString(std::string){};
};
void takeString(std::string){}
//implicit conversions:
takeString("hello");
WithString("hello");
Run Code Online (Sandbox Code Playgroud)
但这不是:
WithString makeWithString() { return "hello";}
// error: no viable conversion from returned value of type 'const char [6]'...
Run Code Online (Sandbox Code Playgroud)
如果std::string在前两种情况下隐式转换为"hello" ,为什么不能在最后一种情况下呢?请注意,我没有将WithString构造函数指定为explicit,所以我希望这样的转换.
通过这样做,我可以让行为起作用:
struct WithString {
WithString(std::string){};
WithString(const char *){};
};
Run Code Online (Sandbox Code Playgroud)
我只是对这种奇怪感到好奇.如果我假定一个猜测,我会说这是因为前两个工作的情况下,转换之间const char *到std::string,但在错误的情况下,这反而会需要2转换链,首先从const char *到std::string,然后从std::string到WithString.也许这就是原因,但我不确定.
c++ stdstring object-construction implicit-conversion char-pointer
以下雪花查询返回 JSON 结构,但输出按键排序。如何不按键排序但保留顺序?是否有什么参数需要设置?
select
object_construct
(
'entity', 'XYZ',
'allowed', 'Yes',
'currency', 'USD',
'statement_month','July, 2020'
)
Run Code Online (Sandbox Code Playgroud)
输出:--按键排序
{
"allowed": "Yes",
"currency": "USD",
"entity": "XYZ",
"statement_month": "July, 2020"
}
Run Code Online (Sandbox Code Playgroud)
预期输出:--与指定的顺序相同
{
"entity": "XYZ",
"allowed": "Yes",
"currency": "USD",
"statement_month": "July, 2020"
}
Run Code Online (Sandbox Code Playgroud) 在 C# 中,可以用相当简洁的语法构造对象树:
var button = new Button() { Content = "Foo" };
Run Code Online (Sandbox Code Playgroud)
在 F# 中是否有一种惯用的方法来执行类似的操作?
记录有很好的语法:
let button = { Content = "Foo" }
Run Code Online (Sandbox Code Playgroud)
据我所知,对象构造似乎是另一回事。通常我会编写如下代码:
let button = new Button()
button.Content <- "Foo"
Run Code Online (Sandbox Code Playgroud)
甚至:
let button =
let x = new Button()
x.Content <- "Foo"
x
Run Code Online (Sandbox Code Playgroud)
解决该问题的一种方法是使用自定义的流畅组合运算符:
// Helper that makes fluent-style possible
let inline (.&) (value : 'T) (init: 'T -> unit) : 'T =
init value
value
let button = new Button() .& (fun x -> x.Content …Run Code Online (Sandbox Code Playgroud) 这是我想做的事情(可能不是最好的事情)能够调用一些类构造函数,它接收一个指向正在调用的类的指针(ufff !!!).代码看起来更好,就像我在C#中所做的那样.
public class SomeClass
{
SomeOtherClass someOtherClass;
//Constructor
public SomeClass(SomeOtherClass someOtherClass)
{
this->someOtherClass = someOtherClass;
}
}
public class SomeOtherClass
{
public SomeOtherMethod()
{
SomeClass c = new SomeClass(this);
}
}
Run Code Online (Sandbox Code Playgroud)
那么,如何在c ++中实现相同的结果呢?Thanx提前.
问题与标题中的一样.
例如:
QPropertyAnimation *animation;
animation = new QPropertyAnimation(this, "windowOpacity", this);
Run Code Online (Sandbox Code Playgroud)
要么
QPropertyAnimation animation;
animation.setTargetObject(this);
animation.setPropertyName("windowOpacity");
animation.setParent(this);
Run Code Online (Sandbox Code Playgroud)
哪个更有效率?
编辑:虽然它没有显着差异,除非反复进行,我仍然想知道,我宁愿想要答案而不是意见 - 如stackoverflow的指导建议.
我刚刚遇到了一些 C++ 容器实现。该类使用内部缓冲区来管理其对象。这是一个没有安全检查的简化版本:
template <typename E> class Container
{
public:
Container() : buffer(new E[100]), size(0) {}
~Container() { delete [] buffer; }
void Add() { buffer[size] = E(); size++; }
void Remove() { size--; buffer[size].~E(); }
private:
E* buffer;
int size;
};
Run Code Online (Sandbox Code Playgroud)
据我所知,如果/没有自定义,这将E冗余地构造/销毁对象。这看起来很危险。Container()~Container()newdelete
使用放置是否是防止危险的冗余构造函数/析构函数调用的最佳new方式Add()(除了将类绑定到功能齐全的池之外)?
使用 Placement 时new,new char[sizeof(E)*100]分配缓冲区的正确方法是吗?
c++ memory-management placement-new object-construction object-destruction
我试图理解C++ 中的放置新表达式。
\n这个 Stack Overflow 答案指出,这T* p = new T(arg);相当于
void* place = operator new(sizeof(T)); // storage allocation\nT* p = new(place) T(arg); // object construction\nRun Code Online (Sandbox Code Playgroud)\n这delete p;相当于
p->~T(); // object destruction\noperator delete(p); // storage deallocation\nRun Code Online (Sandbox Code Playgroud)\n为什么我们需要在对象构造中放置 new 表达式T* p = new(place) T(arg);,\xe2\x80\x99t 与以下等价吗?
T* p = (T*) place;\n*p = T(arg);\nRun Code Online (Sandbox Code Playgroud)\n c++ placement-new dynamic-memory-allocation object-construction
这是一个设计问题.我正在尝试在两个实现之间做出决定.
为了正确解释这个,我需要一个例子.因此,为了举例:
我有一个班级可以生成一份报告,比如某个特定日子的某些股票市场价值.我创建了一个StockMarketReportGenerator对象,将其传递给今天的日期,并根据当前的市场价值生成报告.
在StockMarketReportGenerator"有" StockMarketData的对象.该StockMarketData对象的目的是包含存储在数据库中的表(可能称为StockMarket :))中的所有股票市场值,以及根据表数据计算的一些其他值.它具有与数据库连接的私有方法,检索数据,进行必要的计算,并将最终值存储在对象的成员变量中.(然后它有getter方法来公开这些值,但没有setter方法.)StockMarketData该类基本上是股票市场数据值的"持有者".我有一个称为" calculateStockMarketData()"的中心函数,它调用所有这些私有帮助器方法并设置对象.(我知道所有这些处理都可以通过Hibernate这样的框架更容易处理;但是决定手动执行它,因为它是一个非常小的,有点临时的项目,不值得设置.)
我的问题是这个 - 从我的ReportGenerator班级,我只需要StockMarketData对象来访问它的属性/成员变量 - 后处理和后期计算.这意味着,我真的希望得到预先填充数据的对象.所以我将calculateStockMarketData方法保密,并从StockMarketData
构造函数中自动调用它.但是我对在构造函数中进行所有处理然后没有任何公共方法感到有些不安.这是一个设计缺陷吗?或者这是最合乎逻辑的方式吗?基本上,以下哪两个实现更好?
1)(我当前的实现)将中心calculateStockMarketData()方法设为私有并从StockMarketData方法构造函数(通过今天的日期)调用它,这样无论什么时候有一个StockMarketData对象,它都已填充.因此,ReportGenerator在开始使用对象属性之前,我需要的所有内容都是行:
StockMarketData myData = new StockMarketData(someDate);
Run Code Online (Sandbox Code Playgroud)
2)使中心calculateStockMarketData()方法公开,以便为了设置StockMarketData对象,您需要显式调用该方法.所以从ReportGenerator课上我会编码:
StockMarketData myData = new StockMarketData(someDate);
myData.calculateStockMarketData();
Run Code Online (Sandbox Code Playgroud)
第一个对我来说是更好的设计,特别是因为在初始化之前不可能使用对象属性.但是我也不确定从构造函数执行大量代码的标准......我应该选择哪个?
假设我正在创建一个体育游戏,在这个游戏中,玩家可以玩各种位置,攻击,防御等。所以我从创建一个基类开始:
public abstract class Position
{
public abstract string Name
{
get;
}
}
Run Code Online (Sandbox Code Playgroud)
和子类...
public class Defender : Position
{
public override string Name
{
get { return "Defender"; }
}
}
Run Code Online (Sandbox Code Playgroud)
等等。这一切都很好。
但是现在我需要一个函数来创建这些对象。我需要一个按位置创建的函数。因此,一种可能的解决方案是创建所有位置的枚举并将此值传递给一个函数,该函数打开枚举并返回适当的对象。但这会触发我的代码气味警报。这个灵魂将类、枚举和函数内的开关联系在一起:
public static Position GetByType(Types position)
{
switch(position)
{
case Types.Defender:
return new Defender();
... and further terrible code
Run Code Online (Sandbox Code Playgroud)
我应该考虑什么解决方案?这是哪种设计模式?
c++ ×5
c# ×2
char-pointer ×1
constructor ×1
f# ×1
generics ×1
java ×1
json ×1
setter ×1
snowflake-cloud-data-platform ×1
stdstring ×1
syntax ×1
this-pointer ×1