5

Coming from the Java and C# world, I always like to use

someclass a = someclass();

instead of

someclass a();

to initalize a class variable in C++. However, my compiler sometimes complain

Error C2280: Attempting to reference a deleted function

Is there any difference between them? Which one is better?

doge99
  • 188
  • 8
  • 3
    Big difference. You can start with [What does X f(); mean?](https://stackoverflow.com/questions/15653345/what-does-x-f-mean) – Bo Persson Jan 15 '18 at 03:52
  • 2
    Not a downvoter, but I'd imagine a lack of [mcve] and lack of research will usually garner downvotes. – Tas Jan 15 '18 at 04:24

2 Answers2

8

Is there any difference between them?

A big one: someclass a(); is declaring a function!

And someclass a = someclass();, before C++17's copy ellision, requires the class to be movable, which is probably not the case here as you get the error Attempting to reference a deleted function.

Which one is better?

None. Use instead:

someclass a;

or

someclass a{}; // C++11

Both will call default constructor.

O'Neil
  • 3,790
  • 4
  • 16
  • 30
  • How about classes with constructors that require parameters? Are someclass a(param) and someclass a = someclass(param) same besides the cons you have mentioned? – doge99 Jan 15 '18 at 06:29
  • @AmazingJustus The `someclass a()` special case considered as function declaration applies mostly when there isn't any parameter. But you can find some cases with parameters such as `someclass a(someother())` which will also result in function declaration. – O'Neil Jan 15 '18 at 17:45
  • These will ba corrected by using curly braces or adding extra parenthesis: `someclass a( (someother()) )`. – O'Neil Jan 15 '18 at 17:46
  • 1
    As of `someclass a(param)` againts `someclass a = someclass(param)`, the first will be prefered. – O'Neil Jan 15 '18 at 17:49
-2

Dear Friend in C++ user creates the object as follows

steps

classname objectname;

#include<iostream>
using namespace std;

class demo
{
   public:
      void print()
         {
           cout<<"Demo class";
         }
 };
int main()
   {

      demo d;
      d.print();
      return 0;
   }

OUTPUT Demo class

using pointer object

#include<iostream>
using namespace std;
class demo
{
   public:
      void print()
         {
           cout<<"Demo class using pointer object";
         }
 };

int main()
   {

      demo *d = new demo();
      d->print();
      return 0;
   }

OUTPUT Demo class using pointer object

In java, garbage collection is done automatically. I hope that you understand concept.

K.Vani
  • 72
  • 4
  • 4
    Recommendation: To beat home your closing statement you should add `delete d;` to the final `main`. A correction: `void main()` is invalid in C++. The C++ standard requires that `main` return `int`. A minor niggle: You can go an entire programming career and never see anything but stack and heap, but C++ does not require stacks and heaps. Stack and heap are just the most common (by far) implementations of Automatic and Dynamic storage. – user4581301 Jan 15 '18 at 05:03
  • void main() will work in turboc.it is not invalid statement. In gcc compiler it is mandatory to specify as int. in my third point i specified that delete operator has to be used to deallocates the memory. i attached the turbo c complier screen shot in my answer – K.Vani Jan 15 '18 at 15:39
  • 2
    @K.Vani Saying that some compiler compiles something does not mean it is valid C++ code. Compilers have errors, and older compilers (like turboc) tend to have loads of them. – lisyarus Jan 15 '18 at 15:50
  • 1
    @K.Vani TurboC++ came and went before C++ was standardized in 1998. It is a different language. Knowing one, you can muddle your way through the other just like you can with C and C++, but there are numerous differences that will trip you up. This is one of them. – user4581301 Jan 15 '18 at 16:30