473,498 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with initialization of const

Hi,

thanks to your suggestions I got "Accelerated C++" and I'm studying
it. I found very interesting the possibility of initializing constants
at runtime, but it looks like I'm not getting it right:

#include <iostream>
#include <string>

int main ()
{
std::string name;
const std::string store = name;
std::cout << "Gimme a name " << std::endl;
std::cin >name;
std::cout << "You wrote " << name << std::endl;
std::cout << "Store was set to " << store;
return 0;
}

The output is:

Gimme a name
Tom
You wrote Tom
Store was set to

What happened to the content of store? Thanks,

greetings,

deltaquattro

Jan 31 '07 #1
15 1541
deltaquattro wrote:
Hi,

thanks to your suggestions I got "Accelerated C++" and I'm studying
it. I found very interesting the possibility of initializing constants
at runtime, but it looks like I'm not getting it right:

#include <iostream>
#include <string>

int main ()
{
std::string name;
const std::string store = name;
std::cout << "Gimme a name " << std::endl;
std::cin >name;
std::cout << "You wrote " << name << std::endl;
std::cout << "Store was set to " << store;
return 0;
}

The output is:

Gimme a name
Tom
You wrote Tom
Store was set to

What happened to the content of store? Thanks,
What content? You copied an empty string to it.

Jan 31 '07 #2
On Jan 31, 1:46 pm, "deltaquattro" <deltaquat...@gmail.comwrote:
Hi,

thanks to your suggestions I got "Accelerated C++" and I'm studying
it. I found very interesting the possibility of initializing constants
at runtime, but it looks like I'm not getting it right:

#include <iostream>
#include <string>

int main ()
{
std::string name;
const std::string store = name;
std::cout << "Gimme a name " << std::endl;
std::cin >name;
std::cout << "You wrote " << name << std::endl;
std::cout << "Store was set to " << store;
return 0;

}

The output is:

Gimme a name
Tom
You wrote Tom
Store was set to

What happened to the content of store? Thanks,
The value of a const can only be set once, when it's declared (const
class members are a bit different) and in this case you initializes
store to the value of name, which happens to be empty at that moment.
Move the line 'const std::string store = name;' down below 'std::cin
>name;' and it will work.
Looking at your code above it seems like you expect a behaviour
similar to that of references, where store would be a read-only alias
of name. To get this behaviour change the declaration of store to
'const std::string& store = name;'

--
Erik Wikström

Jan 31 '07 #3
On 31 Gen, 14:09, "Erik Wikström" <eri...@student.chalmers.sewrote:
On Jan 31, 1:46 pm, "deltaquattro" <deltaquat...@gmail.comwrote:
[..]
What happened to the content of store? Thanks,

The value of a const can only be set once, when it's declared (const
class members are a bit different) and in this case you initializes
store to the value of name, which happens to be empty at that moment.
Move the line 'const std::string store = name;' down below 'std::cin
name;' and it will work.
Hi, Erik,

thank you very much for your useful answer. I'm not still used to the
fact that in C++ you can (in this case, you have to) place declaration
statements among executable statements.
>
Looking at your code above it seems like you expect a behaviour
similar to that of references, where store would be a read-only alias
of name. To get this behaviour change the declaration of store to
'const std::string& store = name;'
Yes, that's what I was looking for: thanks for pointing me to
references, which I didn't know.

greetings,

deltaquattro

Jan 31 '07 #4
On 31 Gen, 13:57, Rolf Magnus <ramag...@t-online.dewrote:
deltaquattro wrote:
[..]
What happened to the content of store? Thanks,

What content? You copied an empty string to it.
Hi, Rolf,

whoops! You're right! As I told Erik, I must remember that in C++
declarations need not precede executabvle statements: in this case, if
I place all declarations before execution section, I can't get the
desired results. Thanks,

greetings,

deltaquattro

Jan 31 '07 #5
"Erik Wikström" <er****@student.chalmers.sewrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
On Jan 31, 1:46 pm, "deltaquattro" <deltaquat...@gmail.comwrote:
The value of a const can only be set once, when it's declared (const
class members are a bit different)
When it's defined, actually. Hope I'm not being too pedantic.
Jan 31 '07 #6
On Jan 31, 4:36 pm, "Andrew Koenig" <a...@acm.orgwrote:
"Erik Wikström" <eri...@student.chalmers.sewrote in message

