By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

more related to const

P: n/a
Hello, I dont know exact subject line for this question.
In my class construcor I will read a value into a variable(member
variable or a global variable). I need that variable to be constant
throught the program & that value is not known until the constructor
is invoked. To be more precise I need the variable to remain constant
throughout the program execution but the value is not known while
declaring it. How to declare such varibles?. It is ok if that variable
is declared inside the class or outside the class as global varible.

Jul 5 '06 #1
Share this Question
Share on Google+
19 Replies


P: n/a

pa******@gmail.com wrote:
Hello, I dont know exact subject line for this question.
In my class construcor I will read a value into a variable(member
variable or a global variable). I need that variable to be constant
throught the program & that value is not known until the constructor
is invoked. To be more precise I need the variable to remain constant
throughout the program execution but the value is not known while
declaring it. How to declare such varibles?. It is ok if that variable
is declared inside the class or outside the class as global varible.
you need to know about const member variables.

-- Murali Krishna

Jul 5 '06 #2

P: n/a
pa******@gmail.com wrote:
Hello, I dont know exact subject line for this question.
In my class construcor I will read a value into a variable(member
variable or a global variable). I need that variable to be constant
throught the program & that value is not known until the constructor
is invoked. To be more precise I need the variable to remain constant
throughout the program execution but the value is not known while
declaring it. How to declare such varibles?. It is ok if that variable
is declared inside the class or outside the class as global varible.
Something like this?

struct X
{
const int value;

X( int n ) : value(n) {}
};
--
Ian Collins.
Jul 5 '06 #3

P: n/a
Murali Krishna wrote:
>
you need to know about const member variables.

-- Murali Krishna
You are right. Actually I was under the impression that const variables
should be given value at the time of declaration.Sorry for such a silly
question.

Jul 5 '06 #4

P: n/a
pa******@gmail.com wrote:
Murali Krishna wrote:
>>you need to know about const member variables.

-- Murali Krishna


You are right. Actually I was under the impression that const variables
should be given value at the time of declaration.Sorry for such a silly
question.
Not silly at all, just a misunderstanding.

--
Ian Collins.
Jul 5 '06 #5

P: n/a

Ian Collins wrote:
Not silly at all, just a misunderstanding.

--
Ian Collins.
Thank you. We can declare a member variable to be const and assign it
in constructor.But is there any way where we can declare a const global
variable & assign it later.

Jul 5 '06 #6

P: n/a
pa******@gmail.com wrote:
Ian Collins wrote:

>>Not silly at all, just a misunderstanding.

--
Ian Collins.


Thank you. We can declare a member variable to be const and assign it
in constructor.But is there any way where we can declare a const global
variable & assign it later.
No, otherwise it wouldn't be a constant!

--
Ian Collins.
Jul 5 '06 #7

P: n/a
pa******@gmail.com wrote:
Ian Collins wrote:

>>Not silly at all, just a misunderstanding.

--
Ian Collins.


Thank you. We can declare a member variable to be const and assign it
in constructor.But is there any way where we can declare a const global
variable & assign it later.
This might do what you want:

//global.h
extern const type& global;

//global.cpp

namespace
{
type realglobal;
}

const type& global = realglobal;

Now, realglobal can be modified to your hearts content by code in
global.cpp, in whatever controlled manner you want, but access to it
from everywhere else is const.

Tom
Jul 5 '06 #8

P: n/a

pa******@gmail.com wrote:
But is there any way where we can declare a const global
variable & assign it later.
No. I dont it is possible.

-- Murali Krishna

Jul 5 '06 #9

P: n/a

Tom Widmer wrote:
This might do what you want:

//global.h
extern const type& global;

//global.cpp

namespace
{
type realglobal;
}

const type& global = realglobal;

Now, realglobal can be modified to your hearts content by code in
global.cpp, in whatever controlled manner you want, but access to it
from everywhere else is const.

Tom
Really great idea.What is the logic behind this?

Jul 5 '06 #10

P: n/a

pa******@gmail.com wrote:
Tom Widmer wrote:
This might do what you want:

//global.h
extern const type& global;

//global.cpp

namespace
{
type realglobal;
}

