Questions tagged [rule-of-three]

The rule of three (also known as the Law of The Big Three or The Big Three) is a rule of thumb in C++ that claims that if a class defines one of the following it should probably explicitly define all three: destructor, copy constructor, assignment operator

65 questions
0
votes
1 answer

points not accessible after QVector insert function

I am trying to follow the rule of three, however doing this has cause a problem specifically with QVector's insert function. after adding a copy constructor and destructor, My program will produce an error where a vector of points are no longer…
Privatized
  • 73
  • 9
0
votes
1 answer

Why does deletion of Actor pointer result in "Program.exe has triggered a breakpoint"

I'm trying to create an Actor pointer that points to another Actor object, like so: Actor other = Actor(); Actor* ptr = &other; And then, when I try to delete ptr, it results in an runtime error: Program.exe has triggered a breakpoint But, when I…
Drin
  • 3
  • 3
0
votes
1 answer

heap corruption detected after normal block (#181)

(first of all sorry for English , cuz I'm not good at it) I have written a code for magic-square which works fine and gives the true square. When it is compiled and i run the program , there is nothing wrong. But when it reaches to the return 0 in…
Ali Samie
  • 145
  • 2
  • 11
0
votes
0 answers

how to write a class with its member is a vector of pointers to own defined class

I'm new to C++ and I'm trying to write a CarRental class which contains a vector of pointers to a base class Car. Here's the Car class. class Car{ public: Car():mPlate(""), mBrand(""){}//constructor Car(string p, string b): mPlate(p),…
Xiao Cui
  • 1
  • 2
0
votes
0 answers

Need advise for finding correct Implementation for WrapperInterface in c++ (rule of three)

I try to find a working pattern for an interface. Some information of what i'm doing. I'm implementing a rendering engine in dx11. My goal is to provide an easy and highly interfaced engine, where the client does not have to have any knowledge of…
kaiser
  • 940
  • 1
  • 10
  • 25
0
votes
0 answers

C++ vector lose heap allocated pointer

I wrote a simple program below for the explain. class A{ public: int *x; A(){ x = 0; } ~A(){ delete x; } void foo(){ x = new int(); *x = 99; } }; int main(){ std::vector as; for (int i = 0; i < 3;…
Hyunan Kwon
  • 545
  • 1
  • 4
  • 14
0
votes
1 answer

C++ Overloading assignment operator for dynamic multi dimensional array error

I'm having trouble in overloading the = operator with dynamic arrays. This is what I have so far. Also I know i havent wrote my destructor or constructor but I need to focus on this operator first: In my header file: #ifndef fasdf_dynn_h #define…
Brogrammer
  • 191
  • 1
  • 1
  • 10
0
votes
2 answers

Segmentation fault; rule of three

I have a header file like this #ifndef __coulomb_h_ #define __coulomb_h_ #include "nml_dcvector.h" #include #include #include #include using namespace std; class Coulomb{ public: typedef complex
0
votes
2 answers

Passing a class which holds Dynamic Memory : Methods and their Efficiencies

I have a class that holds some big amount of data, called HeavyData. This class Follows the rule of three (It has overridden the copy-constructor, copy-assignment operator and the destructor to be able to copy the member variable…
user1899812
0
votes
1 answer

Class members garbled after copy-construction or copy by assignment (sometimes)

My class NRRanNormal represents a normally-distributed random variable. By default, instances are normally distributed with mean 0 and stdev 1 (i.e., a standard normal random variable). Sometimes when I copy NRRanNormal objects, the mean and stdev…
synaptik
  • 8,971
  • 16
  • 71
  • 98
0
votes
1 answer

C++ : copy-and-swap idiom, alternative constructor

NB: This question follows a previous one, I hope it is okay to still ask it as a new question. I am trying to implement the "three and a half big rule" (copy-and-swap idiom) for a tree class, which looks like this: class Tree { friend void…
Seub
  • 2,451
  • 4
  • 25
  • 34
0
votes
1 answer

C++ Template Errors with different types

I need to find all the possible, at least three, errors from the following. template C myfunction(const A& a, const B& b) { if ( a < b ) return (C) a; else return (C) b; } My answer was typename A and B…
user2172254
0
votes
1 answer

Memory Management : character arrays and = operator

Memory Management : character arrays and = operator Q. In terms of Memory Management, What error would you have with the following code? class String { public: String(const char right[]); String& operator= (const String& right); int length()…
user2172254
0
votes
2 answers

C++ Destructors , dynamic allocation

I have recently had some errors (bad_alloc) due to my lack of a destructor. I currently have two classes, set up in this way: class ObjOne { friend class ObjTwo; public: //constructors and some other random methods ObjOne(int…
Dax Durax
  • 1,607
  • 5
  • 23
  • 31
-1
votes
1 answer

Destructor issues in a class with structs

I am currently facing issues while writing/utilizing a destructor for my Model class. For an OpenGL assignment I have to write a data structure which holds all the information regarding a model, see my Model.h implementation below. This class…
J. Doe
  • 101
  • 10