473,749 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

destructor invoked without constructor.

Hi Everyone,

I have the following unit to explain the problem that i have,

class sample
{
public : sample()
{
printf("in sample...\n");
}
~sample()
{
printf("out sample...\n");
}
void invoke(sample obj)
{
printf("in invoke...\n");
}
};

int main()

{

sample obj;
sample obj1;
obj1.invoke(obj );
printf("...end. ..\n");
return 0;

}
The following is the output,

in sample...
in sample...
in invoke...
out sample...
....end...
out sample...
out sample...

i think the first "out sample..." is for the object obj passed by
value, which indicates that a new temp object has been destroyed,
which means that it should have been created, if so, shouldn't the "in
sample" have been printed once more before "in invoke"...?

May 4 '07 #1
13 2384
On 4 Maj, 11:03, sam_...@yahoo.c o.in wrote:
Hi Everyone,

I have the following unit to explain the problem that i have,

class sample
{
public : sample()
{
printf("in sample...\n");
}
~sample()
{
printf("out sample...\n");
}
void invoke(sample obj)
{
printf("in invoke...\n");
}
};

int main()

{

sample obj;
sample obj1;
obj1.invoke(obj );
printf("...end. ..\n");
return 0;

}

The following is the output,

in sample...
in sample...
in invoke...
out sample...
...end...
out sample...
out sample...

i think the first "out sample..." is for the object obj passed by
value, which indicates that a new temp object has been destroyed,
which means that it should have been created, if so, shouldn't the "in
sample" have been printed once more before "in invoke"...?
It is created, but using the copy-constructor, which you have not
implemented so the compiler-generated one is used, and that one does
not print anything. Add

sample(const sample&) { std::cout << "copy\n"; }

to your class and see what happens.

--
Erik Wikström

May 4 '07 #2
<sa*****@yahoo. co.inwrote in message
news:11******** **************@ l77g2000hsb.goo glegroups.com.. .
Hi Everyone,

I have the following unit to explain the problem that i have,

class sample
{
public : sample()
{
printf("in sample...\n");
}
~sample()
{
printf("out sample...\n");
}
void invoke(sample obj)
{
printf("in invoke...\n");
}
};

int main()

{

sample obj;
sample obj1;
obj1.invoke(obj );
printf("...end. ..\n");
return 0;

}
The following is the output,

in sample...
in sample...
in invoke...
out sample...
...end...
out sample...
out sample...

i think the first "out sample..." is for the object obj passed by
value, which indicates that a new temp object has been destroyed,
which means that it should have been created, if so, shouldn't the "in
sample" have been printed once more before "in invoke"...?
No. The temporary is created using the copy constructor, and since you
didn't supply any it was automatically generated (and obviously not with the
printf statement in it). Add the following constructor and it should work as
you expect:

sample(const sample & s)
{
printf("in sample... (copy ctor)\n");
}

Btw, it might be handy to output the this pointer as well so you can exactly
see what ctor-dtor pairs belong to eachother :)

- Sylvester
May 4 '07 #3
On May 4, 2:03 pm, sam_...@yahoo.c o.in wrote:
Hi Everyone,

I have the following unit to explain the problem that i have,

class sample
{
public : sample()
{
printf("in sample...\n");
}
~sample()
{
printf("out sample...\n");
}
void invoke(sample obj)
{
printf("in invoke...\n");
}
};

int main()

{

sample obj;
sample obj1;
obj1.invoke(obj );
printf("...end. ..\n");
return 0;

}

The following is the output,

in sample...
in sample...
in invoke...
out sample...
...end...
out sample...
out sample...

i think the first "out sample..." is for the object obj passed by
value, which indicates that a new temp object has been destroyed,
which means that it should have been created, if so, shouldn't the "in
sample" have been printed once more before "in invoke"...?
Invoking a function with pass by value argument would definitely call
copy constructor, whereas pass the argument by reference wouldn't
create an object.
So If you would've defined a copy constructor for the class sample,
within which a print statement your doubt would've got cleared.

