473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

what is const X cx={1};

Hi,

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

Help
sandspiderX

Jul 23 '05 #1
10 1848

"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

"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
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
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
Hi,

Can I do this for class also

Like
class X
{
public:
int x;
};

const class X = {1};

Help
ss

Jul 23 '05 #6
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
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
"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Mertz | last post by:
At the suggestion of one of my correspondents, I slightly reluctantly implemented an RSS feed for my website/writing. It is perhaps a bit crude so far, but maybe I'll spiff it up. The RSS also...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
3
by: Klein | last post by:
const S_Table_Structure * const * cur = table;
4
by: empeegee | last post by:
Hi All, I'm going through the Single Unix Specification V3 (POSIX standard) available in the net. In the specification of system interfaces, there are certain parts marked with or . Is those...
4
by: smnoff | last post by:
I am trying to use the strstr function but it doesn't seem to work with a dynamically allocated string that I pass into a function. Specifically, I am using a Macromedia C-level extensibility and...
14
by: Jonas.Holmsten | last post by:
Hello I'm porting some C++ stuff to C and having problem to get it through gcc. Here is a condensed version of the problem: void foo(const int * const * const ptr) {} main()
9
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int...
12
by: copx | last post by:
I remember reading somewhere that C's 'const' keyword is almost useless, except for maybe triggering some additional compiler warnings. 'Useless' as in: a conforming C compiler cannot assume that a...
2
by: .rhavin grobert | last post by:
i have (do try to have?) the following... & = breakpoints in debugger // ---------------------------------------------------------------- // cx.h class CX { public: CX(CX* pcx = NULL);...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.