我们有一个班级Test
和一个班级AnotherClass
.两者都来自QObject.
Test.h:
class Test : public QObject
{
Q_OBJECT
public:
Test(QObject* parent);
~Test();
private:
AnotherClass* other;
};
class AnotherClass : public QObject
{
Q_OBJECT
public:
AnotherClass(QObject* parent);
~AnotherClass();
};
Run Code Online (Sandbox Code Playgroud)
TEST.CPP:
Test::Test(QObject* parent) : QObject(parent)
{
other = new AnotherClass(this);
}
Test::~Test()
{
delete other;
}
Run Code Online (Sandbox Code Playgroud)
other
应该在Test
-instance被销毁时自动销毁,因为Test
它是父other
,对吧?
other
通过自己在~Test()
或是否处于不确定阶段退出该计划,因为它试图删除同样的对象两次?我有两个类,base_class和derived_class以及以下代码:
base_class *ptr = new derived_class;
delete ptr;
Run Code Online (Sandbox Code Playgroud)
这段代码会产生内存泄漏吗?如果是这样,我该如何处理呢?
我开始学习c ++,但我陷入了析构函数.我们需要实现一个向量,这是我到目前为止所拥有的.
#include<string.h>
#include<cassert>
#include<iostream>
using namespace std;
template<class T>
class Vector {
template<class U> friend ostream& operator<<(ostream&, const Vector<U>&);
private:
T* data;
unsigned len;
unsigned capacity;
public:
Vector(unsigned = 10);
Vector(const Vector<T>&);
virtual ~Vector(void);
Vector<T>& operator =(const Vector<T>&);
bool operator==(const Vector<T>&);
T& operator[](unsigned);
};
//PROBLEM!
template <class T>
~ Vector() {
delete data;
}
template<class T>
Vector<T>::Vector(unsigned int _capacity)
{
capacity = _capacity;
len = _capacity;
data = new T[_capacity];
}
template<class T>
Vector<T>::Vector(const Vector<T> & v)
{
len …
Run Code Online (Sandbox Code Playgroud) 我怎样才能在std :: vector中存储多个shared_ptr,每个shared_ptr都有一个指向不同类型的指针?
std::vector < ? > vec;
vec.push_back( make_shared<int>(3));
vec.push_back( make_shared<float>(3.14f));
Run Code Online (Sandbox Code Playgroud)
是否有一个基本的多态类可以用于该任务而不必使用特定于编译器的东西?
在现代C++中是否有一种方法可以防止类被虚拟继承,同时允许定期继承?现在对我来说似乎不可能,但这种语言中有太多东西似乎是不可能的.
class BASE {
public:
virtual ~BASE() {}
void lamp() {
cout << "\nBASE CLASS";
}
};
class DERIVED : public BASE {
public:
void fun();
};
void DERIVED::fun() {
cout << "\nDERIVED CLASS!";
}
int main() {
BASE * pbase = new DERIVED; //BASE CLASS POINTER
void * vbase = pbase; //VOID POINTER TAKING BASE POINTER
DERIVED * pder; //DERIVED CLASS POINTER
//pder = static_cast<DERIVED *>(vbase); //THIS WORKS
pder = dynamic_cast<DERIVED *>(vbase); //THIS DOESN'T
pder->lamp();
pder->fun();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
每当我尝试将 …
我遇到了这个问题,我想将不同的类(共享相同的接口)存储到一个公共容器中。
在现代 C++ 中可以做到这一点吗?
当我不想将对象存储为指针时,是否允许这样做?如果我必须使用指针,那么推荐或更简洁的方法应该是什么?
处理此类用例的正确方法应该是什么?
#include <iostream>
#include <string>
#include <vector>
enum class TYPES: int {TYPE1 = 0, TYPE2, INVALID};
class IObj
{
public:
virtual auto ObjType(void) -> TYPES = 0;
};
class InterfaceObj: IObj
{
public:
virtual auto Printable(void) -> void = 0;
};
class InterfaceTesla
{
public:
virtual auto Creatable(void) -> void = 0;
};
class CObj: InterfaceObj
{
private:
std::string msg;
public:
CObj()
{
msg = "Elon Mask!";
}
virtual auto ObjType(void) -> TYPES override
{ …
Run Code Online (Sandbox Code Playgroud) 我知道这并std::unique_ptr
不能保证内存安全,尤其是在循环依赖的情况下。然而,就我而言,我看不到它。Child
包含Parent
(或者SecondChild
在多态性的情况下),但它们都不包含Child
.
Valgrind 报告4 bytes
丢失,所以我认为它SecondChild
没有被破坏。我的代码确实依赖于多态性,因此我希望获得有关重构的建议,这些建议不会Parent*
在Child
.
#include <iostream>
#include <memory>
using namespace std;
struct Parent
{
Parent() {
cout << "Expr created" << endl;
}
~Parent() {
cout << "Expr destroyed" << endl;
}
};
struct Child : public Parent
{
std::unique_ptr<Parent> content;
};
struct SecondChild : public Parent
{
int val;
};
std::unique_ptr<Parent> foo()
{
auto test = make_unique<Child>();
auto content_in_child = make_unique<SecondChild>(); …
Run Code Online (Sandbox Code Playgroud) 我试图熟悉OOP概念,但不太明白它的概念virtual
.
virtual destructor
而不是一个virtual constructor
.为什么?virtual destructors
内部如何处理?我指的是连接虚析构函数说明了这个概念,但我的问题是如何将vptr
两者的vtable
S(派生和基地)被称为?(在虚拟成员函数的情况下,当这种情况发生时vptr
,Derived类指向的函数通常只被调用)virtual destructor
?任何人都可以通过链接/示例帮助我理解上述概念吗?
我不明白一件事.例如,我声明A类和B类是A的子类:
class A {
public:
int a;
}
class B : public A {
public:
int b;
}
Run Code Online (Sandbox Code Playgroud)
显然,如果我创建A或B的实例,它们在内存中的大小可以由类型确定.
A instanceA; // size of this will probably be the size of int (property a)
B instanceB; // size of this will probably be twice the size of int (properties a and b)
Run Code Online (Sandbox Code Playgroud)
但是如果我创建动态实例然后在以后释放它会怎样?
A * instanceAPointer = new A();
A * instanceBPointer = new B();
Run Code Online (Sandbox Code Playgroud)
这些是不同类的实例,但程序会将它们视为A类的实例.在使用它们时这很好但是如何释放它们呢?为了释放分配的内存,程序必须知道要释放的内存大小,对吧?
所以,如果我写
delete instanceAPointer;
delete isntanceBPointer;
Run Code Online (Sandbox Code Playgroud)
程序如何知道,有多少内存,从每个指针指向的地址开始,它应该是空闲的?因为很明显,对象具有不同的大小,但程序认为它们是A类型.
谢谢
c++ ×10
inheritance ×2
oop ×2
dynamic-cast ×1
gcc ×1
memory-leaks ×1
object ×1
polymorphism ×1
qt ×1
shared-ptr ×1
templates ×1
unique-ptr ×1
vector ×1
virtual ×1