[C++] default c/dtors

Started by
9 comments, last by ToohrVyk 16 years, 2 months ago
hello all, i'm using the g++ compiler and i'm wondering if it's any faster to write empty default ctors and dtors than no ctors/dtors at all. i've heard a discussion that compilers generate their own def. ctors/dtor, which just slow down the program, and that it's better to just write "Constructor(){};" even though i will never need it. any truth to that? thank you.
Advertisement
If you define an empty constructor or destructor it will do exactly the same thing as a compiler defined default constructor or destructor.
Step one: decide what the constructor should do.

Step two: does the default constructor do what you need? If it does, then use it, as it's quite possibly the fastest way of achieving that.

Step three: if the default constructor isn't enough, implement your own constructor.

In short: if the default constructor does what you want, there's a high probability that you won't outperform it by hand.
understood. thanks!
Quote:
i'm using the g++ compiler and i'm wondering if it's any faster to write empty default ctors and dtors than no ctors/dtors at all.

>I recommend against using g++, if you can avoid it.

Your second sentence seems confusing. If you do not create any ctors nor dtors, the compiler automatically creates them for you. In other words, you dont "write" them, the compiler generates them for you.

Quote:
i've heard a discussion that compilers generate their own def. ctors/dtor, which just slow down the program, and that it's better to just write "Constructor(){};" even though i will never need it.

Incorrect.

You are correct in that compiliers generate their own ctors and dtors if they are not specified, however they do not do anything. It is recommended to create your own ctors and dtors only to insure of proper initialization and destruction of your object.

Automatically generated default ctors/dtors are not slower then creating your own.

*edit: I type slow.
Quote:Original post by Crypter
>I recommend against using g++, if you can avoid it.


Er, why?

Quote:Original post by ToohrVyk
Er, why?
Depending on platform, their may be better options available. i.e., If he is using Windows then I would recommend against it as MSVC++ is freely available.

Granted, GCC and G++ are a good set of tools, however, in my opinion, there are better options for Windows.
Quote:Original post by Crypter
Depending on platform, their may be better options available. i.e., If he is using Windows then I would recommend against it as MSVC++ is freely available.

Granted, GCC and G++ are a good set of tools, however, in my opinion, there are better options for Windows.

I would suggest that programmers should be comfortable with more than one set of tools.

Σnigma
Quote:Original post by Crypter
Depending on platform, their may be better options available. i.e., If he is using Windows then I would recommend against it as MSVC++ is freely available.

Granted, GCC and G++ are a good set of tools, however, in my opinion, there are better options for Windows.


Why? What are the arguments for either?

Why is GCC inferior to CL? Why are all other Windows options inferior to either? Why is Intel's compiler inferior to both?
Quote:Original post by Enigma
I would suggest that programmers should be comfortable with more than one set of tools.

Agreed.

Quote:Original post by Antheus
Why is GCC inferior to CL? Why are all other Windows options inferior to either? Why is Intel's compiler inferior to both?

I understand where you are going with this. MSVC++'s IDE certainly makes things easier for beginner programmers in C++, which is why I usually recommend it to beginner developers. I did not mean for it to sound like other tools are inferior for development purposes.

I suppose my wording in my previous post was not very good. Sorry about that.

This topic is closed to new replies.

Advertisement