By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,910 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,910 IT Pros & Developers. It's quick & easy.

what is const X cx={1};

P: n/a
Hi,

What is meant by
where X is
struct X
{
int i;
}
const X cx={1};

Help
sandspiderX

Jul 23 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a

"sandSpiderX" <m7*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi,

What is meant by
where X is
struct X
{
int i;
}
const X cx={1};

Help
sandspiderX


It's initializing cx with cx.i assigned to 1. Same as X cx; cx.i = 1;

more example:

struct font
{
int size;
char* family;
int color_red;
int color_green;
int color_blue;
bool bold;
bool italic;
};

// you can write
font myfont = {12, "times new roman", 255, 10, 10, true, false};

// instead of
font myfont2;
myfont2.size = 12;
myfont2.family = "times new roman";
myfont2.color_red = 255;
myfont2.color_green = 10;
myfont2.color_blue = 10;
myfont2.bold = true;
myfont2.italic = false;

regards,
ben
Jul 23 '05 #2

P: n/a

"benben" <be******@hotmail.com> wrote in message
news:42**********************@news.optusnet.com.au ...

"sandSpiderX" <m7*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi,

What is meant by
where X is
struct X
{
int i;
}
const X cx={1};

Help
sandspiderX


It's initializing cx with cx.i assigned to 1. Same as X cx; cx.i = 1;

more example:

struct font
{
int size;
char* family;
int color_red;
int color_green;
int color_blue;
bool bold;
bool italic;
};

// you can write
font myfont = {12, "times new roman", 255, 10, 10, true, false};

// instead of
font myfont2;
myfont2.size = 12;
myfont2.family = "times new roman";
myfont2.color_red = 255;
myfont2.color_green = 10;
myfont2.color_blue = 10;
myfont2.bold = true;
myfont2.italic = false;

regards,
ben


but you are also adding the 'const' keyword which says to the compiler that
you are not going to change the value of X.

Allan
Jul 23 '05 #3

P: n/a
sandSpiderX wrote:
Hi,

What is meant by
where X is
struct X
{
int i;
}
const X cx={1};

Help
sandspiderX


This is a way to initialise the structure variable cx such that cx.i =
1. If there were more members declared in X then you could have done
{1,3....}

The const keyword tells that the cx variable once intialised will not
have the value of its members modified.

Jul 23 '05 #4

P: n/a
Hi,

"sandSpiderX" <m7*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi,

What is meant by
where X is
struct X
{
int i;
}
Seems a ; is missing after }.
const X cx={1};
I think this means assigning cx.i to be 1.

Best Regards,
LaBird (Benny).
[Email: Remove all numerals for the correct email.]

Help
sandspiderX

Jul 23 '05 #5

P: n/a
Hi,

Can I do this for class also

Like
class X
{
public:
int x;
};

const class X = {1};

Help
ss

Jul 23 '05 #6

P: n/a
sandSpiderX wrote:
Hi,

Can I do this for class also

Like
class X
{
public:
int x;
};

const class X = {1};

Help
ss


You can use that initialization syntax for anything that qualifies as an
aggregate.

8.5.1.1:
"An aggregate is an array or a class (clause 9) with no user-declared
constructors (12.1), no private or protected non-static data members
(clause 11), no base classes (clause 10), and no virtual functions (10.3)."

In C++, the only difference between a struct and class is that struct
members are public by default, whereas class members are private by default.

-Alan
Jul 23 '05 #7

P: n/a
On Thu, 14 Jul 2005 19:42:05 +1000, benben <be******@hotmail.com> wrote:
It's initializing cx with cx.i assigned to 1. Same as X cx; cx.i = 1;

more example:

struct font
{
int size;
char* family;
int color_red;
int color_green;
int color_blue;
bool bold;
bool italic;
};

[...]
myfont2.family = "times new roman";


imho, you'll get a problem with just storing a pointer to a character
sequence, which will be gone as soon as the above line has been executed.
your struct should contain char family[MAX_NUMBER_OF_CHARS + 1] instead of
char* family!
Jul 23 '05 #8

P: n/a
"ulrich" <ua********@aon.at> wrote in message
news:opstwijefhn2mgp5@innsbruck-neu...
: > myfont2.family = "times new roman";
:
: imho, you'll get a problem with just storing a pointer to a character
: sequence, which will be gone as soon as the above line has been executed.

Not at all: string literals have static storage.
In other words, the above line is equivalent to:
static char const anonymous_string_literal[] = "times new roman";
myfont2.family = anonymous_string_literal;
The storage of string literals persists until program exit.

: your struct should contain char family[MAX_NUMBER_OF_CHARS + 1] instead of
: char* family!
No, this would expose you to a whole range of serious
bugs (i.e. buffer overruns). If a 'naked' char const*
is inadequate, better use std::string .
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 23 '05 #9

P: n/a

"sandSpiderX" <m7*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi,

What is meant by
where X is
struct X
{
int i;
}
const X cx={1};

Help
sandspiderX


Same idea as:

struct X {
int i;
X(int x): i(x) {}
};

const X cx(1);

Better IMO. No assignments.

Jul 23 '05 #10

P: n/a
On Thu, 14 Jul 2005 14:33:49 +0200, Ivan Vecerina
<NO**********************************@vecerina.com > wrote:
"ulrich" <ua********@aon.at> wrote in message
news:opstwijefhn2mgp5@innsbruck-neu...
: > myfont2.family = "times new roman";
:
: imho, you'll get a problem with just storing a pointer to a character
: sequence, which will be gone as soon as the above line has been
executed.

Not at all: string literals have static storage.
In other words, the above line is equivalent to:
static char const anonymous_string_literal[] = "times new roman";
myfont2.family = anonymous_string_literal;
The storage of string literals persists until program exit.

: your struct should contain char family[MAX_NUMBER_OF_CHARS + 1]
instead of
: char* family!
No, this would expose you to a whole range of serious
bugs (i.e. buffer overruns). If a 'naked' char const*
is inadequate, better use std::string .


off course...
Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.