class Test
{
void func(){int i;}
};
int main()
{
cout<<sizeof(Test)<<endl; //gives 1
}
Run Code Online (Sandbox Code Playgroud)
为什么sizeof(Test)不考虑函数的大小.功能在哪里真正与它的身体存储?在Google中找不到任何链接来解释这一点.
编辑:如果除了维基之外提供有关类内部分的任何好的链接,那将是很好的.
class Divide
{
public:
float divident, divisor;
Divide():divident(10.0f),divisor(0.0f){}
};
int main()
{
Divide obj[100];
int quotient = obj[1].divident/obj[1].divisor;
return quotient;
}
Run Code Online (Sandbox Code Playgroud)
编辑:编译器Qt 5.3.1,Windows 7-32位.
为什么在编译时没有除零警告或发生运行时崩溃?
下面给出的代码成功编译:
template <typename T, T nontype_param>
class C;
class X {
public:
int n;
};
int main()
{
C<int X::*, &X::n>* c; //Here
return 1;
}
Run Code Online (Sandbox Code Playgroud)
Scope解析器操作符如何在此处工作而不是. operator?我们是否可以访问这样的非静态成员?
参考:C++模板完整指南,第8.3.3节非类型参数
我在C++中遇到了Hash Map实现.HashMap的构造函数包含下面的代码.这条线new HashEntry*[TABLE_SIZE]说的是什么.我以前从未见过这样的结构.它如何返回指针指针?
class HashMap {
private:
HashEntry **table;
public:
HashMap() {
table = new HashEntry*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = NULL;
}
};
Run Code Online (Sandbox Code Playgroud) 我在irb中遇到错误
NameError: uninitialized constant Student
Run Code Online (Sandbox Code Playgroud)
用于Student.new或任何模型操作.
但在rails中它没有错误,它工作正常.那是什么原因?
此错误仅发生在Windows中,与Linux中的代码相同,并且工作正常.
这有什么不同?
我试图学习yield对我来说非常困惑的方法.最后,当我以为我已经理解了这个方法时,突然出现了这段代码
def test
puts "You are in the method"
yield
puts "You are again back to the method"
yield
end
Run Code Online (Sandbox Code Playgroud)
当我执行包含此代码的文件时,它不会打印任何内容.据了解,没有人调用这种方法所以没有输出.
但是如果我添加,在同一个文件中
test {puts "You are in the block"}
Run Code Online (Sandbox Code Playgroud)
在测试函数下方突然开始打印puts语句.
我不明白谁在调用这个方法.如果test {..}阻塞导致呼叫不应该首先执行其内容?
但是输出是
You are in the method
You are in the block
You are again back to the method
You are in the block
Run Code Online (Sandbox Code Playgroud) class Hello
puts self
end
Run Code Online (Sandbox Code Playgroud)
运行此代码输出Hello.但谁打电话呢?我没有创建任何对象或调用任何类方法.
vector<int> data(istream_iterator<int>(cin),
istream_iterator<int>{}); cout<<"Size is : " << data.size() << endl; //compile success
vector<int> data1(istream_iterator<int>(cin),
std::allocator<int>{}); cout<<"Size is : " << data1.size() << endl; //compile failure
Run Code Online (Sandbox Code Playgroud)
error: no matching function for call to ‘std::vector<int>::vector(std::istream_iterator<int>, std::allocator<int>)’ vector<int> data1(istream_iterator<int>(cin), std::allocator<int>{});
Run Code Online (Sandbox Code Playgroud)
为什么第一个语句很好,但第二个呢?int在这种情况下,向量不采用类型分配器吗?我正在试验allocators。