Default AND explicit destructor?

Hi, let's say I have a class A with data members that are themselves classes (strings, vectors etc.). I know that if class A doesn't have an explicit destructor, these data members' destructors are called automatically, when an object of type A is destroyed.

However, I have an explicit destructor for class A (I need it for other data members).

Do I need to therefor also explicitly call the destructors of all class type data members now, in my explicit ~A destructor (i.e. name.~string(); ), or are those operations still handled automatically?

And, if yes, is the correct syntax name.~string(); , name.~String(); , or something else entirely?
The destructors are still called automatically.
Thanks. Excellent.

Sorry for the over-complication (I should've asked the more general question to begin with), but what about constructors, the copy constructor, and operators (like the assignment operator)?

Do class type data members get their corresponding constructors/operators called as well, even if I have explicit constructors and an operator= function handling some of the data members?
Last edited on
Constructors yes, but not operator=.
1
2
3
4
5
class foo{
   bar asdf;
public:
   foo(const foo &b){}
};
Your members will be constructed.
In the copy constructor, as you didn't specify anything, `asdf' is constructed using the default constructor.
Last edited on
closed account (Dy7SLyTq)
i thought the default constructor goes away when you make a constructor
yes, the default one provided by the compiler.
¿so?
asdf is of type bar, a different type than foo, so declaring a foo constructor will not affect the bar default constructor.
closed account (Dy7SLyTq)
oh i didnt see that. i saw asdf and thought it was builtin my mistake. so if it were builtin then it wouldnt be initialized right?
so if it were builtin then it wouldnt be initialized right?


I don't know what "builtin" means, but members that weren't of class type would not be initialized implicitly, but that's true whether the default constructor is defaulted, supplied by the user, or not defaulted and not supplied by the user.
closed account (Dy7SLyTq)
builtin is int, float, double, char, and bool
I don't know what "builtin" means, ...

From context, I take it DTSCode means he thought that it was a built-in (data) type.

The C++ Standard index entry for "built-in type" says "see fundamental type (p 54)"...

Andy

Thinking about it, there are prob. too many analgous terms: fundamental (data) type, built-in type, primitive type, native type, simple type, intrinsic type, ... (I'm pretty sure these all refer to the same thing?)
Last edited on
Topic archived. No new replies allowed.