Question about constructor
Aug 16, 2011 at 4:55pm UTC
Hi, I have a question about constructors. Let's say I have code like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
class MyClass
{
protected :
// variables
public :
MyClass()
{
// initialize variables here
cout <<"MyClass()" <<endl;
}
MyClass(int x)
{
MyClass(); // Call default constructor to initialize variables
cout <<"MyClass(x)" <<endl;
}
};
int main()
{
MyClass mc(10);
}
MyClass()
MyClass(x)
My question is about line 13. Will it call a constructor with no arguments for current object (mc) or will it create a new instance of MyClass?
I think my second guess is correct because this code works:
1 2 3 4
int main()
{
MyClass();
}
but I'm not sure.
Thanks for help!
Aug 16, 2011 at 4:58pm UTC
in your code doesnt create new class and call for "this" MyClass constructor !
this->MyClass();
when you have a new instance that you create one object of it.
Aug 16, 2011 at 5:14pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
class MyClass
{
protected :
// variables
int SomeVariable; // some vancy variable to be used
public :
MyClass() //this is DEFAULT constructor
{
// initialize variables here
cout <<"MyClass()" <<endl;
SomeVariable=0; //Default constructor is defined to set variable to 0
}
MyClass(int x) //This is "overloaded" constructor which takes an argument type of int
{
cout <<"MyClass(x)" <<endl;
SomeVariable=x; //This version of constructor is defined to set SomeVariable to given parameter
}
};
int main()
{
MyClass mc1; //this calls default constructor
MyClass mc2(123); //This calls second, "overloaded", constructor
}
Note to be remembered: CLASSes are "prototypes" to create any visual (or not) OBJECTs which have unique skills and functionability. :)
Last edited on Aug 16, 2011 at 5:20pm UTC
Aug 16, 2011 at 5:43pm UTC
I found a way to check what's going on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
class MyClass
{
protected :
// variables
public :
MyClass()
{
// initialize variables here
cout <<"MyClass()" <<endl;
}
MyClass(int x)
{
cout <<"MyClass(x)" <<endl;
MyClass(); // Call default constructor
}
~MyClass()
{
cout <<"Destructor" <<endl;
}
};
int main()
{
MyClass mc(10);
}
MyClass(x)
MyClass()
Destructor
Destructor
Destructor is called twice which means that two instances of MyClass are created.
Here's a better illustration of the bug I had in my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
class AnotherClass
{
public :
void msg()
{
cout <<"AnotherClass::msg()" <<endl;
}
};
class MyClass
{
protected :
AnotherClass *ac;
public :
MyClass()
{
ac=new AnotherClass;
cout <<"ac=new AnotherClass;" <<endl;
}
MyClass(int x)
{
MyClass();
}
void ShowMessage()
{
ac->msg();
}
~MyClass()
{
delete ac;
cout <<"delete ac;" <<endl;
}
};
int main()
{
MyClass mc(10);
mc.ShowMessage();
}
ac=new AnotherClass;
delete ac;
AnotherClass::msg()
delete ac;
even though
ac
is deleted, call to
ac->msg()
is successful.
It's amazing how long some bugs can be undetected and do not cause any problems.
Topic archived. No new replies allowed.