我知道在Qt中有很多关于内存管理的问题.我也读了这些问题:
但就我而言,我再次感到困惑!
我有一个QTableWidget名字myTable.我通过以下方式添加运行时小部件setCellWidget:
void MyClass::build()
{
for (int i=LOW; i<HIGH; i++)
{
QWidget *widget = new QWidget(myTable);
//
// ...
//
myTable->setCellWidget(i, 0, widget);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我删除所有项目,如下所示:
void MyClass::destroy()
{
for (int i = myTable->rowCount(); i >= 0; --i)
myTable->removeRow(i);
}
Run Code Online (Sandbox Code Playgroud)
这些方法在很长一段时间内多次调用.而且myTable,这些小部件的父母将按照程序的生命周期生活.
剂量方法destroy()相当自动释放内存?或者我必须自己delete分配小部件,如下所示?
void MyClass::destroy2() // This maybe causes to crash !!
{
for (int i = myTable->rowCount(); i >= 0; --i)
{
QWidget *w …Run Code Online (Sandbox Code Playgroud) 我有一个GtkDrawingArea用于可视化数据.根据数据库和用户输入,绘图可能会变得非常大(大于允许的最大GtkDrawingArea大小).因此,我想使用与当前窗口一样大的绘图区域,并在滚动时手动更新它.
如果我使用ScrolledWindow + Viewport方法将滚动条添加到绘图区域,它显然不起作用,因为绘图区域不够大,不需要滚动条.
有没有什么方法可以让视口欺骗认为底层小部件比实际大?如果不是什么是解决这个问题的最佳方法?
注意:我使用Gtk2并且不可能切换到Gtk3.
当我阅读关于移动语义和右值引用的示例时,它们正在利用rvalue引用的优点并在围绕指针的 大对象周围移动语义,例如:1 2
例如,它们只是复制移动对象内的指针并将其设置为nullptr.(移动/交换)
我的问题是,移动语义对于没有指针但是它们很大的对象有任何优势(性能)吗?
class BigClass
{
int data[BIG_SIZE];
int a01;
.
.
. many members
.
.
int z99;
public:
move constructor ?!
};
Run Code Online (Sandbox Code Playgroud) 阅读这个 Q&A,我认为p不应该是一个nullptr即使x是0.我明白了吗?
int main()
{
int x = 0;
std::cin >> x; // Enter `0`
void *p = (void *)x; // or: void *p = reinterpret_cast<void*>(x);
if (!p)
std::cout << "p is nullptr" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输入0标准输入后,该消息p is nullptr将显示在我的GCC中.根据链接,它不应该评估nullptr,但结果不是我的期望.
代码是否未定义行为?或未指明的结果?为什么要评估nullptr?
我有班苹果
class apples
{
private:
double x;
double y;
double z;
public:
//some methods
};
Run Code Online (Sandbox Code Playgroud)
我想将指针存储到矢量中的苹果对象.我这样做是为了在任何文件中创建任何对象并使用任何文件中的任何对象.我使用以下代码来确定我可以存储在该向量中的指针的最大数量
int _tmain(int argc, _TCHAR* argv[])
{
vector<apples *> myvector;
cout<<"max :"<<myvector.max_size();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给了我:
1073741823
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是,我真的可以在该向量中存储1073741823没有指针,或者这是向量的内存限制(即1073741823字节)吗?
所以如果有2个向量
vector<int> A
&
vector<double> B
Run Code Online (Sandbox Code Playgroud)
A有1073741823元素和B还有1073741823元素?我要求澄清一点, 矢量可以存储的元素的最大数量不依赖于存储的实体类型(int或double)? (这与向量的当前容量无关!)另外,指向apple对象的指针的大小(不要求苹果对象的大小!)?谢谢.
我正在使用JGraphT,我有两个DirectedGraph:g1和g2.
我该如何合并g1和g2第三图形g3?我需要g3是一个普通的图形,并能够添加新的边和顶点.
我想把a QWidget放入一个QGraphicsView并通过使用使小部件可选择和移动QGraphicsProxyWidget.(这完全适用QGraphicsRectItem,QGraphicItem等等)
这是我目前使用的代码:
// Create new QGraphicsScene and assign to graphicsView
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
// Create widget and add to scene
MyWidget *widget = new MyWidget;
QGraphicsProxyWidget *proxy = scene->addWidget(widget);
// Make selectable
proxy->setFlag(QGraphicsItem::ItemIsSelectable, true);
// Make movable
proxy->setFlag(QGraphicsItem::ItemIsMovable, true);
Run Code Online (Sandbox Code Playgroud)
小部件显示正确,但它既不可移动也不可选择!
任何帮助将不胜感激 ;)
1) 有什么区别:
complex& operator = (const complex& c);
Run Code Online (Sandbox Code Playgroud)
和
complex operator = (complex c);
Run Code Online (Sandbox Code Playgroud)
2)如果我定义(在第二种情况下)复制构造函数,它们是否相同?
complex::complex (const complex& c){
//code
}
Run Code Online (Sandbox Code Playgroud)
3)const的作用是什么?
为什么有时候不会移动构造函数调用?测试move-semantics (实时代码):
struct Test {
int id;
Test(int id) : id(id) {
cout << id << " Test() " << endl;
}
~Test() {
cout << id << " ~Test() " << endl;
}
Test(const Test &t) : id(t.id) {
cout << id << " Test(const Test &t) " << endl;
}
Test(Test &&t) : id(t.id) {
cout << id << " Test(Test &&t) " << endl;
}
Test &operator=(const Test &t) {
cout << id << " operator=(const …Run Code Online (Sandbox Code Playgroud) 尝试嵌套宏调用,如下所示:
#include <stdint.h>
#define INT
#define LONG
#define AS(t) AS_##t
#define AS_INT as_int
#define AS_LONG as_long
#define LET(v, t) v. AS(t)
typedef union
{
int32_t as_int;
int64_t as_long;
} mytype_t;
int main()
{
mytype_t s;
s.AS(INT) = 10; /* This is OK */
LET(s, INT) = 10; /* This makes error */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它产生错误:
main.c:xx:yy: error: ‘mytype_t {aka union <anonymous>}’ has no member named ‘AS_’
#define LET(v, t) v. AS(t)
^
main.c:zz:ww: note: in expansion of macro ‘LET’ …Run Code Online (Sandbox Code Playgroud)