Jump to content
  • Advertisement
Sign in to follow this  
MasterViruzprdr

C++ Object Creation

This topic is 4492 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

Im having a problem trying to create an object in C++ in the following way: Cat newCat = Cat(); // Calls Default Constructor This code gives the following compile error: "no matching function for call to `Cat::Cat(Cat)'" This only happens tho, when I have the copy constructor is declared for the class of the object I am trying to create. I know I can use the following method to call the default constructor: "Cat newCat;" But I would prefer to create it the first way I said because it is more like Java, which I have alot more experience in. So can anyone please tell me what is causing this error? I am using Dev-C++ with its packaged compiler. Thanks in advance for the help.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by MasterViruzprdr
Im having a problem trying to create an object in C++ in the following way:
Cat newCat = Cat(); // Calls Default Constructor


Cat newCat; would use the default constructor.

Cat newCat = Cat() would first create an unnamed temporary object using the default constructor and then use the copy constructor to initialize another object with that temporary variable.

[Edited by - Fruny on July 5, 2006 11:16:21 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by MasterViruzprdr
But I would prefer to create it the first way I said because it is more like Java, which I have alot more experience in.


This is closer to Java:
Cat* newCat = new Cat();

Just like in Java, newCat will be a pointer (reference in Java) to a Cat object allocated on the heap. Unfortunately, there is no garbage collector in C++ so you will have to manage the deallocation yourself.

Cat newCat = Cat() does something completely different. It constructs a temporary Cat object on the stack and then uses the copy constructor to copy it into newCat.

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!