473,320 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Explicitly calling a constructor/destructor

464 Expert 256MB
After i create an object can i call the constructor again to reinitialize that objects variables? I know i can create a function to do this, but i was wonder if i can just call the constructor again. If i can call the constructor again is this a good practice or should i avoid such a use of the constructor. Here is some test code.
Expand|Select|Wrap|Line Numbers
  1. //code created and tested in VS 2003
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class TestClass
  7. {
  8. public:
  9.     TestClass();
  10.     ~TestClass();
  11.     void ModifyVariables(int, int);
  12.     void PrintVariables();
  13. private:
  14.     int Variable1;
  15.     int Variable2;
  16. };
  17.  
  18. TestClass::TestClass()
  19. {
  20.     Variable1 = 100;
  21.     Variable2 = 100;
  22. }
  23.  
  24. TestClass::~TestClass()
  25. {
  26.     Variable1 = 0;
  27.     Variable2 = 0;
  28. }
  29.  
  30. void TestClass::ModifyVariables(int Mod1, int Mod2)
  31. {
  32.     Variable1 += Mod1;
  33.     Variable2 += Mod2;
  34. }
  35.  
  36. void TestClass::PrintVariables()
  37. {
  38.     cout<<Variable1<<" "<<Variable2<<endl;
  39. }
  40.  
  41. int main()
  42. {
  43.     TestClass TestObject;
  44.     TestObject.PrintVariables();
  45.     TestObject.ModifyVariables(20,45);
  46.     TestObject.PrintVariables();
  47.     //TestObject.TestClass(); //can't do
  48.     TestObject.~TestClass(); //works just fine.
  49.     TestObject.PrintVariables();
  50.     TestObject.ModifyVariables(120, 145);
  51.     TestObject.PrintVariables();
  52.  
  53.     return 0;
  54.  
  55. /* OUPUT:
  56. 100 100
  57. 120 145
  58. 0 0
  59. 120 145
  60. */
  61. }
  62.  
why am i able to call the destructor like that and not the constructor?
How can i call the constructor to reinit the variables to 100? or do i have to write a separate function?
Feb 19 '08 #1
2 3188
gpraghuram
1,275 Expert 1GB
After i create an object can i call the constructor again to reinitialize that objects variables? I know i can create a function to do this, but i was wonder if i can just call the constructor again. If i can call the constructor again is this a good practice or should i avoid such a use of the constructor. Here is some test code.
Expand|Select|Wrap|Line Numbers
  1. //code created and tested in VS 2003
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class TestClass
  7. {
  8. public:
  9.     TestClass();
  10.     ~TestClass();
  11.     void ModifyVariables(int, int);
  12.     void PrintVariables();
  13. private:
  14.     int Variable1;
  15.     int Variable2;
  16. };
  17.  
  18. TestClass::TestClass()
  19. {
  20.     Variable1 = 100;
  21.     Variable2 = 100;
  22. }
  23.  
  24. TestClass::~TestClass()
  25. {
  26.     Variable1 = 0;
  27.     Variable2 = 0;
  28. }
  29.  
  30. void TestClass::ModifyVariables(int Mod1, int Mod2)
  31. {
  32.     Variable1 += Mod1;
  33.     Variable2 += Mod2;
  34. }
  35.  
  36. void TestClass::PrintVariables()
  37. {
  38.     cout<<Variable1<<" "<<Variable2<<endl;
  39. }
  40.  
  41. int main()
  42. {
  43.     TestClass TestObject;
  44.     TestObject.PrintVariables();
  45.     TestObject.ModifyVariables(20,45);
  46.     TestObject.PrintVariables();
  47.     //TestObject.TestClass(); //can't do
  48.     TestObject.~TestClass(); //works just fine.
  49.     TestObject.PrintVariables();
  50.     TestObject.ModifyVariables(120, 145);
  51.     TestObject.PrintVariables();
  52.  
  53.     return 0;
  54.  
  55. /* OUPUT:
  56. 100 100
  57. 120 145
  58. 0 0
  59. 120 145
  60. */
  61. }
  62.  
why am i able to call the destructor like that and not the constructor?
How can i call the constructor to reinit the variables to 100? or do i have to write a separate function?
Constructors is called by the compiler internally and i dont think so we can call the constructor explicitly.
You can call the destructor explicitly, as there is case for this.
That is if we use placement new then we have to call the destructor explicitly.
I think becos of this explicit calling of destructor is being allowed.
Other members please correct me if i am wrong.

Raghuram
Feb 19 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
When the compiler creates an object, it calls your constrcutor to initialize the variables.

When you call the constructor, the compiler first creates an object and then uses the constructor call tio initialize it.
Expand|Select|Wrap|Line Numbers
  1. void func(string& arg)
  2. {
  3.  
  4. }
  5. int main()
  6. {
  7.     func(string("Hello"));
  8. }
  9.  
Here the string constructor call results in the creation of a temporary string object containing "Hello" that can be used as the reference argument of the function call.

That means you cannot re-initialize an existing object using a constructor.
Feb 19 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Timothy Madden | last post by:
I know I can call destructors if I want (probably with their fully qualified name). However I don't see the point here. When and where would I want to call destructors ? Destructors are not...
3
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
6
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor...
4
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int...
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
7
by: engaarea | last post by:
hi all, What are the circumstances in which one would have to call a destructor explicitly. Regards 5
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
2
by: bg_ie | last post by:
Hi, I'm creating objects in my python script belonging to a COM object which I dispatch using win32com.client.DispatchEx. Hence, dllhost.dll is the concerned process. The problem is that the...
1
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.