我的问题很简单:如果我有一些类Man,我想定义返回man名称的成员函数,我更喜欢以下两种变体中的哪一种?
第一:
string name();
Run Code Online (Sandbox Code Playgroud)
第二:
void name(/* OUT */ string &name);
Run Code Online (Sandbox Code Playgroud)
第一个变体是低效的,因为它会产生不必要的副本(局部变量 - >返回值 - >赋值左侧的变量).
第二个变体看起来非常有效,但它让我哭泣
string name;
john.name(name);
Run Code Online (Sandbox Code Playgroud)
而不是简单
string name(john.name());
Run Code Online (Sandbox Code Playgroud)
那么,我更喜欢哪种变体,以及效率和便利性/可读性之间的适当权衡是什么?
提前致谢.
我正在使用.NET上的Windows.Forms开发简单的C#应用程序.我需要一些按钮,它会显示一个包含子类别的下拉菜单 - 很像ToolStripMenu,但是按钮,你知道.我搜索它并找不到任何变种.
我的问题是:有没有办法做到这一点,也许一些秘密按钮属性,允许附加菜单吗?
任何帮助将不胜感激.
例如,假设我std::string包含某些文件的UNIX样式路径:
string path("/first/second/blah/myfile");
Run Code Online (Sandbox Code Playgroud)
假设现在我想扔掉与文件相关的信息并从这个字符串获取'blah'文件夹的路径.那么是否有一个有效的(说'有效'我的意思是'没有任何副本')截断这个字符串的方式,"/first/second/blah"只包含它?
提前致谢.
我想用绘制图像UIImage的drawInRect:方法.这是代码:
UIImage *image = [UIImage imageNamed:@"OrangeBadge.png"];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
问题是生成的图像模糊.这是与源图像(左侧)相比得到的图像(在右侧):

我试过了两个,CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), NO) CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), NO)但这并没有解决问题.
有任何想法吗?
提前致谢.
这是代码:
def Property(func):
return property(**func())
class A:
def __init__(self, name):
self._name = name
@Property
def name():
doc = 'A''s name'
def fget(self):
return self._name
def fset(self, val):
self._name = val
fdel = None
print locals()
return locals()
a = A('John')
print a.name
print a._name
a.name = 'Bob'
print a.name
print a._name
Run Code Online (Sandbox Code Playgroud)
以上产生以下输出:
{'doc': 'As name', 'fset': <function fset at 0x10b68e578>, 'fdel': None, 'fget': <function fget at 0x10b68ec08>}
John
John
Bob
John
Run Code Online (Sandbox Code Playgroud)
代码取自这里.
问题:怎么了?它应该是简单的东西,但我找不到它.
注意:我需要属性来进行复杂的获取/设置,而不是简单地隐藏属性.
提前致谢.
我需要显示货币列表,以便用户可以选择一个.我想要包含iOS支持的所有货币.
iOS中有没有办法获取所有支持的货币的代码和名称?
提前致谢!
我在 Swift 中的泛型上遇到了一些困难。
我有以下代码:
class Parent {
init(value: SomeThing) {
// ...
}
func clone<T: Parent>() -> T {
return T(value: SomeThing)
}
}
class Child : Parent {
var otherValue: SomeThingElse?
override func clone<T>() -> T where T : Parent {
let clone: Child = super.clone()
clone.otherValue = self.otherValue
return clone //ERROR: cannot convert return expression of type 'Child' to return type T
}
}
Run Code Online (Sandbox Code Playgroud)
这个想法是创建一个简单的方法,返回具有相同值的子实例的新副本。我不想为每个子类类型编写构造函数。(它在真实的类中有很多参数,我喜欢保持干净)。
我得到的错误是:
cannot convert return expression of type 'Child' to return type T
建议的解决方案是制作它 …
我正在使用ctime函数来获取time_t变量的可读表示.ctime声明如下:
char *ctime (const time_t *timer);
Run Code Online (Sandbox Code Playgroud)
你可以看到它返回一个指向结果char数组的指针,而没有在参数中传递任何char指针.因此,我想知道在哪里ctime分配char缓冲区以及谁将销毁它并且不在内部分配内存(malloc当然除了各种各样的内存)被认为是一团糟.
有什么建议?
让它成为std::list::iterator和std::list::reverse_iterator.反向派来自前锋吗?如果没有那么为什么没有相反的成员函数list?
提前致谢.
c++ ×4
ios ×2
string ×2
antialiasing ×1
c# ×1
ctime ×1
generics ×1
iterator ×1
localization ×1
objective-c ×1
properties ×1
python ×1
stl ×1
swift ×1
uiimage ×1
uikit ×1
winforms ×1