我正在尝试使用 C# 从我的程序中打开 CorelDRAW。到目前为止,我已经能够通过引用适当的 com 库并调用来做到这一点
CorelDRAW.Application draw = new CorelDRAW.Application();
draw.Visible = true;
Run Code Online (Sandbox Code Playgroud)
但是,我希望我的程序能够与任何支持互操作的 CorelDRAW 版本一起使用。我尝试使用反射在运行时加载互操作库,可以为正确的版本选择特定的 dll。通过环顾四周,我尝试了以下方法。
string path = "Interop.CorelDRAW.dll";
Assembly u = Assembly.LoadFile(path);
Type testType = u.GetType("CorelDRAW.Application");
if (testType != null)
{
object draw = u.CreateInstance("CorelDRAW.Application");
FieldInfo fi = testType.GetField("Visible");
fi.SetValue(draw, true);
}
Run Code Online (Sandbox Code Playgroud)
该程序失败了,u.CreateInstance...因为它CorelDRAW.Application是一个接口,而不是一个类。我还尝试替换CorelDRAW.Application为,CorelDRAW.ApplicationClass因为当我浏览 Interop.CorelDRAW 作为资源时可用,但随后u.getType...失败。
我怎样才能让它发挥作用?谢谢你!
这是我对一些C++ 11研究示例的实现.我让所有构造函数和析构函数打印到控制台.但令人惊讶的是,我得到了两次构造函数但是析构函数三次.
看起来意想不到的是0x7fff5fbff6d0.创建此对象时?但为什么没有关联的构造函数调用?
为什么会这样?
template<typename T>
class ArrayWrapper{
public:
ArrayWrapper():data_(nullptr), size_(0){
cout << "Default ctor called "<< this <<endl;
}
ArrayWrapper(size_t n, const T& val) : data_(new T[n]), size_(n){
cout << "ctor_n_val called "<< this << endl;
for_each(data_, data_+size_, [&](T& elem){ elem=val; });
}
ArrayWrapper(const ArrayWrapper& other): data_(new T[other.size_]), size_(other.size_)
{
cout << "copy ctor called "<< this <<endl;
copy(other.data_, other.data_+other.size_, data_);
}
ArrayWrapper(ArrayWrapper&& other): data_(other.data_), size_(other.size_)
{
cout << "move ctor called"<<endl;
other.data_ = nullptr;
other.size_ = 0;
} …Run Code Online (Sandbox Code Playgroud) 我需要在我的班级中有一个共享计数器(当计数器变为零时调用某个函数).我可以使用shared_ptr<char>带有删除器的那个,但是这种方法有分配不需要的char并且保持指针的开销.
基本上,我需要引用计数的一部分shared_ptr.我不知道如何利用shared_ptr并避免这种开销.
std共享计数器是否有可移植的C++ 11实现(即,使用标准的c ++ 11,只有标准的互斥锁等)?
PS.计数器不是整个班级独有的.我可能有我班级的对象a1,a2,a3共享同一个计数器.与b1,b2,b3共享不同的计数器.因此,当a1,a2,a3中的最后一个超出范围时(与a1,a2,a3相关)应该发生.当b1,b2,b3中的最后一个超出范围时,应该发生某些事情(与b1,b2,b3相关).
谢谢
例如,有一种方法:
void foo( A* p )
{
auto l = [=](){ /* Do Something using p */ };
// Use l ...
}
Run Code Online (Sandbox Code Playgroud)
我应该如何捕获指针:通过引用或值?里面的lambda p没有改变,只是使用了它的方法.
我在 C++ 中使用多重集,我相信它存储一个元素及其插入时的相应计数。
在这里,当我想删除一个元素时,我只想将集合中该元素的计数减1,直到它大于0。
C++ 代码示例:
multiset<int>mset;
mset.insert(2);
mset.insert(2);
printf("%d ",mset.count(2)); //this returns 2
// here I need an O(1) constant time function (in-built or whatever )
// to decrease the count of 2 in the set without deleting it
// Remember constant time only
-> Function and its specifications
printf("%d ",mset.count(2)); // it should print 1 now .
Run Code Online (Sandbox Code Playgroud)
有没有什么方法可以实现这一点,或者我应该删除它并按所需的(count-1)次插入元素 2 ?
在处理C++ 11类型集时,我尝试实现此功能(剥离到最小):
constexpr auto test() -> bool;
template <typename T, typename... Rest>
constexpr auto test() -> decltype(test<Rest...>())
{
return {};
}
Run Code Online (Sandbox Code Playgroud)
gcc和clang都对此感到窒息.Clang说:
test.cpp:54:40: error: 'Rest' does not refer to a value
constexpr auto test() -> decltype(test<Rest...>())
^
Run Code Online (Sandbox Code Playgroud)
gcc抱怨:
test.cpp:54:44: error: expected primary-expression before ‘...’ token
constexpr auto test() -> decltype(test<Rest...>())
Run Code Online (Sandbox Code Playgroud)
我想这是因为在decltype查看它时,甚至没有完全声明测试的可变版本.
但是,当我在C++ 14中使用返回类型推导时,这编译得很好:
constexpr auto test() -> bool;
template <typename T, typename... Rest>
constexpr auto test()
{
return test<Rest...>();
}
Run Code Online (Sandbox Code Playgroud)
似乎test在这里被认为是充分宣布的.
我想知道为什么这对decltype变体不起作用?即使我打开C++ 14支持?
PS:事实证明,我甚至无法调用C++ …
这个问题已在C++ 98上下文中提出,并在该上下文中得到了回答,但没有明确说明C++ 11
const some_type& create_const_thingy()
{
lock my_lock(some_mutex);
static const some_type the_const_thingy;
return the_const_thingy;
}
void use_const_thingy()
{
static const some_type& the_const_thingy = create_const_thingy();
// use the_const_thingy
}
Run Code Online (Sandbox Code Playgroud)
这种初始化模式是否可以确保:
create_const_thingy 只被召唤一次提前致谢!
#include <random>
int f() {
std::random_device seeder;
std::mt19937 engine(seeder());
std::uniform_int_distribution<int> dist(1, 6);
return dist(engine);
}
Run Code Online (Sandbox Code Playgroud)
多个线程可以安全地调用此函数吗?功能线程安全吗?这是reduntant调用std::random_device seeder;和std::mt19937 engine(seeder());每一次?
假设我说这个代码:
template<int... Us>
struct Matrix{};
template<int... U1, int... U2>
auto compute(Matrix<U1...>, Matrix<U2...>){return 0;}
Matrix<1> a; Matrix<2,3> b;
Matrix<1,2> c; Matrix<3> d;
int main(){
compute(a,b);
compute(c,d);
auto fp = &compute<1,2,3>;
fp(a,b);
fp(c,d);
}
Run Code Online (Sandbox Code Playgroud)
两个compute()调用是否只实例化一个函数模板,即计算<1,2,3>,还是会有两个不同的实例,具体取决于参数?
我想通过获取指向特定实例的函数指针来确认这一点,看看我是否可以使用相同的函数指针调用具有2个不同参数集的函数,但是我在调用fp的行中得到以下错误(a, b):
Run Code Online (Sandbox Code Playgroud)[x86-64 gcc 8.2 #1] error: could not convert 'a' from 'Matrix<#'nontype_argument_pack' not supported by dump_expr#<expression error>>' to 'Matrix<#'nontype_argument_pack' not supported by dump_expr#<expression error>>'
虽然 C++ 标准将虚拟调度的实现留给了编译器,但目前只有 3 个主要编译器(gcc、clang 和 msvc)。
当您通过指向抽象基类的指针调用抽象基类上的方法时,它们如何实现虚拟调度?构建期间如何设置 vtable?
一个简单的“好像”示例会很有用。
c++ polymorphism language-implementation vtable dynamic-dispatch
c++ ×9
c++11 ×8
c# ×1
c++14 ×1
com-interop ×1
coreldraw ×1
decltype ×1
lambda ×1
pointers ×1
polymorphism ×1
recursion ×1
reflection ×1
shared-ptr ×1
static ×1
vtable ×1