Problems with initialization of const

Discussion in 'C++' started by deltaquattro, Jan 31, 2007.

  1. deltaquattro

    deltaquattro Guest

    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
    deltaquattro, Jan 31, 2007
    #1
    1. Advertising

  2. deltaquattro

    Rolf Magnus Guest

    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.
    Rolf Magnus, Jan 31, 2007
    #2
    1. Advertising

  3. On Jan 31, 1:46 pm, "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,


    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
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 31, 2007
    #3
  4. deltaquattro

    deltaquattro Guest

    On 31 Gen, 14:09, "Erik Wikström" <> wrote:
    > On Jan 31, 1:46 pm, "deltaquattro" <> wrote:

    [..]
    > > 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
    deltaquattro, Jan 31, 2007
    #4
  5. deltaquattro

    deltaquattro Guest

    On 31 Gen, 13:57, Rolf Magnus <> wrote:
    > 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
    deltaquattro, Jan 31, 2007
    #5
  6. "Erik Wikström" <> wrote in message
    news:...
    On Jan 31, 1:46 pm, "deltaquattro" <> wrote:

    > 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.
    Andrew Koenig, Jan 31, 2007
    #6
  7. deltaquattro

    peter koch Guest

    On Jan 31, 4:36 pm, "Andrew Koenig" <> wrote:
    > "Erik Wikström" <> wrote in message
    >
    > news:...
    > On Jan 31, 1:46 pm, "deltaquattro" <> wrote:
    >
    > > 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
    peter koch, Jan 31, 2007
    #7
  8. deltaquattro

    Rolf Magnus Guest

    peter koch wrote:

    > On Jan 31, 4:36 pm, "Andrew Koenig" <> wrote:
    >> "Erik Wikström" <> wrote in message
    >>
    >> news:...
    >> On Jan 31, 1:46 pm, "deltaquattro" <> wrote:
    >>
    >> > 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.
    Rolf Magnus, Jan 31, 2007
    #8
  9. deltaquattro

    deltaquattro Guest

    On 31 Gen, 16:36, "Andrew Koenig" <> wrote:
    > "Erik Wikström" <> wrote in message
    >
    > news:...
    > On Jan 31, 1:46 pm, "deltaquattro" <> wrote:
    >
    > > 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
    deltaquattro, Jan 31, 2007
    #9
  10. deltaquattro

    Rolf Magnus Guest

    deltaquattro wrote:

    > On 31 Gen, 16:36, "Andrew Koenig" <> wrote:
    >> "Erik Wikström" <> wrote in message
    >>
    >> news:...
    >> On Jan 31, 1:46 pm, "deltaquattro" <> wrote:
    >>
    >> > 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.
    Rolf Magnus, Jan 31, 2007
    #10
  11. deltaquattro

    peter koch Guest

    On 31 Jan., 19:35, Rolf Magnus <> wrote:
    > peter koch wrote:
    > > On Jan 31, 4:36 pm, "Andrew Koenig" <> wrote:
    > >> "Erik Wikström" <> wrote in message

    >
    > >>news:...
    > >> On Jan 31, 1:46 pm, "deltaquattro" <> wrote:

    >
    > >> > 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
    peter koch, Jan 31, 2007
    #11
  12. deltaquattro

    Kai-Uwe Bux Guest

    peter koch wrote:

    > On 31 Jan., 19:35, Rolf Magnus <> wrote:
    >> peter koch wrote:
    >> > On Jan 31, 4:36 pm, "Andrew Koenig" <> wrote:
    >> >> "Erik Wikström" <> wrote in message

    >>
    >> >>news:...
    >> >> On Jan 31, 1:46 pm, "deltaquattro" <> wrote:

    >>
    >> >> > 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
    Kai-Uwe Bux, Jan 31, 2007
    #12
  13. On Wed, 31 Jan 2007 14:28:52 -0500, Kai-Uwe Bux <> wrote:


    >>> 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
    Dave Rahardja, Jan 31, 2007
    #13
  14. deltaquattro

    deltaquattro Guest

    On 31 Gen, 20:08, Rolf Magnus <> wrote:
    > deltaquattro wrote:
    > > On 31 Gen, 16:36, "Andrew Koenig" <> wrote:
    > >> "Erik Wikström" <> wrote in message

    >
    > >>news:...
    > >> On Jan 31, 1:46 pm, "deltaquattro" <> wrote:

    >
    > >> > 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
    deltaquattro, Feb 1, 2007
    #14
  15. On Feb 1, 1:40 pm, "deltaquattro" <> wrote:
    > On 31 Gen, 20:08, Rolf Magnus <> wrote:
    >
    >
    >
    > > deltaquattro wrote:
    > > > On 31 Gen, 16:36, "Andrew Koenig" <> wrote:
    > > >> "Erik Wikström" <> wrote in message

    >
    > > >>news:...
    > > >> On Jan 31, 1:46 pm, "deltaquattro" <> wrote:

    >
    > > >> > 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
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 1, 2007
    #15
  16. deltaquattro

    deltaquattro Guest

    On 1 Feb, 13:56, "Erik Wikström" <> wrote:
    [..]
    > > > 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
    deltaquattro, Feb 2, 2007
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,086
  2. Javier
    Replies:
    2
    Views:
    541
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,087
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    870
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    526
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page