const type& global = realglobal;

Now, realglobal can be modified to your hearts content by code in
global.cpp, in whatever controlled manner you want, but access to it
from everywhere else is const.

Tom

Really great idea.What is the logic behind this?
Tom, have you compiled the code well? I don't think it is possible.

extern storage class is used to access a variable that is not in
current scope and which is in process scope.

if declare extern reference of a constant, that should have already
been initialized. (reference)
so after initialization it is as good as #define. How can you change it
again?

Can you plz send the complete code?

-- Murali Krishna

Jul 5 '06 #11

P: n/a
In article <e8**********@nntp.aioe.org>,
Tom Widmer <to********@hotmail.comwrote:
pa******@gmail.com wrote:
Ian Collins wrote:

>Not silly at all, just a misunderstanding.

--
Ian Collins.

Thank you. We can declare a member variable to be const and assign it
in constructor.But is there any way where we can declare a const global
variable & assign it later.

This might do what you want:

//global.h
extern const type& global;

//global.cpp

namespace
{
type realglobal;
}

const type& global = realglobal;

Now, realglobal can be modified to your hearts content by code in
global.cpp, in whatever controlled manner you want, but access to it
from everywhere else is const.
I'm not so sure this will work. When dealing with a const variable, the
compiler is allowed to assume it never changes and thus can check the
memory location only once then never again. I've tried something like
the above before and it hasn't worked for me.

If you really want to do something like the above, I would wrap it in a
function.

// header

const type& getGlobal();

// cpp file

namespace {
type global;
}

const type& getGlobal() {
return global;
}
Jul 5 '06 #12

P: n/a
Murali Krishna wrote:
Can you plz send the complete code?

-- Murali Krishna
I dont know much abt it but here is a code which i have checked

/***a.h***/
extern const unsigned int& temp ;
/****end of a.h***/

/***a.cpp***/
unsigned int temp2 ;
int main(){
temp2 = 20 ;
cout << "\ntemp =" << temp ; //prints 20
temp2 = 40 ;
cout << "\ntemp =" << temp ; //prints 40
} //end of main
const unsigned int& temp =temp2 ;
/****end of a.cpp***/

Main problem is even though we have made temp to be const, it will
alter if temp2 is changed.

Jul 5 '06 #13

P: n/a
Daniel T. wrote:
In article <e8**********@nntp.aioe.org>,
Tom Widmer <to********@hotmail.comwrote:
>>This might do what you want:

//global.h
extern const type& global;

//global.cpp

namespace
{
type realglobal;
}

const type& global = realglobal;

Now, realglobal can be modified to your hearts content by code in
global.cpp, in whatever controlled manner you want, but access to it
from everywhere else is const.


I'm not so sure this will work. When dealing with a const variable, the
compiler is allowed to assume it never changes and thus can check the
memory location only once then never again. I've tried something like
the above before and it hasn't worked for me.
The compiler isn't dealing with a const variable, but a const reference.
It can't make any assumptions about the value of that variable not
changing. e.g.

void f(int const& i)
{
std::cout << i;
g();
std::cout << i;
}

In that code, the compiler will reload the value of i for the second
call, if it can't tell whether or not g has modified it.
If you really want to do something like the above, I would wrap it in a
function.

// header

const type& getGlobal();

// cpp file

namespace {
type global;
}

const type& getGlobal() {
return global;
}
Obviously, that works too. Global variables are generally a bad idea in
any case.

Tom
Jul 5 '06 #14

P: n/a
pa******@gmail.com wrote:
Murali Krishna wrote:
Can you plz send the complete code?

-- Murali Krishna

I dont know much abt it but here is a code which i have checked

/***a.h***/
extern const unsigned int& temp ;
/****end of a.h***/

/***a.cpp***/
unsigned int temp2 ;
int main(){
temp2 = 20 ;
cout << "\ntemp =" << temp ; //prints 20
temp2 = 40 ;
cout << "\ntemp =" << temp ; //prints 40
} //end of main
const unsigned int& temp =temp2 ;
/****end of a.cpp***/

