class B
{
public:
B():
{
d = 0;
e = 0;
f = 0;
}
B(int a)
{
B(); // doesnt call????
}
int d,e,f;
};
int main()
{
B b; // d,e, and f doesnt change to zero
return 1;
}
Can you call classes own constructor?
Hi, i just ran in to a tricky problem.
This code below doesnt set the three vars to zero like i asumed, so is there any other ways?
How about:
If you'd like, you can google "constructor initializers" and you'll find helpful examples.
[edited by - nervo on May 29, 2004 12:12:38 AM]
class B{public: B(): d(0),e(0),f(0) {} //<-- Constructor Initializersprivate:int d,e,f;};
If you'd like, you can google "constructor initializers" and you'll find helpful examples.
[edited by - nervo on May 29, 2004 12:12:38 AM]
If I remember correctly, yes you can call a class' own constructor.
Just change
B();
to
B::B();
I use this technique for a few of my constructors as well. (With inheritance , for example).
[edited by - Just3D on May 29, 2004 12:17:28 AM]
[edited by - Just3D on May 29, 2004 12:18:12 AM]
Just change
B();
to
B::B();
I use this technique for a few of my constructors as well. (With inheritance , for example).
[edited by - Just3D on May 29, 2004 12:17:28 AM]
[edited by - Just3D on May 29, 2004 12:18:12 AM]
No. B() will create (and here discard) a temporary object. What Just3D is referring to works only for passing arguments to parent classes in inheritance hierarchies, and only in the initializer list, not the constructor body.
Wow! I just tested that with Builder, and you''re right, Miserable!
Nevermind what I said Craazer, the method I used only works when the constructor is inherited.
I guess you learn something new everyday.
Nevermind what I said Craazer, the method I used only works when the constructor is inherited.
I guess you learn something new everyday.
Thank you for your replys.
Nervo, im aware of that option but thats not what i want.
So no way to call classes own constructor?
Nervo, im aware of that option but thats not what i want.
So no way to call classes own constructor?
The solution to your problem is to create a special initialization function you call in both constructors.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement