472,972 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,972 software developers and data experts.

more related to const

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
19 1498

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

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

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

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

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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: N3TB1N | last post by:
Let me try again. I could use some help with this assignment, even though my teacher does not grade assignments.but because I need to know this stuff for a test very soon, but haven't been in...
7
by: MLH | last post by:
I have been using variations of the following to determine whether a functional connection to internet exists. I'm thinking of replacing it with a simple PING, but I've never seen any code for...
12
by: Mark P | last post by:
A couple template related questions: 1. In the code pasted below, why does the basic version get selected in the call to foo and the const version get selected in the call to bar? 2. When...
5
by: thetrueaplus | last post by:
Hey all. I have bit shifted my enum values all the way to 31 items. I feel that this api that i am using continues to grow, i will be stuck with out any more items in my enum. I would like...
7
by: T.A. | last post by:
Class hierarchy below demonstrates my problem: #include <vector> #include <boost/smart_ptr.hpp> class Fruit { public: virtual ~Fruit() = 0; };
12
by: swellfr | last post by:
Hi if have simple macro defined this way: #define M_OPplus( classname ) typedef classname operator+(const classname& rhs); and a template class : template<typename X, typename Y> class...
3
by: alishapal | last post by:
Hi there So I am getting these error messages from Intel (v10) and g++ (v4.1.2) compilers respectively: "error: the object has cv-qualifiers that are not compatible with the member function. object...
1
by: nsdevelop12 | last post by:
Is "const Object* pA = new Object(1)" the same as "const Object A(1)" in terms of creating const objects that should not be modified through cast-to-non-const pointers. See the following example:...
2
by: sanjay | last post by:
Hi All, I have a doubt in understanding the output of the following program that i executed on my system. I was using DevC++ IDE which uses minGW based compiler. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.