rule-of-three

Must a c++ interface obey the rule of five?

别来无恙 提交于 2019-12-03 10:39:28
What is the correct way to declare instantiation methods when defining an interface class? Abstract base classes are required to have a virtual destructor for obvious reasons. However, the following compilation warning is then given: "'InterfaceClass' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator", which is the 'rule of five'. I understand why the 'rule of five' should be obeyed in general, but is it still applicable for an abstract base class or interface? My implimentation is then: class

Default constructor missing - but I'm not calling it?

大兔子大兔子 提交于 2019-11-29 17:07:01
I'm writing a C++ application in which I have a Controller class with two nested structs, defined in my header file as follows: class Controller { struct help_message { // controller.hpp, line 19 std::string summary; std::string details; help_message(const std::string&, const std::string&); }; struct player_command { cmd_t cmd; help_message help; // cmd_t is my own typedef, irrelevant for this question player_command(const cmd_t&, const help_message&); }; // more members... }; In my source file, I have this: Controller::player_command::player_command(const Controller::cmd_t& c, const help

C++ Copy Constructor + Pointer Object

好久不见. 提交于 2019-11-29 02:13:09
I'm trying to learn "big three" in C++.. I managed to do very simple program for "big three".. but I'm not sure how to use the object pointer.. The following is my first attempt. I have a doubt when I was writing this... Questions Is this the correct way to implement the default constructor? I'm not sure whether I need to have it or not. But what I found in another thread about copy constructor with pointer is that I need to allocate the space for that pointer before copying the address in copy constructor.. How to assign the pointer variable in the copy constructor? The way that I wrote in

Rule of Three in C++

泪湿孤枕 提交于 2019-11-28 02:07:16
I've read that The Rule of Three, What is The Rule of Three? is summarized as follows: If you need to explicitly declare either the destructor, copy constructor or copy assignment operator yourself, you probably need to explicitly declare all three of them. My question is: In a C++ application, I have a class that manages resources (has a destructor that handles deleting pointers). I know that the application uses assignment operator all over the place, but I am absolutely certain that there is no usage in the application of a copy constructor, i.e., usage of the type Class c(..); Class d(c);

Understanding -Weffc++

强颜欢笑 提交于 2019-11-27 20:13:51
Consider the following program: #include <string> struct S { S (){} private: void *ptr = nullptr; std::string str = ""; }; int main(){} This, when compiled with -Weffc++ on GCC 4.7.1, will spit out: warning: 'struct S' has pointer data members [-Weffc++] warning: but does not override 'S(const S&)' [-Weffc++] warning: or 'operator=(const S&)' [-Weffc++] That's no problem normally, except for a couple things with this example: If I comment out any of the constructor, the pointer declaration, or the string declaration, the warning disappears. This is odd because you'd think the pointer alone

Rule of Three in C++

核能气质少年 提交于 2019-11-27 04:51:31
问题 I've read that The Rule of Three, What is The Rule of Three? is summarized as follows: If you need to explicitly declare either the destructor, copy constructor or copy assignment operator yourself, you probably need to explicitly declare all three of them. My question is: In a C++ application, I have a class that manages resources (has a destructor that handles deleting pointers). I know that the application uses assignment operator all over the place, but I am absolutely certain that there

Understanding -Weffc++

房东的猫 提交于 2019-11-26 16:17:51
问题 Consider the following program: #include <string> struct S { S (){} private: void *ptr = nullptr; std::string str = ""; }; int main(){} This, when compiled with -Weffc++ on GCC 4.7.1, will spit out: warning: 'struct S' has pointer data members [-Weffc++] warning: but does not override 'S(const S&)' [-Weffc++] warning: or 'operator=(const S&)' [-Weffc++] That's no problem normally, except for a couple things with this example: If I comment out any of the constructor, the pointer declaration,

What is The Rule of Three?

99封情书 提交于 2019-11-25 22:50:37
问题 What does copying an object mean? What are the copy constructor and the copy assignment operator ? When do I need to declare them myself? How can I prevent my objects from being copied? 回答1: Introduction C++ treats variables of user-defined types with value semantics . This means that objects are implicitly copied in various contexts, and we should understand what "copying an object" actually means. Let us consider a simple example: class person { std::string name; int age; public: person

Rule-of-Three becomes Rule-of-Five with C++11?

守給你的承諾、 提交于 2019-11-25 22:13:58
问题 So, after watching this wonderful lecture on rvalue references, I thought that every class would benefit of such a \"move constructor\", template<class T> MyClass(T&& other) edit and of course a \"move assignment operator\", template<class T> MyClass& operator=(T&& other) as Philipp points out in his answer, if it has dynamically allocated members, or generally stores pointers. Just like you should have a copy-ctor, assignment operator and destructor if the points mentioned before apply.