I hope this helps.

-
Sukumar R

May 4 '07 #4
On May 4, 2:21 pm, WittyGuy <wittyguys...@g mail.comwrote:
On May 4, 2:03 pm, sam_...@yahoo.c o.in wrote:


Hi Everyone,
I have the following unit to explain the problem that i have,
class sample
{
public : sample()
{
printf("in sample...\n");
}
~sample()
{
printf("out sample...\n");
}
void invoke(sample obj)
{
printf("in invoke...\n");
}
};
int main()
{
sample obj;
sample obj1;
obj1.invoke(obj );
printf("...end. ..\n");
return 0;
}
The following is the output,
in sample...
in sample...
in invoke...
out sample...
...end...
out sample...
out sample...
i think the first "out sample..." is for the object obj passed by
value, which indicates that a new temp object has been destroyed,
which means that it should have been created, if so, shouldn't the "in
sample" have been printed once more before "in invoke"...?

Invoking a function with pass by value argument would definitely call
copy constructor, whereas pass the argument by reference wouldn't
create an object.
So If you would've defined a copy constructor for the class sample,
within which a print statement your doubt would've got cleared.

I hope this helps.

-
Sukumar R- Hide quoted text -

- Show quoted text -
So it is correct to understand that the default copy constructor would
be invoked in all calls to member functions where the object of the
same class's type is passed by value?
....

Thanks in advance!!!

May 4 '07 #5
On May 4, 2:14 pm, Erik Wikström <eri...@student .chalmers.sewro te:
On 4 Maj, 11:03, sam_...@yahoo.c o.in wrote:


Hi Everyone,
I have the following unit to explain the problem that i have,
class sample
{
public : sample()
{
printf("in sample...\n");
}
~sample()
{
printf("out sample...\n");
}
void invoke(sample obj)
{
printf("in invoke...\n");
}
};
int main()
{
sample obj;
sample obj1;
obj1.invoke(obj );
printf("...end. ..\n");
return 0;
}
The following is the output,
in sample...
in sample...
in invoke...
out sample...
...end...
out sample...
out sample...
i think the first "out sample..." is for the object obj passed by
value, which indicates that a new temp object has been destroyed,
which means that it should have been created, if so, shouldn't the "in
sample" have been printed once more before "in invoke"...?

It is created, but using the copy-constructor, which you have not
implemented so the compiler-generated one is used, and that one does
not print anything. Add

sample(const sample&) { std::cout << "copy\n"; }

to your class and see what happens.

--
Erik Wikström- Hide quoted text -

- Show quoted text -
And what is the reason that only copy constructor is invoked in this
case and why not the regular constructor?

May 4 '07 #6
sa*****@yahoo.c o.in wrote:
....
So it is correct to understand that the default copy constructor would
be invoked in all calls to member functions where the object of the
same class's type is passed by value?
Remove the word "default" and then you are correct.

The compiler provides a "default" copy constructor (if one is needed).
The programmer can provide a copy constructor, in which case the
compiler does not create a "default" one. Either way, if you pass by
value, the copy constructor is invoked.
May 4 '07 #7
sa*****@yahoo.c o.in wrote:
....
And what is the reason that only copy constructor is invoked in this
case and why not the regular constructor?
Because it is defined that way in the standard. Passing by value by
definition requires the compiler to (appear to) create a temporary copy
of the object being passed. New copies of objects are created using the
copy constructor, almost by definition.
May 4 '07 #8

<sa*****@yahoo. co.inwrote in message
news:11******** **************@ c35g2000hsg.goo glegroups.com.. .
On May 4, 2:14 pm, Erik Wikström <eri...@student .chalmers.sewro te:
On 4 Maj, 11:03, sam_...@yahoo.c o.in wrote:

