This question already has an answer here: When should I use the new keyword in C++? 11 answers Yes, when you initialize a local variable it gets memory from stack. But if you want to initialize some local variable and use heap memory then use concept of Dynamic Memory Allocation like this: int *foo; foo = new int [5]; This will initialize a int pointer in stack but it will point to some
这个问题在这里已经有了答案: 我应该什么时候在C ++中使用新的关键字? 11个答案 是的,当你初始化一个局部变量时,它从栈中获取内存。 但是,如果你想初始化一些本地变量并使用堆内存,那么使用如下Dynamic Memory Allocation概念: int *foo; foo = new int [5]; 这将在堆栈中初始化一个int pointer ,但它将指向heap某个内存位置,您可以使用它来存储您的foo数组。 检查了解更多的理解。
Possible Duplicate: When should I use the new keyword in C++? I don't know the difference between case 1 and case 2: I define a struct below: struct Graph { int ID; } Case 1: Graph g; g.ID = 1; case 2: Graph* g = new G(); g.ID = 1; Are these two cases the same? What's the difference? In case 1, the memory used by g is allocated on the stack, which means that it will be
可能重复: 我应该什么时候在C ++中使用新的关键字? 我不知道案例1和案例2的区别: 我在下面定义一个结构体: struct Graph { int ID; } 情况1: Graph g; g.ID = 1; 情况2: Graph* g = new G(); g.ID = 1; 这两种情况是一样的吗? 有什么不同? 在情况1中,g所使用的内存被分配到堆栈中,这意味着当它所在的函数返回时它将被自动释放。 在情况2中,g使用的内存分配在堆上,这意味着只有在使用delete操作
Possible Duplicates: When to use “new” and when not to, in C++? When should I use the new keyword in C++? What is the difference between A a; and A a = new A(); ? Edited : A* a = new A(); My mistake. When inside a function, A a declares a variable on the stack and calls A's default constructor* on a. This variable is automatically cleaned up when the variable goes out of scop
可能重复: 何时使用“新”,何时不使用C ++? 我应该什么时候在C ++中使用新的关键字? A a;什么区别A a; 和A a = new A(); ? 编辑 : A* a = new A(); 我的错。 当在一个函数内部时, A a 在栈上声明一个变量并在a上调用A的默认构造函数*。 该变量在变量超出范围时自动清除。 A a = new A(); 但是,不会编译 A* a = new A(); 在堆上创建一个新的A,并在新分配的内存上调用A的默认构造函数*。 然后,表
Possible Duplicates: When to use “new” and when not to, in C++? When should I use the new keyword in C++? It seems like I could program something without ever using the word new , and I would never have to worry about deleting anything either, so why should I ever call it? From what I understand, it's because I would run out of stack memory. Is this correct? I guess my main questio
可能重复: 何时使用“新”,何时不使用C ++? 我应该什么时候在C ++中使用新的关键字? 似乎我可以编程一些东西,而不用使用new这个词,而且我也不必担心删除任何东西,为什么我要称它? 据我所知,这是因为我会用尽堆栈内存。 它是否正确? 我想我的主要问题是,什么时候应该叫new ? 这是一个对象生命周期的问题:如果你堆栈分配你的对象,当这些对象超出作用域时(比如说在方法结束时),对象析构函数将被调用。
I am using non-native QFileDialog (for choosing directory path) and I need to add some custom drives. I don't even need to display any content inside of these drives for now, I just need to display these drives on the top level (and preferably with my icon) and output some special string in the result when user selects it. What is the easiest way to implement this? I have read in the doc
我正在使用非本地QFileDialog (用于选择目录路径),我需要添加一些自定义驱动器。 我甚至不需要在这些驱动器中显示任何内容,我只需要在顶层显示这些驱动器(最好是使用我的图标),并在用户选择它时在结果中输出一些特殊的字符串。 实现这个最简单的方法是什么? 我已经阅读过可以使用代理模型的文档,但我不明白如何实现这种模型,所有示例仅显示对已有项目进行过滤和排序。 如果我理解正确,你想添加额外的驱动器到
I get the following compiler error when I used non-static function pointer in the constructor argument at myclass_2, void load (void) method. Reference to non-static member function must be called If I change the function to static void load (void) no error. but I need to have non-static function, I have to work on some global variable in load function. Any help would be appreciate it. cla
当我在myclass_2的void load(void)方法的构造函数参数中使用非静态函数指针时,出现以下编译器错误。 必须调用对非静态成员函数的引用 如果我将函数更改为static void load(void),则不会出现错误。 但我需要有非静态函数,我必须在加载函数中处理一些全局变量。 任何帮助将不胜感激。 class myclass{ public: //_load_event_handler vector<Function> _handler_one; vector<Function> _handl
I'm writing RTSP client and after creating it with class RtspClientManager { private: rtsp_client; void continueAfterDescribe(RTSPClient* rtspClient, int resultCode, char* resultString); } ... rtsp_client = RTSPClient::createNew(*env, szUrl); i'm sending describe command: rtsp_client->sendDescribeCommand(continueAfterDescribe); I would like to have a continueAfterDescribe
我正在编写RTSP客户端,并在创建之后 class RtspClientManager { private: rtsp_client; void continueAfterDescribe(RTSPClient* rtspClient, int resultCode, char* resultString); } ... rtsp_client = RTSPClient::createNew(*env, szUrl); 我正在发送描述命令: rtsp_client->sendDescribeCommand(continueAfterDescribe); 我想有一个continueAfterDescribe作为RtspClientManager::continueAfterDescribe
I'd like to access non-static class members out of a static method. It is also defined which instance of the class should be used for the access. It is the instance, that has been created most recently. I try to do this by using a static pointer as a member of that class, which indicates, what was the object inastatiated most recently. It is intended that the class constructor sets this
我想从静态方法中访问非静态类成员。 它还定义了该类的哪个实例应该用于访问。 这是最近创建的实例。 我试图通过使用静态指针作为该类的成员来完成此操作,该指针指示最近传播的对象是什么。 每当我们安装一个对象时,类的构造函数都会设置这个指针。 代码如下所示: class Klasse { public: Klasse() { me = this; //an error is thown here:"undefined reference to `Klasse::me'" nummer = eins; } private:
I have a class with a static vector of pointers to all instances of the class. When I access the member variables through a static getter method, I sometimes get wrong results. Code: hpp-file: class ObjectID { public: ObjectID(); float getShininess() const { return m_shininess; } static const std::shared_ptr<ObjectID> getID(unsigned long); private:
我有一个类的静态向量指向类的所有实例的类。 当我通过静态getter方法访问成员变量时,我有时会得到错误的结果。 码: HPP文件: class ObjectID { public: ObjectID(); float getShininess() const { return m_shininess; } static const std::shared_ptr<ObjectID> getID(unsigned long); private: float m_shininess; static std::vector<std::shared_ptr<
I have a ClassB which have a thread object and a function defined as: pthread_t m_thWorkThread; void* ThreadProc(void *); And in the constructor of ClassB, I create a thread like: pthread_create(&m_thWorkThread, NULL, &(ClassB::ThreadProc), this); However I got two error messages: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function
我有一个ClassB有一个线程对象和一个函数定义为: pthread_t m_thWorkThread; void* ThreadProc(void *); 而在ClassB的构造函数中,我创建了一个如下所示的线程: pthread_create(&m_thWorkThread, NULL, &(ClassB::ThreadProc), this); 但是我收到了两条错误消息: 错误:ISO C ++禁止将非限定或带括号的非静态成员函数的地址形成指向成员函数的指针。 无法将*(ClassB ::)(void)“void*()(void)â作为