简体   繁体   中英

In which C++ Compilers I can assign values to struct variables?

I was unsure of how should I name this question. It's probably a noob question but anyway...
I don't really now why assigning values in struct in C++ does work in Linux(Ubuntu) but doesn't in Windows(I get an error compiling same program in windows)
It depends on compiler, yes? Or there is something else I should know?

#include <iostream>
#include <fstream>

const char data[] = "data.txt";
const char result[] = "data_result.txt";
const int Cn = 10;

struct Players{

    /*Assigning values in struct does not work in windows */
    int Health = 100;
    int Mana = 120;
    int Experience = 0;

};

struct Players Player[Cn];

int main(){


    return 0;
}

Assigning "default" values to structs or classes in C++ is a feature that only became a thing in C++11. So it's probably fair to say that your Windows compiler either doesn't support C++11, or else isn't receiving the correct compile arguments to compile with C++11.

Your question is "In which C++ compilers can I assign values to struct variables".

My answer is "from all compilant c++ compilers, conforming to c++98 forward". All allow struct data attributes to be initialized.

The following works even when I set my compiler option std=c++98

What it does is avoid the C++11 feature and moves the data attribute initialization into a user defined default ctor.

struct Players
{
   int Health;
   int Mana;
   int Experience;

   Players() : Health(100), Mana(120), Experience(0)
      {
      }   

   // an array of Cn Players will build Cn copies, 
   // all initialized to these same 3 values of the default ctor.

};

Yes, your code struct does have a compiler provided ctor (and a few other method attributes.)

And yes, I suppose you don't really want to use c++98.


update

The following is yet another default ctor, though it may not look like it.

struct Players
{
   int Health;
   int Mana;
   int Experience;

   Players(int aHealth=100, int aMana=120, int anExperiance=0) : 
           Health(aHealth), Mana(aMana), Experience(anExperience)
      {
      }   

};

This is default ctor because you can invoke it as "Players players();", and all the method defaults will apply.


You may control what the compiler provides for your struct/class by using the "= delete." statement.

The methods you can order the compiler to not provide

  //  coding standard: disallow when not used
  T(void)                  = delete; // default ctor    (1)
  ~T(void)                 = delete; // default dtor    (2)
  T(const T&)              = delete; // copy ctor       (3)
  T& operator= (const T&)  = delete; // copy assignment (4)
  T(const T&&)             = delete; // move ctor       (5)
  T& operator= (const T&&) = delete; // move assignment (6)

Just replace T your class name (ie Players)

Remember, you disallow when you want to not have one of these methods. Do not disallow if you provide an alternative to the compiler provide method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM