473,624 Members | 2,245 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 "Accelerate d 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 1564
deltaquattro wrote:
Hi,

thanks to your suggestions I got "Accelerate d 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, "deltaquatt ro" <deltaquat...@g mail.comwrote:
Hi,

thanks to your suggestions I got "Accelerate d 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.sewro te:
On Jan 31, 1:46 pm, "deltaquatt ro" <deltaquat...@g mail.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.sewro te in message
news:11******** **************@ q2g2000cwa.goog legroups.com...
On Jan 31, 1:46 pm, "deltaquatt ro" <deltaquat...@g mail.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.orgwr ote:
"Erik Wikström" <eri...@student .chalmers.sewro te in message

news:11******** **************@ q2g2000cwa.goog legroups.com...
On Jan 31, 1:46 pm, "deltaquatt ro" <deltaquat...@g mail.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.orgwr ote:
>"Erik Wikström" <eri...@student .chalmers.sewro te in message

news:11******* *************** @q2g2000cwa.goo glegroups.com.. .
On Jan 31, 1:46 pm, "deltaquatt ro" <deltaquat...@g mail.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 Jan., 19:35, Rolf Magnus <ramag...@t-online.dewrote:
peter koch wrote:
On Jan 31, 4:36 pm, "Andrew Koenig" <a...@acm.orgwr ote:
"Erik Wikström" <eri...@student .chalmers.sewro te in message
>news:11******* *************** @q2g2000cwa.goo glegroups.com.. .
On Jan 31, 1:46 pm, "deltaquatt ro" <deltaquat...@g mail.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 #9
deltaquattro wrote:
On 31 Gen, 16:36, "Andrew Koenig" <a...@acm.orgwr ote:
>"Erik Wikström" <eri...@student .chalmers.sewro te in message

news:11******* *************** @q2g2000cwa.goo glegroups.com.. .
On Jan 31, 1:46 pm, "deltaquatt ro" <deltaquat...@g mail.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 #10

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

Similar topics

1
4614
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, section "Static initialization dependency". There is a example to show how to solve the prolem involved with a technique first poineered by Jerry Schwarz while creating the iostream library (because the definitions for cin, cout, and cerr are static...
19
1940
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
2201
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 additinional methods to deal with it, i.e. open/close/write: classA { private: const std::auto_ptr<std::ofstream> filePtr;
6
2871
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 suggestion for improvements or conversion to iteration would be much appreciated
7
1864
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 as 2300 ms to complete. This is fairly rare, but the test code below will definitely show it. Somehow, I must not have my design right. The idea of this code is that I do two different types of processing ( 1-starting and 2-ending) based on...
5
2383
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
4016
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 member inside my class. class Packet { static const int session_id;
3
5843
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 section describes a solution using methods returning references to static objects. But consider:
10
1867
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 using the * character. The program was just for practice but I am having problems. My main problem is, in my program I use 4 functions to change or access two variables in my code. The variables are
3
1610
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 the memeory management stuff, and another class, that is only a tile of the image. Let's call the first MemoryImage, and the second ReferenceImage. My code works well for creating non const and const reference images to a non
0
8170
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8334
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6108
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4078
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1482
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.