What is the difference between class a () and class a = class () in C ++?

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

someclass a = someclass();

instead

someclass a();

to initialize a class variable in C ++. However, my compiler sometimes complains

Error C2280: Attempting to reference a deleted function

Is there any difference between the two? Which one is better?

+4
source share
2 answers

Is there any difference between the two?

Big: someclass a();announces a function!

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

Which one is better?

Missing. Use instead

someclass a;

or

someclass a{}; // C++11

Both will call the default constructor .

+7
source

++

steps

classname _;

#include<iostream>
using namespace std;

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

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

-

-

#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;
   }

- -

java . , .

-2

Source: https://habr.com/ru/post/1692181/


All Articles