我反汇编了一个用C++编写的简单程序,并且有两个函数名.我猜ctor意味着构造函数,dtor意味着析构函数,而word global可能意味着它们创建并销毁全局对象.我无法猜出这个名字.这两个功能有什么作用?
插槽(在slots节中声明的方法)和Qt中的方法(使用Q_INVOKABLE关键字声明的方法)之间有什么区别?它们都可以使用调用QMetaObject::invokeMethod,当使用SLOT宏连接到插槽时它们都被接受,但是当获得metamethod的类型时,它可以返回QMetaMethod::Method或者QMetaMethod::Slot,所以看起来Qt有一些区别?
有这个代码:
# assignment behaviour for integer
a = b = 0
print a, b # prints 0 0
a = 4
print a, b # prints 4 0 - different!
# assignment behaviour for class object
class Klasa:
def __init__(self, num):
self.num = num
a = Klasa(2)
b = a
print a.num, b.num # prints 2 2
a.num = 3
print a.num, b.num # prints 3 3 - the same!
Run Code Online (Sandbox Code Playgroud)
问题:
有这个代码:
#include <iostream>
class Outer{
int a; // private data member of class Outer
public:
Outer(): a(55){}
class Inner{
public:
void fun(Outer ob){
std::cout << ob.a << std::endl;
}
};
};
int main() {
Outer::Inner object;
object.fun(Outer()); // prints 55
//std::cout << (Outer().a) << std::endl; error: 'int Outer::a' is private
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么内部类可以访问类外部的私有成员数据'a'?在本文XL C/C++ V8.0 for Linux之后,它不应该编译,但它在g ++ 4.4.0上编译.
在这个答案/sf/answers/293558891/中写道,"内联函数默认具有外部链接".但是,默认情况下不可能链接内联的内容.那么说内联函数有外部联系是什么意思呢?
有这个代码:
int x;
//void x(); // error: redefinition of 'x' as different kind of symbol
class x {}; // works ok
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么定义具有相同名称的变量和类是合法的,但定义具有相同名称的变量和函数是不合法的?
有这个代码:
class MyException(Exception):
pass
def gen():
for i in range(3):
try:
yield i
except MyException:
print("MyException!")
a = gen()
next(a)
a.throw(MyException)
Run Code Online (Sandbox Code Playgroud)
运行此代码:
$ python3.3 main.py
MyException!
$ python3.3 main.py
MyException!
Exception TypeError: TypeError('catching classes that do not inherit from BaseException is not allowed',) in <generator object gen at 0xb712efa4> ignored
$ python3.3 main.py
MyException!
$ python3.3 main.py
MyException!
$ python3.3 main.py
MyException!
Exception TypeError: TypeError('catching classes that do not inherit from BaseException is not allowed',) in <generator object gen at …Run Code Online (Sandbox Code Playgroud) 有这样的代码:
#include <iostream>
#include <vector>
template <class T>
class A{
public:
class iterator : public std::vector<T>::iterator{
public:
T& operator*(){
??
}
};
iterator begin(){
return v.begin(); // error
}
iterator end(){
return v.end(); // error
}
void add(const T& elem){
v.push_back(elem);
}
private:
std::vector<T> v;
};
int main() {
A<int> a;
a.add(2);
a.add(4);
for(A<int>::iterator it = a.begin(); it != a.end(); ++it){
std::cout << *it << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是std::vector我自己的附加功能的包装.我想使用std::vector的迭代器,但我只想改变operator*迭代器的行为:
T& operator*(){
// …Run Code Online (Sandbox Code Playgroud) 有这个代码:
class Sample:
variable = 2
object1 = Sample()
object2 = Sample()
print object1.variable # 2
print object2.variable # 2
object1.variable = 1
print object1.variable # 1
print object2.variable # 2 <- why not 1 too?
Run Code Online (Sandbox Code Playgroud)
为什么object2.variable在赋值给类变量后也不是1?
有这个代码:
#include <iostream>
class Bazowa
{
int x;
public:
Bazowa() : x(55){}
Bazowa(int x_) : x(x_) {}
void fun()
{
std::cout << x << "fun\n";
}
};
class Pochodna1 : virtual public Bazowa
{
public:
Pochodna1() : Bazowa(101) {}
};
class Pochodna2 : virtual public Bazowa
{
public:
Pochodna2() : Bazowa(103) {}
};
class SuperPochodna : public Pochodna1, public Pochodna2
{
public:
SuperPochodna() : {}
};
int main() {
SuperPochodna sp;
sp.fun(); // prints 55fun
return 0;
}
Run Code Online (Sandbox Code Playgroud)
执行该程序后,将打印"55fun".在Pochodna1和Pochodna2类中构造函数调用发生了什么 - …
c++ ×7
python ×3
assembly ×1
constructor ×1
destructor ×1
elf ×1
exception ×1
generator ×1
inheritance ×1
nested-class ×1
python-3.x ×1
qt ×1
stl ×1