c++ destructor didn't get called? - c++

I have a class:
class Rectangle {
int width;
int height;
public:
Rectangle(int w, int h) {
width = w;
height = h;
cout << "Constructing " << width << " by " << height << " rectangle.\n";
}
~Rectangle() {
cout << "Destructing " << width << " by " << height << " rectangle.\n";
}
int area() {
return width * height;
}
};
int main()
{
Rectangle *p;
try {
p = new Rectangle(10, 8);
} catch (bad_alloc xa) {
cout << "Allocation Failure\n";
return 1;
}
cout << "Area is " << p->area();
delete p;
return 0;
}
This is a quite simple C++ sample. I compiled in Linux g++ and run it.
Suddenly I found the delete p did not call ~Rectangle() ...
I should see string like "Destructing " << width << " by " << height << " rectangle."
but I did not ....
but why?
Deleting an object should call that object's destructor, shouldn't it?

You haven't ended the line, so the line was not output. Add << endl to your printing.

Related

How to know when a destructor is called? in c++

#include<iostream>
using namespace std;
class Point {
private:
int x;
int y;
static int numCreatedObjects;
public:
Point() :x(0), y(0) {
numCreatedObjects++;
}
Point(int _x, int _y) {
x = _x;
y = _y;
numCreatedObjects++;
}
~Point() {
cout << "Destructed..." << endl;
}
void setXY(int _x, int _y) {
this->x= _x;
this->y = _y;
}
int getX() const { return x; }
int getY() const { return y; }
Point operator+(Point& pt2) {
Point result(this->x + pt2.x, this->y + pt2.y);
return result;
}
Point& operator = (Point& pt) {
this->x = pt.x;
this->y = pt.y;
}
static int getNumCreatedObject() { return numCreatedObjects; }
friend void print(const Point& pt);
friend ostream& operator<<(ostream& cout, Point& pt);
friend class SpyPoint;
};
int Point::numCreatedObjects = 0;
void print(const Point& pt) {
cout << pt.x << "," << pt.y << endl;
}
ostream& operator<<(ostream& cout, Point& pt) {
cout << pt.x << "," << pt.y;
return cout;
}
class SpyPoint{
public:
void hack_point_info(const Point& pt) {
cout << "Hacked by SpyPoint" << endl;
cout <<"x: "<<pt.x <<endl<<"y: "<<pt.y << endl;
cout << "numCreatedObj:" << pt.getNumCreatedObject() << endl;
}
};
int main() {
Point pt1(1, 2);
cout << "pt1 : ";
print(pt1);
cout << endl;
Point* pPt1 = &pt1;
cout << "pt1 : ";
cout << pPt1->getX() << ", " << pPt1->getY() << endl;
cout << "pt1 : ";
cout << pPt1->getX() << ", " << pPt1->getY() << endl;
cout << endl;
Point* pPt2 = new Point(10, 20);
cout << "pt2 : ";
cout << pPt2->getX() << ", " << pPt2->getY() << endl;
cout << endl;
delete pPt2;
cout << "pt1 NumCreatedObject : ";
cout << pt1.getNumCreatedObject() << endl;
Point pt2(10, 20);
Point pt3(30, 40);
Point pt4 = pt2 + pt3;
cout << "pt2 : ";
cout << pt2 << endl;
cout << "pt3 : ";
cout << pt3 << endl;
cout << "pt4 : ";
cout << pt4 << endl;
cout << "pt1 NumCreatedObject : ";
cout << pt1.getNumCreatedObject() << endl << endl;
Point* ptAry = new Point[5];
cout << "pt2 NumCreatedObject : ";
cout << pt2.getNumCreatedObject() << endl;
cout << endl;
delete[] ptAry;
cout << endl;
SpyPoint spy;
cout << "pt1 info" << endl;
spy.hack_point_info(pt1);
cout << endl;
cout << "pt4 info" << endl;
spy.hack_point_info(pt4);
cout << endl;
return 0;
}
I don't know why the yellow underlined part prints out. I understand that the destructor is called when returning, unless it is dynamically allocated.
I guess pt1 is going to disappear, but I don't know why.
Also, I would like to know if there is a method that can be forcibly destroyed even if it is not dynamically allocated.

How to pass a pointer to a structure to the constructor?

