来自Python 文档:
不能保证
__del__()在解释器退出时仍然存在的对象调用方法。
据我了解,也无法保证对象在解释器退出之前停止存在,因为由垃圾收集器决定是否以及何时删除对象。
那么使用这种方法有什么意义呢?您可以在其中编写清理代码,但不能保证它会被执行。
我知道您可以使用try-finally或with子句来解决这个问题,但我仍然想知道该__del__()方法的有意义的用例是什么。
考虑以下代码
#include <iostream>
using namespace std;
enum myEnum { a, b, c };
void test(myEnum e) {
cout << "myEnum overload" << endl;
}
void test(unsigned int i) {
cout << "unsigned int overload" << endl;
}
int main() {
test(a);
test(1);
test(1u);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(我知道enum class比enum这种事情更安全,但是我使用的是带有旧式枚举的开源代码。)
当我用g ++ 4.4.7编译并运行它时,我得到了
myEnum overload
unsigned int overload
unsigned int overload
Run Code Online (Sandbox Code Playgroud)
也就是说,编译器更喜欢转换int,而unsigned int不是将其转换为myEnum。这就是我想要的,但是我想知道是否总是可以保证。该标准并未明确规定底层类型myEnum应该是什么,因此我认为也许是这样int,也许会更受欢迎unsigned int。
但是当我注释掉unsigned …
我想知道在编译C++程序时,.o或.so文件中存储的确切内容. 这篇文章给出了编译过程的一个相当不错的概述,并在其.o文件的功能,而据我了解,从这个帖子,u和.so文件只是多个.o文件合并成一个单一的文件,以静态(.a)或动态(.so)方式链接.
但我想检查一下我是否正确理解了这样一个文件中存储的内容.编译以下代码后
void f();
void f2(int);
const int X = 25;
void g() {
f();
f2(X);
}
void h() {
g();
}
Run Code Online (Sandbox Code Playgroud)
我希望在.o文件中找到以下项目:
g(),包含一些占位符地址f()和f2(int)调用的位置.h(),没有占位符X,这只是数字25g(),h()并且X可以找到f()和f2(int),其必须在链接期间得到解决.然后一个程序nm会列出两个表中的所有符号名称.
我想编译器可以f2(X)通过调用f2(25)来优化调用,但它仍然需要将符号X保留在.o文件中,因为无法知道它是否将从不同的.o文件中使用.
那会是正确的吗?.a和.so文件是否相同?
谢谢你的帮助!
sc_port有人能清楚直观地解释SystemC中的an和an有什么区别吗sc_export?什么时候使用端口,什么时候导出?
我已经阅读了手册的部分内容,但我仍然无法掌握两者之间的主要概念差异。
模板类的成员函数可以完全特化,例如
template<class A>
struct MyClass {
// Lots of other members
int foo();
};
template<class A>
MyClass<A>::foo() { return 42; }
template<>
MyClass<int>::foo() { return 0; }
Run Code Online (Sandbox Code Playgroud)
会编译没有问题。请注意,这foo()不是模板函数,因此这与模板函数专业化无关(我可以理解在那里不允许部分专业化,因为它与重载相结合会变得非常混乱)。在我看来,上面的代码只是以下模板类专业化的简写:
template<class A>
struct MyClass {
// Lots of other members
int foo();
};
template<class A>
MyClass<A>::foo() { return 42; }
template<>
struct MyClass<int> {
// Copy all the other members from MyClass<A>
int foo();
};
template<>
MyClass<int>::foo() { return 0; }
Run Code Online (Sandbox Code Playgroud)
这样对吗?
在这种情况下,我想知道为什么不允许使用类似速记的部分专业化,即为什么我不能写
template<class …Run Code Online (Sandbox Code Playgroud) c++ templates partial-specialization member-functions class-template
执行以下 SystemVerilog 代码时(用 Questa 编译并运行)
bit [7:0] test = 255;
$display("%b %b %b", test, test == 255, test == '1);
$display("%b %b %b", ~test, ~test == 0, ~test == '0);
$display("%b %b %b", 8'b00000000, 8'b00000000 == 0, 8'b00000000 == '0);
Run Code Online (Sandbox Code Playgroud)
输出是
11111111 1 1
00000000 0 1
00000000 1 1
Run Code Online (Sandbox Code Playgroud)
我的问题是关于第二个输出行上的第二个数字:二进制00000000与 0 有何不同?为什么它只有在它是 的结果时才不同~test,而在它是文字时才不同?这是 Questa 的错误还是语言的属性?
以下代码
#include <iostream>
using namespace std;
class A {};
class B : public A {};
class C : public B {};
void foo(A *a) {
cout << 'A' << endl;
}
void foo(B *b) {
cout << 'B' << endl;
}
int main() {
C *c = new C[10];
foo(c);
}
Run Code Online (Sandbox Code Playgroud)
编译好并按预期打印'B'.
但是当我改变main()功能时
int main() {
C c[10];
foo(c);
}
Run Code Online (Sandbox Code Playgroud)
我收到编译错误说
test_arr.cpp: In function 'int main()':
test_arr.cpp:23:10: error: call of overloaded 'foo(C [10])' is ambiguous
test_arr.cpp:23:10: note: candidates …Run Code Online (Sandbox Code Playgroud) c++ ×4
archive-file ×1
arrays ×1
c ×1
compilation ×1
destructor ×1
enums ×1
finalizer ×1
object-files ×1
overloading ×1
pointers ×1
python ×1
systemc ×1
templates ×1