Main problem is even though we have made temp to be const, it will
alter if temp2 is changed.
you asked in the previous reply..
But is there any way where we can declare a const global
variable & assign it later.
This has nothing to with the above code. I'll try to explain breifly.

reference variable creates an alias to another variable of same type.

so if you say..

int i = 10;
int& ref = i;

variables ref and i are same. meaning they both hold same address.
unsigned int temp2 ;
is a normal auto storage class variable. Change it when ever you want.
const unsigned int& temp =temp2 ;
temp is const alias of temp2. so.. what's the point?

temp always has to be alias of temp2. This is what const ref mean.

try to reference to another variable. it will give an error.

So you did not change value of const variable. and you cannot change
reference alias also.

const type& is different from const type.

-- Murali Krishna

Jul 5 '06 #15

P: n/a
pa******@gmail.com wrote:
Hello, I dont know exact subject line for this question.
In my class construcor I will read a value into a variable(member
variable or a global variable). I need that variable to be constant
throught the program & that value is not known until the constructor
is invoked. To be more precise I need the variable to remain constant
throughout the program execution but the value is not known while
declaring it. How to declare such varibles?. It is ok if that variable
is declared inside the class or outside the class as global varible.
Can't you just declare your variable as private, and only access it
through an accesor function setVariable() which checks if it has already
been set? So YOU actually assure that it's constant (but you don't get
the speed up from using const then...)
Phil

-pseudo code
bool varHasBeenSet = False;
int myVar = 0;

bool setVariable(int i){
if(! varHasBeenSet){
myVar = i;
return true;
}
return false;
}
In constructor
MyClass::MyClass(int argument){
setVariable(argument);
}

Jul 5 '06 #16

P: n/a
Philipp wrote:
Sorry I meant
-pseudo code
bool varHasBeenSet = False;
int myVar = 0;

bool setVariable(int i){
if(! varHasBeenSet){
myVar = i;
varHasBeenSet = true;
return true;
}
return false;
}
In constructor
MyClass::MyClass(int argument){
setVariable(argument);
}
Jul 5 '06 #17

P: n/a

Philipp wrote:
Can't you just declare your variable as private, and only access it
through an accesor function setVariable() which checks if it has already
been set? So YOU actually assure that it's constant (but you don't get
the speed up from using const then...)
If it were a member varible, I would have used const instead and
assigned it in the constructor. The discussion is the situation if the
varible is a global variable.

Jul 5 '06 #18

P: n/a
pa******@gmail.com wrote:
Tom Widmer wrote:
>>This might do what you want:

//global.h
extern const type& global;

//global.cpp

namespace
{
type realglobal;
}

const type& global = realglobal;

Now, realglobal can be modified to your hearts content by code in
global.cpp, in whatever controlled manner you want, but access to it
from everywhere else is const.

Tom


Really great idea.What is the logic behind this?
What do you mean? Do you mean, "Why does it work?"

Well, realglobal is in an anonymous namespace, which means that it can
only be directly accessed from within global.cpp - you don't need to
worry about any other code modifying it. All other translation units see
it through the global reference, which is const, hence those TUs cannot
use the reference to modify realglobal (unless they employ the nasty
hack of const_cast).

Tom
Jul 5 '06 #19

P: n/a
pa******@gmail.com wrote:
Murali Krishna wrote:
>Can you plz send the complete code?
I dont know much abt it but here is a code which i have checked

/***a.h***/
extern const unsigned int& temp ;
/****end of a.h***/

/***a.cpp***/
unsigned int temp2 ;
int main(){
temp2 = 20 ;
cout << "\ntemp =" << temp ; //prints 20
temp2 = 40 ;
cout << "\ntemp =" << temp ; //prints 40
} //end of main
const unsigned int& temp =temp2 ;
/****end of a.cpp***/

Main problem is even though we have made temp to be const, it will
alter if temp2 is changed.
Problem? That is what all the fuss was about! Read the type of
'temp' from right to left and you will see that 'temp' is not const. It
is a reference *to* a constant unsigned integer. This means, you may not
change the integer thru that reference. It does not mean, it cannot be
changed by other means.

hth
--
jb

(reply address in rot13, unscramble first)
Jul 5 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.