您可以在for循环中定义2个相同类型的变量:
int main() {
for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
但是定义不同类型的变量是违法的:
int main() {
for (int i = 0, float j = 0.0; i < 10; i += 1, j = 2*i) {
cout << j << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?(我不需要i
在循环内使用,只是j
.)
如果你完全被黑客攻击并且模糊不清,那对我来说没问题.
在这个人为的例子中,我知道你可以使用double
这两个变量.我正在寻找一般答案.
请不要建议移动body之外的任何变量,可能对我不可用,因为一个迭代器必须在循环之后消失并且for语句将被包含在我的foreach
宏中:
#define foreach(var, iter, instr) { \
typeof(iter) var##IT = …
Run Code Online (Sandbox Code Playgroud) 初始化shared_ptr成员变量时:
// .h
class Customer
{
public:
Customer();
private:
std::shared_ptr<OtherClass> something_;
}
// .cpp
Customer():
something_(new OtherClass())
{
}
Run Code Online (Sandbox Code Playgroud)
与
Customer():
something_(std::make_shared<OtherClass>())
{
}
Run Code Online (Sandbox Code Playgroud)
是否允许使用make_shared版本?我似乎总是看到第一个版本,这是首选?
尝试用C语言编译程序时,我有一些警告:
Run Code Online (Sandbox Code Playgroud)13:20: warning: excess elements in array initializer [enabled by default] 13:20: warning: (near initialization for ‘litera’) [enabled by default] 22:18: warning: excess elements in array initializer [enabled by default] 22:18: warning: (near initialization for ‘liczba’) [enabled by default] 43:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat] 45:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat] 55:2: warning: format ‘%s’ expects argument of …
我试图创建一个分隔字符的程序.问题是:
"创建一个char数组并使用数组初始值设定项来初始化数组,字符串为'Hi there'.使用for-statement显示数组的内容.用空格分隔数组中的每个字符".
我制作的节目:
String ini="Hi there";
char[] array=new char[ini.length()];
for(int count=0;count<array.length;count++){
System.out.print(" "+array[count]);
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决这个问题?
所以我在网上搜索解决方案,但似乎有关于测试在Rails中创建的初始化程序的稀缺信息.
目前,我在config/initializers/retrieve_users.rb文件中编写了一个非常大的API call-n-store.它发出API请求,解析JSON,然后将数据存储为用户.由于非常实质,我还没有找到最简洁的测试方法.由于我需要在运行任何函数之前检索用户,我不相信我可以在其他地方移动此脚本(尽管欢迎其他建议).我有几个问题:
谢谢!
我听说__init__
python 中的函数不是构造函数,它是一个初始化器,实际上__new__
函数是构造函数,不同之处在于__init__
函数是在创建对象和__new__
之前调用之后调用的.我对吗?你能解释的区别更好,我们为什么需要两个__new__
和__init__
?
我有一个类直接映射JSON实现Mappable
(ObjectMapper框架)协议,我试图继承NSManagedObject
.
class AbstractModel: NSManagedObject, Mappable {
@NSManaged var uuid: String?
@NSManaged var updatedAt: String?
@NSManaged var createdAt: String?
required init?(_ map: Map) {
mapping(map)
}
func mapping(map: Map) {
uuid <- map["uuid"]
updatedAt <- map["updatedAt"]
createdAt <- map["createdAt"]
}
}
Run Code Online (Sandbox Code Playgroud)
这个实现的问题是编译器抱怨mapping(map)
在超级初始化器之前使用self:
AbstractModel.swift:19:9: Use of 'self' in method call 'mapping' before super.init initializes self
不幸的是我以前不能调用super initializer(super.init(entity: NSEntityDescription, insertIntoManagedObjectContext: NSManagedObjectContext?)
)mapping(map)
因为我需要self
得到它NSManagedObjectContext
.
我该怎么解决这个问题?
这是我的代码:
public class A<T : Any> {
public init(n : Int) {
print("A")
}
}
public class B : A<Int> {
}
public class C : B {
}
let x = C(n: 123)
Run Code Online (Sandbox Code Playgroud)
这无法编译并大喊这样的错误:
repl.swift:9:9: error: 'C' cannot be constructed because it has no accessible initializers
Run Code Online (Sandbox Code Playgroud)
可以编译以下代码.
public class A {
public init(n : Int) {
print("A")
}
}
public class B : A {
}
public class C : B {
}
let x = C(n: 123)
Run Code Online (Sandbox Code Playgroud)
是否应该继承要求类型指定的泛型类型的初始值设定项? …
我想把我的chargify conf放在初始化器中,但我发现初始化器不会在我的rails c中执行,有没有办法调用我的初始化器,所以我可以在我的控制台中测试?
Chargify.configure do |c|
c.api_key = "keykey"
c.subdomain = "test-site"
end
Run Code Online (Sandbox Code Playgroud) 我在我的Swift文件中有一个Initializer方法,如下所示:
public init(frame: CGRect, type: NVActivityIndicatorType? = nil, color: UIColor? = nil, padding: CGFloat? = nil) {
self.type = type ?? NVActivityIndicatorView.DEFAULT_TYPE
self.color = color ?? NVActivityIndicatorView.DEFAULT_COLOR
self.padding = padding ?? NVActivityIndicatorView.DEFAULT_PADDING
super.init(frame: frame)
isHidden = true
}
Run Code Online (Sandbox Code Playgroud)
我想从我的Objective-C文件中调用此方法,但在编译时抛出它的错误.
错误:
/ Users/Desktop/old data/ChatScreenViewController.m:396:92:'NVActivityIndicatorView'没有可见的@interface声明选择器'initWithFrame:type:color:padding:'
Obj-C调用代码:
NVActivityIndicatorView *objNVActivityIndicatorView = [[NVActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) type:NVActivityIndicatorTypeLineScalePulseOut color:[UIColor blueColor] padding:10]
Run Code Online (Sandbox Code Playgroud)
我试过的:
Objective-C
在我的快速课程中添加
@objc public final class NVActivityIndicatorView: UIView
Run Code Online (Sandbox Code Playgroud)
仍然无法访问上述方法.
我的Swift文件:NVActivityIndicatorView.swift
任何想法出了什么问题?
提前致谢!
initializer ×10
swift ×3
arrays ×2
c++ ×2
c ×1
c++11 ×1
char ×1
character ×1
config ×1
constructor ×1
for-loop ×1
generics ×1
ios ×1
java ×1
make-shared ×1
objective-c ×1
python ×1
rspec ×1
rspec-rails ×1
scope ×1
shared-ptr ×1