我有一些类,C
并希望将其实例和方法的地址传递给测试函数中的某个仿函数Test_C_Foo1()
.Functor是一个模板类,我必须提供类method(MEMFN1
)的类型作为其模板参数之一.我必须在MEMFN1
某处定义类型但不想更改C.h
并且不希望用它污染全局命名空间.我决定尽可能本地化typedef,所以把它放在一个测试函数中 - 在MEMFN1
实际使用的范围内.在函数体内部使用typedef是一个很好的做法吗?
Standard允许在函数体内使用typedef,仅在这些特定情况下限制它:
除了类型说明符之外,typedef说明符不应在decl-specifier-seq中与任何其他类型的说明符组合,并且它不应在参数声明(8.3.5)的decl-specifier-seq中使用,也不得用于在函数定义(8.4)的decl-specifier-seq中.
这是代码片段:
CH:
...
#include <string>
...
class C
{
public:
int foo1(const std::string&);
};
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
...
#include "C.h"
...
void Test_C_Foo1()
{
typedef int(C::*MEMFN1)(const std::string&);
C c;
Functor1<C, MEMFN1,...> f1(&c, &C1::foo1,...);
...
}
...
int main()
{
Test_C_Foo1();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个功能
var data = {};
var myFunc = function() {
data.stuff = new ClassName().doA().doB().doC();
};
Run Code Online (Sandbox Code Playgroud)
我想测试doA
,doB
和doC
全部调用.
我试图监视像这样的实例方法
beforeEach(function() {
spyOn(ClassName, 'doA');
};
it('should call doA', function() {
myFunc();
expect(ClassName.doA).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)
但那只是给了我一个"doA()方法不存在"的错误.
有任何想法吗?
以下代码创建一个正在取消的任务.await
表达式(案例1)抛出System.OperationCanceledException
同步Wait()
(案例2)抛出System.Threading.Tasks.TaskCanceledException
(包装System.AggregateException
).
using System;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
Program.MainAsync().Wait();
}
private static async Task MainAsync()
{
using(var cancellationTokenSource = new CancellationTokenSource())
{
var token = cancellationTokenSource.Token;
const int cancelationCheckTimeout = 100;
var task = Task.Run(
async () =>
{
for (var i = 0; i < 100; i++)
{
token.ThrowIfCancellationRequested();
Console.Write(".");
await Task.Delay(cancelationCheckTimeout);
}
},
cancellationTokenSource.Token
);
var cancelationDelay = 10 * …
Run Code Online (Sandbox Code Playgroud) e.exe
链接到我的自定义静态库c.lib
,它使用在中定义的Win32 API w.dll
.w.dll
位于C:\ Windows\System32,其导入库w.lib
位于Windows SDK目录中.壳牌w.lib
被列为附加依赖于c.lib
或e.exe
项目?(e.exe
在两种情况下都能成功构建.)最佳实践是什么?为什么?我想e.exe
不应该知道w.lib
.
c.lib
仅供一组开发人员共享(不得发送给客户).
测试:我使用VS2008和dumpbin实用程序来测试这两种情况,结果如下:
w.lib
添加为附加依赖项c.lib
.dumpbin /archivemembers c.lib
输出列出项目中的偏移w.dll
和.obj文件c.lib
作为存档成员.
w.lib
不添加额外的依赖中c.lib
,但在e.exe
项目:这次,dumpbin输出只包含.obj文件,c.lib
其大小c.lib
小于Case 1
(c.lib
添加为其他依赖于w.exe
在这两种情况下的项目.)
注:我使用w.lib
,并w.dll
在这里为Windows库虚构的,通用的名字,但他们可能是如Userenv.lib和Userenv.dll或Version.lib和VERSION.DLL ...
我有一个状态模式的实现,其中每个状态处理从事件队列获取的事件.State
因此,基类具有纯虚方法void handleEvent(const Event*)
.事件继承基Event
类,但每个事件都包含可以是不同类型的数据(例如int,string ......或者其他).handleEvent
必须确定接收事件的运行时类型,然后执行向下转换以提取事件数据.事件是动态创建并存储在队列中的(因此在此处进行向上转换 ......).
我知道向下倾斜是一种糟糕设计的标志,但在这种情况下可以避免它吗?我正在考虑访问者模式,其中基类State将包含每个事件的虚拟处理程序,但是然后需要在代码片段中进行向下转换,该代码将队列中的事件从队列中取出并将其传递到当前状态.(至少在这种情况下,大switch(eventID)
只会在一个地方......).访客模式是避免向下转换的最佳方式(最佳实践)吗?
这是伪代码(我boost::shared_ptr
在这个例子中传递,但无论如何都会发生向下转换):
enum EventID
{
EVENT_1,
EVENT_2,
...
};
class Event
{
EventID id;
public:
Event(EventID id):id(id){}
EventID id() const {return id;}
virtual ~Event() = 0;
};
class Event1 : public Event
{
int n;
public:
Event1(int n):Event(EVENT_1), n(n){}
int getN() const {return n;}
};
class Event2 : public Event
{
std::string s; …
Run Code Online (Sandbox Code Playgroud) 通过将XML文档加载到C++类,修改数据并将其序列化为XML,我一直在玩gSOAP XML数据绑定.
这是XML-library.xml的片段:
<?xml version="1.0" encoding="UTF-8"?>
<gt:Library xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:gt="http://www.bk.com/gSOAP/test">
<gt:Books>
<gt:Book isbn="0132350882" author="Robert C. Martin" title="Clean Code">
<gt:CopiesAvailable>2</gt:CopiesAvailable>
</gt:Book>
<gt:Book isbn="020161622X" author="Andrew Hunt" title="The Pragmatic Programmer">
<gt:CopiesAvailable>0</gt:CopiesAvailable>
</gt:Book>
<gt:Book isbn="0201633612" author="Erich Gamma" title="Design patterns">
<gt:CopiesAvailable>1</gt:CopiesAvailable>
</gt:Book>
</gt:Books>
...
</gt:Library>
Run Code Online (Sandbox Code Playgroud)
以下代码将XML加载到对象中,修改对象并将其序列化为XML.请注意,XML是通过文件流从文件加载的,并且要添加的数据是通过stdin(cin)从用户获得的.
main.cpp中:
#include "soapH.h"
#include "gt.nsmap"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::fstream;
using std::string;
using std::stringstream;
void DisplayAllBooks(const _gt__Library& library)
{
cout << "\n\nDisplaying …
Run Code Online (Sandbox Code Playgroud) 来自nsi脚本的代码片段:
VIProductVersion 1.2.0.0
VIAddVersionKey /LANG=${LANG_ENGLISH} FileVersion 1.1.0.0
Run Code Online (Sandbox Code Playgroud)
我想将FileVersion设置为,1.1.0.0
但在文件属性中将其设置为1.2.0.0
。我还注意到,单独VIProductVersion
执行会添加FileVersion键并设置其值。
文档说VIProductVersion添加了产品版本,但我看到的是实际上已添加FileVersion。这是NSIS中的错误吗?VIAddVersionKey FileVersion
如果将它设置的值替换为一个set by 的目的是VIProductVersion
什么?
VIAddVersionKey
需要VIProductVersion
调用,否则脚本不会编译。
我正在使用的版本:EclipseNSIS 0.9.8;MakeNSIS 2.46。操作系统:Windows 7。
让我们假设我们正在实现一个行为类似于向量的自定义集合,operator[]
如果集合为空,我们想要抛出一些异常.std::vector
在这种情况下有未定义的行为,但我们想抛出异常.如果这是C#我们可能会扔InvalidOperationException
.但在这种情况下哪个C++异常最合适/最直观?我认为std::out_of_range
不是最好的选择,因为集合是空的,因此没有"范围"索引将返回有效(任何)元素.
我希望实现这样的目标:
class C
{
int m_nVal;
public:
C(int nVal) : m_nVal(nVal){}
void foo(int nVal = m_nVal)
{
// use nVal, if provided; otherwise use m_nVal
}
};
C c(1);
c.foo(); // use 1
c.foo(2); // use 2
Run Code Online (Sandbox Code Playgroud)
这是不可能的,因为C++标准说:
非静态成员不得在默认参数中使用
我有的选择是:
(1)超载foo()
:
class C
{
int m_nVal;
public:
C(int nVal) : m_nVal(nVal){}
void foo()
{
// use m_nVal
}
void foo(int nVal)
{
// use nVal
}
};
Run Code Online (Sandbox Code Playgroud)
(2)使用静态成员:
class C
{
static int m_nVal;
public:
void foo(int nVal …
Run Code Online (Sandbox Code Playgroud) 我试图得到一个所有子目录(递归)的路径列表,其中有一些指定的名称,例如"bin"
.问题是如果当前目录包含该名称的子目录,则DIR命令将仅在该子目录中执行,忽略其他子目录.
例:
C:\DEVELOPMENT\RESEARCH>ver
Microsoft Windows [Version 6.1.7601]
C:\DEVELOPMENT\RESEARCH>dir *bin* /ad /s /b
C:\DEVELOPMENT\RESEARCH\bin
C:\DEVELOPMENT\RESEARCH\Apache\2bin
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin1
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin
C:\DEVELOPMENT\RESEARCH>dir bin* /ad /s /b
C:\DEVELOPMENT\RESEARCH\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin1
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin
C:\DEVELOPMENT\RESEARCH>dir bin /ad /s /b
C:\DEVELOPMENT\RESEARCH\bin\test
C:\DEVELOPMENT\RESEARCH>rmdir bin /s /q
C:\DEVELOPMENT\RESEARCH>dir bin /ad /s /b
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin
C:\DEVELOPMENT\RESEARCH>
Run Code Online (Sandbox Code Playgroud)
dir *bin* /ad /s /b
输出bin
名称中包含的所有子目录.这个输出没问题.与dir bin* /ad /s /b
输出名称开头的所有子目录相同bin
.但dir bin /ad /s /b
只输出当前目录中具有名称的第一个子节点的内容bin
.期望的输出是:
C:\DEVELOPMENT\RESEARCH\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
注意:如果当前目录不包含子目录bin
,则输出符合预期.(我删除了bin
孩子以显示这个)
我正在尝试在Xamarin中构建MonoMac项目时将Mac包(.app)作为构建输出.该项目实际上是一个默认项目 - 它只显示一个空窗口,可以通过以下方式在Xamarin中创建:添加新项目...,选择Mac(开源),然后选择MonoMac项目(C#).
项目构建没有错误,但构建输出是.exe文件.如何设置项目构建,以便输出.app包而不是.exe?我查看了Project Options - > Build - > General,其中可以选择Compile Target为*Executable/Executable with GUI /...*但这不是我需要的.还看了Xamarin工作室首选项 - >项目 - >构建,但它也没有帮助.
我的设置:
- Xamarin studio 4.0.3 (build 13) which includes free MonoMac
- Xcode 4.6.1 (2067, build 4H512); gcc installed
- Mono frameworks 2.10.9 and 2.10.12
- all running on Mac OS X 10.7.5
Run Code Online (Sandbox Code Playgroud)
构建Debug | x86构建配置的输出:
我有一个输入 XML 文件:
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<runtime name="test" version="1.2" xmlns:ns0="urn:schemas-microsoft-com:asm.v1">
<ns0:assemblyBinding>
<ns0:dependentAssembly />
</ns0:assemblyBinding>
</runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)
...和Python脚本:
import xml.etree.ElementTree as ET
file_xml = 'test.xml'
tree = ET.parse(file_xml)
root = tree.getroot()
print (root.tag)
print (root.attrib)
element_runtime = root.find('.//runtime')
print (element_runtime.tag)
print (element_runtime.attrib)
tree.write(file_xml, xml_declaration=True, encoding='utf-8', method="xml")
Run Code Online (Sandbox Code Playgroud)
...给出以下输出:
>test.py
configuration
{}
runtime
{'name': 'test', 'version': '1.2'}
Run Code Online (Sandbox Code Playgroud)
...并且将XML 修改为以下不良副作用:
<?xml version='1.0' encoding='utf-8'?>
<configuration xmlns:ns0="urn:schemas-microsoft-com:asm.v1">
<runtime name="test" version="1.2">
<ns0:assemblyBinding>
<ns0:dependentAssembly />
</ns0:assemblyBinding>
</runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我的原始脚本修改了 XML,因此我必须调用tree.write
并保存编辑后的文件。但问题是 ElementTree 解析器将xmlns
属性从 …
两个类,D1
并且D2
从一个抽象基类派生B
.他们每个人都在分享共同声明公共接口B
,但他们每个人可能也有自己特定的公共接口(例如D2
具有D2.Bar()
这才有用D2
对象):
public abstract class B
{
public int N { get; set; }
public abstract void Foo();
}
public class D1 : B
{
public override void Foo()
{
}
}
public class D2 : B
{
public override void Foo()
{
}
public void Bar()
{
}
}
Run Code Online (Sandbox Code Playgroud)
我不断派生对象的组合在单个集合,(如表),因为有时候我不得不打电话共同(继承)在集合中的所有对象,但有时我想调用方法Bar()
的D2
唯一对象:
var list = new List<B>();
list.Add(new D1());
list.Add(new D2());
foreach(var b in …
Run Code Online (Sandbox Code Playgroud) c++ ×6
c# ×3
downcast ×2
exception ×2
xml ×2
.net ×1
async-await ×1
batch-file ×1
casting ×1
cmd ×1
data-binding ×1
elementtree ×1
gsoap ×1
inheritance ×1
jasmine ×1
javascript ×1
macos ×1
monomac ×1
nsis ×1
oop ×1
parameters ×1
python ×1
state ×1
typedef ×1
winapi ×1
windows-7 ×1
xamarin ×1