Problems with initialization of const

D

deltaquattro

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
 
R

Rolf Magnus

deltaquattro said:
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.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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
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;'
 
D

deltaquattro

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

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
 
D

deltaquattro

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
 
A

Andrew Koenig

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

peter koch

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
 
R

Rolf Magnus

peter said:
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.
 
D

deltaquattro

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
 
R

Rolf Magnus

deltaquattro said:
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.
 
P

peter koch

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
 
K

Kai-Uwe Bux

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

deltaquattro

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
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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;
}
 
D

deltaquattro

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
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top