Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problems with initialization of const

Reply
Thread Tools

Problems with initialization of const

 
 
peter koch
Guest
Posts: n/a
 
      01-31-2007
On 31 Jan., 19:35, Rolf Magnus <(E-Mail Removed)> wrote:
> peter koch wrote:
> > On Jan 31, 4:36 pm, "Andrew Koenig" <(E-Mail Removed)> wrote:
> >> "Erik Wikström" <(E-Mail Removed)> wrote in message

>
> >>news:(E-Mail Removed) roups.com...
> >> On Jan 31, 1:46 pm, "deltaquattro" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-31-2007
peter koch wrote:

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

>>
>> >>news:(E-Mail Removed) roups.com...
>> >> On Jan 31, 1:46 pm, "deltaquattro" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Dave Rahardja
Guest
Posts: n/a
 
      01-31-2007
On Wed, 31 Jan 2007 14:28:52 -0500, Kai-Uwe Bux <(E-Mail Removed)> 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
 
Reply With Quote
 
deltaquattro
Guest
Posts: n/a
 
      02-01-2007
On 31 Gen, 20:08, Rolf Magnus <(E-Mail Removed)> wrote:
> deltaquattro wrote:
> > On 31 Gen, 16:36, "Andrew Koenig" <(E-Mail Removed)> wrote:
> >> "Erik Wikström" <(E-Mail Removed)> wrote in message

>
> >>news:(E-Mail Removed) roups.com...
> >> On Jan 31, 1:46 pm, "deltaquattro" <(E-Mail Removed)> 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


 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      02-01-2007
On Feb 1, 1:40 pm, "deltaquattro" <(E-Mail Removed)> wrote:
> On 31 Gen, 20:08, Rolf Magnus <(E-Mail Removed)> wrote:
>
>
>
> > deltaquattro wrote:
> > > On 31 Gen, 16:36, "Andrew Koenig" <(E-Mail Removed)> wrote:
> > >> "Erik Wikström" <(E-Mail Removed)> wrote in message

>
> > >>news:(E-Mail Removed) roups.com...
> > >> On Jan 31, 1:46 pm, "deltaquattro" <(E-Mail Removed)> 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

 
Reply With Quote
 
deltaquattro
Guest
Posts: n/a
 
      02-02-2007
On 1 Feb, 13:56, "Erik Wikström" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments