Const в структуре
От: SpLove Россия  
Дата: 07.06.05 15:48
Оценка:
Объясните пожалуйста почему я не могу написать так:
struct journal_page
{
    unsigned int csum;
    unsigned char data[512];
    const unsigned short MAGIC = 0x3035; // ругается на это
    static unsigned int calc_csum(char* buf, unsigned size){return 1;}
};

Компилятор в VC++ 6.0 SP6 это не проглатывает . Хотя по-моему должен
Re: Const в структуре
От: Сергей Мухин Россия  
Дата: 07.06.05 15:51
Оценка:
Здравствуйте, SpLove, Вы писали:
>> Хотя по-моему должен

кто главней? ты или компилятор?

напиши:
enum { MAGIC = 0x3035 };
---
С уважением,
Сергей Мухин
Re: Const в структуре
От: Pavel Chikulaev Россия  
Дата: 07.06.05 15:55
Оценка: -1
Здравствуйте, SpLove, Вы писали:

SL>Объясните пожалуйста почему я не могу написать так:

SL>
SL>struct journal_page
SL>{
SL>    unsigned int csum;
SL>    unsigned char data[512];
SL>    const unsigned short MAGIC = 0x3035; // ругается на это
SL>    static unsigned int calc_csum(char* buf, unsigned size){return 1;}
SL>};
SL>

SL>Компилятор в VC++ 6.0 SP6 это не проглатывает . Хотя по-моему должен

Потому что надо так:

journal_page.h

struct journal_page
{
    unsigned int csum;
    unsigned char data[512];
    const unsigned short MAGIC;
    static unsigned int calc_csum(char* buf, unsigned size){return 1;}
};


journal_page.cpp

const unsigned short journal_page::MAGIC = 0x3035;


А проще использовать enum hack:

struct journal_page
{
    unsigned int csum;
    unsigned char data[512];
    enum { MAGIC = 0x3035 };
    static unsigned int calc_csum(char* buf, unsigned size){return 1;}
};
Re: Const в структуре
От: VoidEx  
Дата: 07.06.05 15:57
Оценка: +1 -2
Здравствуйте, SpLove, Вы писали:

SL>Объясните пожалуйста почему я не могу написать так:

SL>
SL>struct journal_page
SL>{
SL>    unsigned int csum;
SL>    unsigned char data[512];
SL>    const unsigned short MAGIC = 0x3035; // ругается на это
SL>    static unsigned int calc_csum(char* buf, unsigned size){return 1;}
SL>};
SL>

SL>Компилятор в VC++ 6.0 SP6 это не проглатывает . Хотя по-моему должен
Не должен. Инициализировать внутри структуры можно только статическую константу, которая одна для всех структур.
Т.е. в данном случае можно написать так :
static unsigned const short MAGIC = 0x3035;
Re[2]: Const в структуре
От: korzhik Россия  
Дата: 07.06.05 16:16
Оценка: +1 :)
Здравствуйте, Pavel Chikulaev, Вы писали:

PC>Потому что надо так:


PC>journal_page.h


PC>
PC>struct journal_page
PC>{
PC>    unsigned int csum;
PC>    unsigned char data[512];
PC>    const unsigned short MAGIC;
PC>    static unsigned int calc_csum(char* buf, unsigned size){return 1;}
PC>};
PC>


PC>journal_page.cpp


PC>
PC>const unsigned short journal_page::MAGIC = 0x3035;
PC>


хммм, это ты со статическим членом перепутал. Здесь это не подходит.
Re: Const в структуре
От: WinterMute Россия http://yarrr.ru
Дата: 07.06.05 16:17
Оценка:
Здравствуйте, SpLove, Вы писали:

SL>Объясните пожалуйста почему я не могу написать так:

SL>
SL>struct journal_page
SL>{
SL>    unsigned int csum;
SL>    unsigned char data[512];
SL>    const unsigned short MAGIC = 0x3035; // ругается на это
SL>    static unsigned int calc_csum(char* buf, unsigned size){return 1;}
SL>};
SL>


Потому что "const" внутри объявления класса имеет смысл: "переменная не будет менятся после инициализации экземпляра класса". Если тебе нужна константа, которая будет общей для всех экземпляров класса, то нужно объявлять её как "static const".
Re[2]: Const в структуре
От: WinterMute Россия http://yarrr.ru
Дата: 07.06.05 16:19
Оценка:
VE>Т.е. в данном случае можно написать так :
VE>static unsigned const short MAGIC = 0x3035;

Может всё-таки static const unsigned short MAGIC = 0x3035;
Re: Const в структуре
От: Кодт Россия  
Дата: 07.06.05 16:22
Оценка:
Здравствуйте, SpLove, Вы писали:

SL>Компилятор в VC++ 6.0 SP6 это не проглатывает . Хотя по-моему должен


Шестёрка эту фичу не поддерживает. Так что
— или внешнюю константу (может быть, в namespace)
— или статическую константную переменную-член с инициализацией в .cpp-файле
— или enum.
Перекуём баги на фичи!
Re[2]: Const в структуре
От: Pavel Chikulaev Россия  
Дата: 07.06.05 16:33
Оценка:
Здравствуйте, Кодт, Вы писали:

К>Здравствуйте, SpLove, Вы писали:


SL>>Компилятор в VC++ 6.0 SP6 это не проглатывает . Хотя по-моему должен


К>Шестёрка эту фичу не поддерживает. Так что

Що за фига? (VC7.1 тоже не поддерживает)
Может он просто забыл static?
Re[3]: Const в структуре
От: korzhik Россия  
Дата: 07.06.05 16:36
Оценка:
Здравствуйте, WinterMute, Вы писали:

VE>>Т.е. в данном случае можно написать так :

VE>>static unsigned const short MAGIC = 0x3035;

WM>Может всё-таки static const unsigned short MAGIC = 0x3035;


как я понял стандарт:

7.1.5/1
const or volatile can be combined with any other type-specifier.


следующие определения идентичны
int main()
{
  const unsigned short int n1(0u);
  unsigned const short int n2(0u);
  unsigned short const int n3(0u);
  unsigned short int const n4(0u);
}
Re[4]: Const в структуре
От: Pavel Chikulaev Россия  
Дата: 07.06.05 16:51
Оценка: 1 (1) :))
Здравствуйте, korzhik, Вы писали:

[skipped]

const int short static unsigned a0;
const int short unsigned static a1;
const int static short unsigned a2;
const int static unsigned short a3;
const int unsigned short static a4;
const int unsigned static short a5;
const short int static unsigned a6;
const short int unsigned static a7;
const short static int unsigned a8;
const short static unsigned int a9;
const short unsigned int static a10;
const short unsigned static int a11;
const static int short unsigned a12;
const static int unsigned short a13;
const static short int unsigned a14;
const static short unsigned int a15;
const static unsigned int short a16;
const static unsigned short int a17;
const unsigned int short static a18;
const unsigned int static short a19;
const unsigned short int static a20;
const unsigned short static int a21;
const unsigned static int short a22;
const unsigned static short int a23;
int const short static unsigned a24;
int const short unsigned static a25;
int const static short unsigned a26;
int const static unsigned short a27;
int const unsigned short static a28;
int const unsigned static short a29;
int short const static unsigned a30;
int short const unsigned static a31;
int short static const unsigned a32;
int short static unsigned const a33;
int short unsigned const static a34;
int short unsigned static const a35;
int static const short unsigned a36;
int static const unsigned short a37;
int static short const unsigned a38;
int static short unsigned const a39;
int static unsigned const short a40;
int static unsigned short const a41;
int unsigned const short static a42;
int unsigned const static short a43;
int unsigned short const static a44;
int unsigned short static const a45;
int unsigned static const short a46;
int unsigned static short const a47;
short const int static unsigned a48;
short const int unsigned static a49;
short const static int unsigned a50;
short const static unsigned int a51;
short const unsigned int static a52;
short const unsigned static int a53;
short int const static unsigned a54;
short int const unsigned static a55;
short int static const unsigned a56;
short int static unsigned const a57;
short int unsigned const static a58;
short int unsigned static const a59;
short static const int unsigned a60;
short static const unsigned int a61;
short static int const unsigned a62;
short static int unsigned const a63;
short static unsigned const int a64;
short static unsigned int const a65;
short unsigned const int static a66;
short unsigned const static int a67;
short unsigned int const static a68;
short unsigned int static const a69;
short unsigned static const int a70;
short unsigned static int const a71;
static const int short unsigned a72;
static const int unsigned short a73;
static const short int unsigned a74;
static const short unsigned int a75;
static const unsigned int short a76;
static const unsigned short int a77;
static int const short unsigned a78;
static int const unsigned short a79;
static int short const unsigned a80;
static int short unsigned const a81;
static int unsigned const short a82;
static int unsigned short const a83;
static short const int unsigned a84;
static short const unsigned int a85;
static short int const unsigned a86;
static short int unsigned const a87;
static short unsigned const int a88;
static short unsigned int const a89;
static unsigned const int short a90;
static unsigned const short int a91;
static unsigned int const short a92;
static unsigned int short const a93;
static unsigned short const int a94;
static unsigned short int const a95;
unsigned const int short static a96;
unsigned const int static short a97;
unsigned const short int static a98;
unsigned const short static int a99;
unsigned const static int short a100;
unsigned const static short int a101;
unsigned int const short static a102;
unsigned int const static short a103;
unsigned int short const static a104;
unsigned int short static const a105;
unsigned int static const short a106;
unsigned int static short const a107;
unsigned short const int static a108;
unsigned short const static int a109;
unsigned short int const static a110;
unsigned short int static const a111;
unsigned short static const int a112;
unsigned short static int const a113;
unsigned static const int short a114;
unsigned static const short int a115;
unsigned static int const short a116;
unsigned static int short const a117;
unsigned static short const int a118;
unsigned static short int const a119;


#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <string>

using namespace std;


int main(
{
    vector<string> v;
    v.push_back("const");
    v.push_back("unsigned");
    v.push_back("static");
    v.push_back("int");
    v.push_back("short");
    sort(v.begin(), v.end());

    int i = 0;
    do 
    {
        copy(v.begin(), v.end(), ostream_iterator<string>(cout, " "));
        cout << "a" << i << ";"<< endl;
        ++i;
    }
    while (next_permutation(v.begin(), v.end()));
    return 0;
}
Re[5]: Const в структуре
От: korzhik Россия  
Дата: 07.06.05 16:53
Оценка: +2 :))) :)))
Здравствуйте, Pavel Chikulaev, Вы писали:

PC>Здравствуйте, korzhik, Вы писали:


PC>[skipped]


PC>
PC>const int short static unsigned a0;
PC>const int short unsigned static a1;
...
PC>


надо бы было ещё volatile использовать и в конце передать привет всем пользователем модемов
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.