我有以下问题:在myClass中我想默认初始化一个指向yourClass的指针,带有一个新的yourClass地址.不幸的是,如果我想在任何一点删除指针,我得到一个(核心转储).
class myClass
{
protected:
yourClass * yc;
public:
myClass() { yc = new yourClass(); }
myClass(yourClass * tyc ) { delete yc; yc = tyc; }
~myClass() { delete yc; yc = NULL; }
void setMyClass (yourClass * tyc) { delete yc; yc = tyc; }
void print () { yc->print(); }
};
int main()
{
yourClass b (//parameter);
myClass * a = new myClass();
a->print();
a->setMyClass(&b)
a->print();
delete a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
a的print()应该产生两种不同的打印,取决于//参数.
我认为你的等级是yc; 而不是yourClass*yc,但我想知道它是否可行.
编辑: 我以下面的方式重新编写代码,它的工作原理.仍然看起来很复杂,聪明的指针看起来很有希望,我仍然没有应用"三个规则".这里的代码.谢谢大家.
class …Run Code Online (Sandbox Code Playgroud) 我想存储10个Obj对象objList,但我不知道delete在这种情况下何时适当使用.如果我delete Obj;在下面代码中注明的行中使用,是否Obj仍然存储objList?
struct Obj {
int u;
int v;
};
vector<Obj> objList;
int main() {
for(int i = 0; i < 10; i++) {
Obj *obj = new Obj();
obj->u = i;
obj->v = i + 1;
objList.push_back(*obj);
// Should i use "delete Obj;" here?
}
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
C++:删除这个?
面向对象的自杀或删除此;
我正在通过阅读非常好的C++ Primer来学习C++,并且我正在学习C++如何通过delete像C这样的关键字释放内存free.Java和Pascal没有这种用于释放内存的机制.如果程序运行很长时间并且变量被破坏,那么它可能会导致程序错误,因此不应该忽略它.
简而言之,我想知道它是合法的还是可以推荐的,例如在C++中变量可以做this.delete()和删除自己.我们大多听说过在C和C++中释放指针,这是用新的free和delete关键字完成的.Pascal也有指针,但Java没有.所以在Java中它不应该是可能的,因为你没有明确删除对象,C没有对象,因此一个struct不能free为它分配即使因为C没有对象也不是技术上是可行的内存确实帕斯卡.
因此,我认为离开C++是为了我的问题,对象是否合法删除自己的类似的东西this.delete()?
我想在我的c ++应用程序中使用智能指针.
我应该包含哪个头文件来使用std scoped_ptr?
我有一个模板功能
template <class T>
void foo() {
// Within this function I need to create a new T
// with some parameters. Now the problem is I don't
// know the number of parameters needed for T (could be
// 2 or 3 or 4)
auto p = new T(...);
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?不知怎的,我记得看到函数输入像(...,...)?
我意识到错误来自于在自定义类中使用向量,但我一直在努力解决它们.如何在类对象的一部分调用向量方法?
这些是我得到的错误:
Word.cpp: In member function ‘void Word::addPosition(int)’:
Word.cpp:20: error: request for member ‘push_back’ in ‘((Word*)this)->Word::positions’, which is of non-class type ‘std::vector<int, std::allocator<int> >*’
Word.cpp: In member function ‘int Word::getPosition(int)’:
Word.cpp:26: error: request for member ‘size’ in ‘((Word*)this)->Word::positions’, which is of non-class type ‘std::vector<int, std::allocator<int> >*’
Word.cpp:27: error: request for member ‘size’ in ‘((Word*)this)->Word::positions’, which is of non-class type ‘std::vector<int, std::allocator<int> >*’
Word.cpp:29: error: cannot convert ‘std::vector<int, std::allocator<int> >’ to ‘int’ in return
Run Code Online (Sandbox Code Playgroud)
头
#pragma once
#include <string>
#include <vector> …Run Code Online (Sandbox Code Playgroud) 我需要创建一个返回指向int的指针的函数.
像这样:
int * count()
{
int myInt = 5;
int * const p = &myInt;
return p;
}
Run Code Online (Sandbox Code Playgroud)
由于指针只是一个地址,因此在调用此函数后,变量myInt将被销毁.如何在此方法中声明一个int,它将在内存中保留一个位置,以便我以后通过返回的指针访问它?我知道我可以在函数外部声明int,但是我想在函数内部声明它.
在此先感谢您的帮助!
下午好,我希望这里的人可以帮助我了解我所缺少的东西。我自由地承认这是一项家庭作业,但是我们被允许在代码上进行协作,因此希望这里有人愿意帮忙。
对于此程序,我需要使用递归和迭代来旋转C ++中的三个项目。我的递归案例没有任何问题,但迭代版本给我带来了很多麻烦。我尝试过的所有操作都会产生段错误或无限打印。这是代码,再次感谢您的帮助:
template<typename A, typename B, typename C>
class Triple{
public:
A first;
B second;
C third;
Triple(A a, B b, C c){ first = a; second = b; third = c;}
A fst(){return first;}
B snd(){return second;}
C thd(){return third;}
// The function change1(), changes the first component of a triple.
void change1(A a){ first = a;}
};
// A linked list of triples where the order of the triple rotates as it goes.
template<typename A, typename B, …Run Code Online (Sandbox Code Playgroud) 好.这是我的代码:
CShop::~CShop()
{
TPacketGCShop pack;
pack.header = HEADER_GC_SHOP;
pack.subheader = SHOP_SUBHEADER_GC_END;
pack.size = sizeof(TPacketGCShop);
Broadcast(&pack, sizeof(pack));
GuestMapType::iterator it;
it = m_map_guest.begin();
while (it != m_map_guest.end())
{
LPCHARACTER ch = it->first;
ch->SetShop(NULL);
++it;
}
M2_DELETE(m_pGrid);
}
Run Code Online (Sandbox Code Playgroud)
所以我有GuestMapType::iterator it;和这个it = m_map_guest.begin();
如果我像这样做我的功能,那很好吗?
CShop::~CShop()
{
TPacketGCShop pack;
pack.header = HEADER_GC_SHOP;
pack.subheader = SHOP_SUBHEADER_GC_END;
pack.size = sizeof(TPacketGCShop);
Broadcast(&pack, sizeof(pack));
auto it = m_map_guest.begin();
while (it != m_map_guest.end())
{
LPCHARACTER ch = it->first;
ch->SetShop(NULL);
++it;
}
M2_DELETE(m_pGrid);
}
Run Code Online (Sandbox Code Playgroud)
我删除了GuestMapType :: iterator; 简化我的代码?我的问题是.影响我的程序?有风险吗?
假设我有一个结构.
struct node {
string info;
node link*;
}
Run Code Online (Sandbox Code Playgroud)
有什么区别
node k;
node b;
k.info = "string";
b.info = "string";
k.link = &b;
Run Code Online (Sandbox Code Playgroud)
和
node *k;
node *b;
k = new node;
b = new node;
k->info = "string";
b->info = "string";
k->link = b;
Run Code Online (Sandbox Code Playgroud)
在内存分配方面?两个示例都是正确的并创建一个正确的链表?补充:在大多数书籍中使用了第二个例子,为什么?使用第一个例子有不足之处吗?