news:11**********************@q2g2000cwa.googlegro ups.com...
On Jan 31, 1:46 pm, "deltaquattro" <deltaquat...@gmail.comwrote:
The value of a const can only be set once, when it's declared (const
class members are a bit different)

When it's defined, actually. Hope I'm not being too pedantic.
You are not by those terms are quite non-intuitive for some.
Apparently Erik has problems and so do I: I have to think hard every
time I have to remember if
class C;

is a declaration or a definition. To me it is both, as C gets defined
as a class, but when thinking about it I do realise that in
standardese it is only a declaration.
I wonder if there is a linguistic connection as Erik and I are both
scandinavians with a closely related native tongue. The answer to this
would be off-topic in this newsgroup, of course.

/Peter

Jan 31 '07 #7
peter koch wrote:
On Jan 31, 4:36 pm, "Andrew Koenig" <a...@acm.orgwrote:
>"Erik Wikström" <eri...@student.chalmers.sewrote in message

news:11**********************@q2g2000cwa.googlegr oups.com...
On Jan 31, 1:46 pm, "deltaquattro" <deltaquat...@gmail.comwrote:
The value of a const can only be set once, when it's declared (const
class members are a bit different)

When it's defined, actually. Hope I'm not being too pedantic.

You are not by those terms are quite non-intuitive for some.
Apparently Erik has problems and so do I: I have to think hard every
time I have to remember if
class C;

is a declaration or a definition. To me it is both, as C gets defined
as a class,
It doesn't. It's only declared as a class. However,

class C {};

is both a declaration and a definition. Definitions are always also
declarations, but not the other way round.
Think about the sentence: "I declare war!". That just means that you say
that there is now war. Same if you declare a type/object/function. You just
tell the compiler that it exists. Now someone says: "Please define war!",
in which case an answer would be an explanation of what war is exactly.
Similarly in C++, a definition actually contains all the details.

Hope that helps you remember the meaning of the terms.

Jan 31 '07 #8
On 31 Gen, 16:36, "Andrew Koenig" <a...@acm.orgwrote:
"Erik Wikström" <eri...@student.chalmers.sewrote in message

news:11**********************@q2g2000cwa.googlegro ups.com...
On Jan 31, 1:46 pm, "deltaquattro" <deltaquat...@gmail.comwrote:
The value of a const can only be set once, when it's declared (const
class members are a bit different)

When it's defined, actually. Hope I'm not being too pedantic.
Hi, Andrew,

I'm now learning the language so I'd like to understand the difference
between "define" and "declare". I thought declaration was the act of
associating a type and an identifier (name) to a variable. The type
allows the compiler to interpret statements correctly, allocating the
correct amount of storage. For example,

int n, i, j, k;
float x1, x2;

should be declaration (type + name of variables). In the same way, I'd
think

const std::string store

would be the declaration of store: I'm saying that store is a constant
of type std::string, named store. Why do you say it's the definition
of store, instead?
Could this be related to the fact that the type (object?) std::string
is part of a namespace, while int and float are "primitive" types?

greetings,

deltaquattro

Jan 31 '07 #9
On 31 Jan., 19:35, Rolf Magnus <ramag...@t-online.dewrote:
peter koch wrote:
On Jan 31, 4:36 pm, "Andrew Koenig" <a...@acm.orgwrote:
"Erik Wikström" <eri...@student.chalmers.sewrote in message
>news:11**********************@q2g2000cwa.googlegr oups.com...
On Jan 31, 1:46 pm, "deltaquattro" <deltaquat...@gmail.comwrote:
The value of a const can only be set once, when it's declared (const
class members are a bit different)
When it's defined, actually. Hope I'm not being too pedantic.
You are not by those terms are quite non-intuitive for some.
Apparently Erik has problems and so do I: I have to think hard every
time I have to remember if
class C;
is a declaration or a definition. To me it is both, as C gets defined
as a class,

It doesn't. It's only declared as a class. However,

class C {};

is both a declaration and a definition. Definitions are always also
declarations, but not the other way round.
Think about the sentence: "I declare war!". That just means that you say
that there is now war. Same if you declare a type/object/function. You just
tell the compiler that it exists. Now someone says: "Please define war!",
in which case an answer would be an explanation of what war is exactly.
Similarly in C++, a definition actually contains all the details.

Hope that helps you remember the meaning of the terms
Thanks Rolf

War is wonderful! While I knew about the technicalities, I now can
also remember their names.

/Peter

