what is const X cx={1};

S

sandSpiderX

Hi,

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

Help
sandspiderX
 
L

LaBird

Hi,

sandSpiderX said:
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.]
 
B

benben

sandSpiderX said:
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
 
A

Allan Bruce

benben said:
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
 
J

Jaspreet

sandSpiderX said:
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.
 
S

sandSpiderX

Hi,

Can I do this for class also

Like
class X
{
public:
int x;
};

const class X = {1};

Help
ss
 
A

Alan Johnson

sandSpiderX said:
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
 
U

ulrich

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!
 
I

Ivan Vecerina

: > 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
 
D

Duane Hebert

sandSpiderX said:
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.
 
U

ulrich

: > 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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top