Jump to content
  • Advertisement
Sign in to follow this  
Thirthe

[C++] default c/dtors

This topic is 3885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
If you define an empty constructor or destructor it will do exactly the same thing as a compiler defined default constructor or destructor.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!