Jan 31 '07 #10
deltaquattro wrote:
On 31 Gen, 16:36, "Andrew Koenig" <a...@acm.orgwrote:
>"Erik Wikström" <eri...@student.chalmers.sewrote in message

news:11**********************@q2g2000cwa.googlegr oups.com...
On Jan 31, 1:46 pm, "deltaquattro" <deltaquat...@gmail.comwrote:
The value of a const can only be set once, when it's declared (const
class members are a bit different)

When it's defined, actually. Hope I'm not being too pedantic.

Hi, Andrew,

I'm now learning the language so I'd like to understand the difference
between "define" and "declare". I thought declaration was the act of
associating a type and an identifier (name) to a variable. The type
allows the compiler to interpret statements correctly, allocating the
correct amount of storage. For example,

int n, i, j, k;
float x1, x2;

should be declaration (type + name of variables). In the same way, I'd
think

const std::string store

would be the declaration of store: I'm saying that store is a constant
of type std::string, named store. Why do you say it's the definition
of store, instead?
Not instead, but in addition. The declaration of an object just says that
there is an object of that type somewhere. The definition actually reserves
space for it.
Could this be related to the fact that the type (object?) std::string
is part of a namespace, while int and float are "primitive" types?
No.

Jan 31 '07 #11
peter koch wrote:
On 31 Jan., 19:35, Rolf Magnus <ramag...@t-online.dewrote:
>peter koch wrote:
On Jan 31, 4:36 pm, "Andrew Koenig" <a...@acm.orgwrote:
"Erik Wikström" <eri...@student.chalmers.sewrote in message
>>news:11**********************@q2g2000cwa.googleg roups.com...
On Jan 31, 1:46 pm, "deltaquattro" <deltaquat...@gmail.comwrote:
The value of a const can only be set once, when it's declared (const
class members are a bit different)
>When it's defined, actually. Hope I'm not being too pedantic.
You are not by those terms are quite non-intuitive for some.
Apparently Erik has problems and so do I: I have to think hard every
time I have to remember if
class C;
is a declaration or a definition. To me it is both, as C gets defined
as a class,

It doesn't. It's only declared as a class. However,

class C {};

is both a declaration and a definition. Definitions are always also
declarations, but not the other way round.
Think about the sentence: "I declare war!". That just means that you say
that there is now war. Same if you declare a type/object/function. You
just tell the compiler that it exists. Now someone says: "Please define
war!", in which case an answer would be an explanation of what war is
exactly. Similarly in C++, a definition actually contains all the
details.

Hope that helps you remember the meaning of the terms

Thanks Rolf

War is wonderful! While I knew about the technicalities, I now can
also remember their names.
Indeed, the war analogy is truly striking: at the point of declaration, the
size may be unknown.
Best

Kai-Uwe Bux
Jan 31 '07 #12
On Wed, 31 Jan 2007 14:28:52 -0500, Kai-Uwe Bux <jk********@gmx.netwrote:

>>is both a declaration and a definition. Definitions are always also
declarations, but not the other way round.
Think about the sentence: "I declare war!". That just means that you say
that there is now war. Same if you declare a type/object/function. You
just tell the compiler that it exists. Now someone says: "Please define
war!", in which case an answer would be an explanation of what war is
exactly. Similarly in C++, a definition actually contains all the
details.

Hope that helps you remember the meaning of the terms

Thanks Rolf

War is wonderful! While I knew about the technicalities, I now can
also remember their names.

Indeed, the war analogy is truly striking: at the point of declaration, the
size may be unknown.
Another classic in the making.

-dr
Jan 31 '07 #13
On 31 Gen, 20:08, Rolf Magnus <ramag...@t-online.dewrote:
deltaquattro wrote:
On 31 Gen, 16:36, "Andrew Koenig" <a...@acm.orgwrote:
"Erik Wikström" <eri...@student.chalmers.sewrote in message
>news:11**********************@q2g2000cwa.googlegr oups.com...
On Jan 31, 1:46 pm, "deltaquattro" <deltaquat...@gmail.comwrote:
The value of a const can only be set once, when it's declared (const
class members are a bit different)
When it's defined, actually. Hope I'm not being too pedantic.
Hi, Andrew,
I'm now learning the language so I'd like to understand the difference
between "define" and "declare". I thought declaration was the act of
associating a type and an identifier (name) to a variable. The type
allows the compiler to interpret statements correctly, allocating the
correct amount of storage. For example,
int n, i, j, k;
float x1, x2;
should be declaration (type + name of variables). In the same way, I'd
think
const std::string store
would be the declaration of store: I'm saying that store is a constant
of type std::string, named store. Why do you say it's the definition
of store, instead?