I want to pass the pointer to a structure(variables of this structure is an array with elements that have value x and y) to the constructor. Next I want to assign values x and y of each variable of this structure to the similar variable values of the structure in the class.
class Convex_quadrliteral
{
protected:
struct VC {
float x, y;
} vertice_coordinate[4];
public:
Convex_quadrliteral (VC *pointerVC);
};
Convex_quadrliteral::Convex_quadrliteral (VC *pointerVC) {
cout << "\nObject is being created" << endl;
for (int i = 0; i < 4; i++) //variable initialisation
{
vertice_coordinate[i].x = pointerVC[i].x;
vertice_coordinate[i].y = pointerVC[i].y;
}
//object's properties output
cout << "Properties: " << endl
<< "A (" << vertice_coordinate[0].x << ", " << vertice_coordinate[0].y << ")" << endl
<< "B (" << vertice_coordinate[1].x << ", " << vertice_coordinate[1].y << ")" << endl
<< "C (" << vertice_coordinate[2].x << ", " << vertice_coordinate[2].y << ")" << endl
<< "D (" << vertice_coordinate[3].x << ", " << vertice_coordinate[3].y << ")" << endl;
}
int main()
{
struct vertice_coordinate
{
float x, y;
};
vertice_coordinate *pointerVC = new vertice_coordinate[4];
for (int i = 0; i < 4; i++) {
pointerVC[i].x = 2;
pointerVC[i].y = 2;
}
Convex_quadrliteral figure_1(pointerVC);
I expect the output:
A(2, 2)
B(2, 2)
C(2, 2)
D(2, 2)
The output is error: no declaration matches 'Convex_quadrliteral::Convex_quadrliteral(Convex_quadrliteral::VC*)'C onvex_quadrliteral::Convex_quadrliteral (VC *pointerVC)
You re-define your VC struct, and even though the struct looks identical, the compiler will treat them as two different types. Define one struct and use it in both your class and in main.

" invalid operands of types 'int' and 'int* const'" error from function, from book

I am going through the book "Beginning C++ Through Game Programming". I have typed this supplied script perfectly (I even pasted over it with the script supplied from online download- and used undo/redo to compare every character- to find no difference- except line endings and actually capitalizing the first character of function names). Despite no change, the supplied script compiles perfectly fine, but mine does not (unless I copy/paste supplied script). I am given the error in the second function: GoodSwap()
#include<iostream>
using namespace std;
void BadSwap(int x, int y);
void GoodSwap(int* const pX, int* const pY);
int main(){
int myScore = 150;
int yourScore = 1000;
cout << "Original Values" << endl;
cout << "myScore : " << myScore << endl;
cout << "yourScore: " << yourScore << endl;
cout << "Calling BadSwap()" << endl;
BadSwap(myScore, yourScore);
cout << "myScore : " << myScore << endl;
cout << "yourScore: " << yourScore << endl;
cout << "Calling GoodSwap()" << endl;
GoodSwap(&myScore, &yourScore);
cout << "myScore : " << myScore << endl;
cout << "yourScore: " << yourScore << endl;
return 0;
}
void BadSwap(int x, int y){
int temp = x;
x = y;
y = temp;
}
void GoodSwap(int* const pX, int* const pY){
int temp = *pX
*pX = *pY;
*pY = temp;
}
You missed a ; at first line of GoodSwap().
int temp = *pX // There should be a `;`
After this change, your program can compile in VS2015.

Initialize member variables with multiple constructor calls

I'm trying to execute the following code:
#include <iostream>
using namespace std;
class ABC {
private:
int x, y;
public:
ABC(){
cout << "Default constructor called!" << endl;
ABC(2, 3);
cout << x << " " << y << endl;
}
ABC(int i, int j){
cout << "Parameterized constructor called with parameters "<< i << " " << j << "!" << endl;
x = i;
y = j;
cout << x << " " << y << endl;
}
};
int main(){
ABC a;
return 0;
}
I am getting the following output:
Default constructor called!
Parameterized constructor called with parameters 2 3!
2 3
-858993460 -858993460
Shouldn't the member variables be initialized with values 2 and 3?
ABC(2, 3); doesn't call the constructor to initialize the members, it just create a temporary variable which will be destroyed immediately.
If you meant delegating constructor you should:
ABC() : ABC(2, 3) {
cout << "Default constructor called!" << endl;
cout << x << " " << y << endl;
}
Note this is a C++11 feature. You can add a member function if you can't use C++11.
class ABC {
private:
int x, y;
init(int i, int j) {
x = i;
y = j;
}
public:
ABC(){
cout << "Default constructor called!" << endl;
init(2, 3);
cout << x << " " << y << endl;
}
ABC(int i, int j){
cout << "Parameterized constructor called with parameters "<< i << " " << j << "!" << endl;
init(i, j);
cout << x << " " << y << endl;
}
};
You create a temporary variable in ABC() body. You can use this syntax to overcome this:
class ABC
{
private:
int x, y;
public:
ABC() : ABC(2,3)
{
std::cout << "Default constructor called!" << std::endl;
}
ABC(int i, int j)
{
std::cout << "Parameterized constructor called with parameters "<< i << " " << j << "!" << std::endl;
x = i;
y = j;
std::cout << x << " " << y << std::endl;
}
};

error : expression must have class type

I have a problem with the class in C++; it's written like this.
in second source,
void idealtype::compare(idealtype T1)
{
if (height.size() > T1.height.size())
cout << T1.getname() << " " << T1.getage() << "\t" << T1.getheight() << "\n";
else if (height.size() < T1.height.size())
cout << getname() << " " << getage() << "\t" << getheight() << "\n";
else if (height.size() == T1.height.size())
{
cout << T1.getname() << " " << T1.getage() << "\t" << T1.getheight() << "\n";
cout << getname() << " " << getage() << "\t" << getheight() << "\n";
}
cout << "\n";
}
in header;
class idealtype
{public:
void compare(idealtype);
....
private:
int height;
}
in main source;
....
idealtype A(a,b,c) // c is "height"
....
idealtype B(a,b,c) // c is "height"
B.compare(A)
I think it's all done well, but Visual keeps showing me,
(in second source, on every if() state) error : expression must have class type
So, what's the KEY of this problem?
Plz help me, guys :)
So, what's the KEY of this problem?
In your code you say
if (height.size() > T1.height.size())
class idealtype {
// ...
private:
int height; // <<<<<<<<<<<<
};
since height is declared as int it doesn't have any class like methods. That's why the compiler complains.