constructor and destructor not called
Jun 14, 2009 at 12:36am UTC
hello everybody
I have a piece of code where I create an object with the default constructor, but the constructor (and destructor) isn't called. I use
ConstructorTest a();
to create the object, which should just call the default constructor. If I remove the (), it does work.
how come the constructor isn't called when using ()?
here is the entire 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 43 44 45 46 47 48 49 50 51 52 53 54
#include <iostream>
using namespace std;
class ConstructorTest
{
private :
const char *name;
public :
//default constructor
ConstructorTest() : name("no name" )
{
cout << "calling ConstructorTest default constructor from: " << name << endl;
}
//custom constructor
ConstructorTest(const char *name) : name(name)
{
cout << "calling ConstructorTest custom constructor from: " << name << endl;
}
//copy constructor
ConstructorTest(const ConstructorTest& a) : name(a.name)
{
cout << "calling ConstructorTest copy constructor from: " << name << endl;
}
//destructor
~ConstructorTest()
{
cout << "calling ConstructorTest destructor from: " << name << endl;
}
};
void func()
{
cout << "entered func" << endl;
ConstructorTest a();
cout << "leaving func" << endl;
}
int main()
{
cout << "Hello world!" << endl;
func();
cout << "Goodbye world!" << endl;
return 0;
}
and here is the output:
Hello world!
entered func
leaving func
Goodbye world!
Process returned 0 (0x0) execution time : 0.087 s
Press any key to continue.
Jun 14, 2009 at 12:43am UTC
line 41 does not create an object of ConstructorTest, it declares a
function which returns a ConstructorTest and takes no parameters.
A solution to this is to leave off the parenthesis.
1 2 3 4 5 6
void func()
{
cout << "entered func" << endl;
ConstructorTest a; // <--- no parenthesis
cout << "leaving func" << endl;
}
EDIT: but now that I actually read what you typed, you already knew that! XD
Sorry. But yeah -- the compiler thinks you're making a function, not an object.
Last edited on Jun 14, 2009 at 12:44am UTC
Jun 14, 2009 at 1:08pm UTC
now why would the compiler think I would declare a function inside another function. silly compilers.
anyway, is there any situation where using () wouldn't result in a function declaration?
Jun 14, 2009 at 2:41pm UTC
when a constructor doesn't have a parameter then u just call it using a variable name
BUT if they do then i think u need to call it your way using the brackets with the parameter values
Jun 14, 2009 at 3:30pm UTC
now why would the compiler think I would declare a function inside another function. silly compilers.
I agree it's absurd. But it's not the compiler's fault -- it's the language.
anyway, is there any situation where using () wouldn't result in a function declaration?
Yes.
1 2 3 4 5 6 7 8 9
// assume foo is a class name
foo* bar = new foo();
somefunction( foo() );
foo barb = foo();
barb = foo();
//and I'm sure there are others
Basically it will only get confused as a function if you make
foo
appear as if it's the return type.
Here's a related read:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19
Topic archived. No new replies allowed.