小编scd*_*dmb的帖子

__do_global_dtors_aux和__do_global_ctors_aux

我反汇编了一个用C++编写的简单程序,并且有两个函数名.我猜ctor意味着构造函数,dtor意味着析构函数,而word global可能意味着它们创建并销毁全局对象.我无法猜出这个名字.这两个功能有什么作用?

c++ assembly constructor destructor elf

8
推荐指数
2
解决办法
1万
查看次数

Qt中的插槽和方法有什么区别?

插槽(在slots节中声明的方法)和Qt中的方法(使用Q_INVOKABLE关键字声明的方法)之间有什么区别?它们都可以使用调用QMetaObject::invokeMethod,当使用SLOT宏连接到插槽时它们都被接受,但是当获得metamethod的类型时,它可以返回QMetaMethod::Method或者QMetaMethod::Slot,所以看起来Qt有一些区别?

c++ qt

8
推荐指数
2
解决办法
1492
查看次数

对象和基本类型的分配

有这个代码:

# 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)

问题:

  1. 为什么赋值运算符对基本类型和类对象的工作方式不同(对于基本类型,它按值复制,对于通过引用复制的类对象)?
  2. 如何只按值复制类对象?
  3. 如何为C++ int&b = a中的基本类型引用?

python

7
推荐指数
2
解决办法
2423
查看次数

访问内部类中外部类的私有成员数据

有这个代码:

#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上编译.

c++ nested-class

7
推荐指数
2
解决办法
2853
查看次数

内联函数和外部链接

在这个答案/sf/answers/293558891/中写道,"内联函数默认具有外部链接".但是,默认情况下不可能链接内联的内容.那么说内联函数有外部联系是什么意思呢?

c++

7
推荐指数
1
解决办法
2110
查看次数

具有相同名称的类和变量的定义

有这个代码:

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)

为什么定义具有相同名称的变量和类是合法的,但定义具有相同名称的变量和函数是不合法的?

c++

7
推荐指数
3
解决办法
999
查看次数

有时显示异常TypeError警告,有时不使用生成器的throw方法

有这个代码:

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)

python exception generator python-3.x

7
推荐指数
1
解决办法
5943
查看次数

包装STL向量并更改其迭代器的行为

有这样的代码:

#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)

c++ stl

6
推荐指数
1
解决办法
3790
查看次数

同一类的两个实例的Class属性

有这个代码:

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?

python

6
推荐指数
1
解决办法
1218
查看次数

基类的多个虚拟继承和构造函数调用

有这个代码:

#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++ inheritance

6
推荐指数
1
解决办法
1977
查看次数