It is created, but using the copy-constructor, which you have not
implemented so the compiler-generated one is used, and that one does
not print anything. Add

sample(const sample&) { std::cout << "copy\n"; }

to your class and see what happens.
And what is the reason that only copy constructor is invoked in this
case and why not the regular constructor?
Because you're creating a copy, obviously. That's what the copy constructor
is for. You're not creating a new object using no arguments. How else would
this work:
#include <iostream>

struct sample
{
int i;

sample() : i(0) { }
sample(int _i) : i(_i) { }
};

void foo(sample s)
{
std::cout << s.i << std::endl;
}

int main()
{
sample s(23);
foo(s);
}

If the temporary passed to foo() were to be default-constructed, you are
never able to pass the value 23 to foo. The copy-ctor is invoked to
construct a new sample that is a copy from the sample passed to foo(), so
you want the value 23 to be copied. This is the responsibility from the copy
constructor.

- Sylvester Hesp
May 4 '07 #9
On May 4, 2:59 pm, "Sylvester Hesp" <s.h...@oisyn.n lwrote:
<sam_...@yahoo. co.inwrote in message

news:11******** **************@ c35g2000hsg.goo glegroups.com.. .
On May 4, 2:14 pm, Erik Wikström <eri...@student .chalmers.sewro te:
On 4 Maj, 11:03, sam_...@yahoo.c o.in wrote:
It is created, but using the copy-constructor, which you have not
implemented so the compiler-generated one is used, and that one does
not print anything. Add
sample(const sample&) { std::cout << "copy\n"; }
to your class and see what happens.
And what is the reason that only copy constructor is invoked in this
case and why not the regular constructor?

Because you're creating a copy, obviously. That's what the copy constructor
is for. You're not creating a new object using no arguments. How else would
this work:

#include <iostream>

struct sample
{
int i;

sample() : i(0) { }
sample(int _i) : i(_i) { }

};

void foo(sample s)
{
std::cout << s.i << std::endl;

}

int main()
{
sample s(23);
foo(s);

}

If the temporary passed to foo() were to be default-constructed, you are
never able to pass the value 23 to foo. The copy-ctor is invoked to
construct a new sample that is a copy from the sample passed to foo(), so
you want the value 23 to be copied. This is the responsibility from the copy
constructor.

- Sylvester Hesp
Thanks you very much. I understand the purpose of copy constructors
being used i would assume they are invoked for the following case too,

foo(s(23));

isn't it?

I also tried to define a constructor like

sample(sample obj) // i get an error saying first parameter
shouldn't be of type sample but the following works fine
sample(int i, sample obj)

why doesn't the first work? I pass an already existing object to the
constructor, a new temp object would be created with the help of copy
constructor and i'm using its value to create my object, what is wrong
in this scenario?

Thanks in advance!!!

May 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3222
by: Charles Jamieson | last post by:
I declare a class class myClass{ public: ~myClass();
7
1897
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
8
3266
by: ctick | last post by:
When defining a clas and no constructor and destructor provided, compiler generates both. What're the need for this since they do nothing as to constructing/destructing an obejct. What's happening in constructor/destructor if they both are defaulted and empty? Thanks!
23
5180
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
5
1727
by: manitu | last post by:
Hello all, I am writing a kind of encapsulation of SQL statements in C++ as a class. I am encountering problems that the destructor of my class never gets called. The class should be used as a kind of hierarchical representation of the different SQL operations such as AND, OR and possible values. I will post some abbreviated / shortened / simplified (partiyll
26
541
by: Prawit Chaivong | last post by:
Hi All, There is code here. ------------------------------------------------------------------ class Base{ public: Base(){} virtual ~Base(){} private: int a;
5
3343
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
12
7209
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? class Trial { public: Trial() {
7
4293
by: Rahul | last post by:
Hi Everyone, I was trying to implement a final class and i start having the destructor of the final class as private, class A { ~A() { printf("destructor invoked\n");
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.