Not instead, but in addition. The declaration of an object just says that
there is an object of that type somewhere. The definition actually reserves
space for it.
Hi, Rolf,

thank you for the explaination: can you make an example of declaring a
variable without defining it? Thank you very much,

greetings,

deltaquattro
Feb 1 '07 #14
On Feb 1, 1:40 pm, "deltaquattro" <deltaquat...@gmail.comwrote:
On 31 Gen, 20:08, Rolf Magnus <ramag...@t-online.dewrote:
deltaquattro wrote:
On 31 Gen, 16:36, "Andrew Koenig" <a...@acm.orgwrote:
>"Erik Wikström" <eri...@student.chalmers.sewrote in message
>>news:11**********************@q2g2000cwa.googleg roups.com...
>On Jan 31, 1:46 pm, "deltaquattro" <deltaquat...@gmail.comwrote:
The value of a const can only be set once, when it's declared (const
class members are a bit different)
>When it's defined, actually. Hope I'm not being too pedantic.
Hi, Andrew,
I'm now learning the language so I'd like to understand the difference
between "define" and "declare". I thought declaration was the act of
associating a type and an identifier (name) to a variable. The type
allows the compiler to interpret statements correctly, allocating the
correct amount of storage. For example,
int n, i, j, k;
float x1, x2;
should be declaration (type + name of variables). In the same way, I'd
think
const std::string store
would be the declaration of store: I'm saying that store is a constant
of type std::string, named store. Why do you say it's the definition
of store, instead?
Not instead, but in addition. The declaration of an object just says that
there is an object of that type somewhere. The definition actually reserves
space for it.

Hi, Rolf,

thank you for the explaination: can you make an example of declaring a
variable without defining it? Thank you very much,
You can declare a variable without defining it using the extern
keyword.

extern int i;

If you do you have to make sure that you somewhere else in your code
(or some library that you use) defines the variable or you'll get an
error from the linker. I've never had to use this in C++, but have
used it when writing some C. Below is a small example.

/*---- Foo.h ----*/
#ifndef FOO_H
#define FOO_H

void foo();

#endif

/*---- Foo.cpp ----*/
#include "Foo.h"

extern int global;

void foo()
{
++global;
}

/*---- Main.cpp ----*/
#include <iostream>

#include "Foo.h"

int global;

int main()
{
global = 4;
foo();
std::cout << global;
}

--
Erik Wikström

Feb 1 '07 #15
On 1 Feb, 13:56, "Erik Wikström" <eri...@student.chalmers.sewrote:
[..]
Not instead, but in addition. The declaration of an object just says that
there is an object of that type somewhere. The definition actually reserves
space for it.
Hi, Rolf,
thank you for the explaination: can you make an example of declaring a
variable without defining it? Thank you very much,

You can declare a variable without defining it using the extern
keyword.

extern int i;

If you do you have to make sure that you somewhere else in your code
(or some library that you use) defines the variable or you'll get an
error from the linker. I've never had to use this in C++, but have
used it when writing some C. Below is a small example.
[..]

Ok, thanks: the concept is similar to that of global variables in
Fortran. You were all very helpful, thanks,

greetings,

deltaquattro

Feb 2 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4605
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
19
1921
by: JustSomeGuy | last post by:
I have a class that has a static member variable. string x; x should never change during use and should be intialized to "abcd". How does one do this?
4
2188
by: Anton Pervukhin | last post by:
Hi everybody! I have a small problem regarding the initialization of pointer to the file stream under some conditions. Imagine a class which has a pointer to output file stream and some...
6
2857
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
7
1856
by: Mr. Mountain | last post by:
In the following code I simulate work being done on different threads by sleeping a couple methods for about 40 ms. However, some of these methods that should finish in about 40 -80 ms take as long...
5
2367
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
7
3994
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const...
3
5827
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
10
1857
by: Cliff | last post by:
Greetings, I have been trying to teach myself C++ over the past few weeks and have finally came across a problem I could not fix. I made a simple program that prints out a square or rectangle...
3
1595
by: Torsten Wiebesiek | last post by:
Hi folks, currently I'm writing image classes for easier handling of Intel's IPP library. My idea is to have to different classes. One class that represents a complete image and deals with all...
0
7125
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
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
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...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.