|
|
I want to know is it possible to to make a constant variable to hold a value the user of my application at the run of the program choose and i want him to not not chang it again.
thank you
******************
******************
** Ahmed Ismail **
******************
******************
|
|
|
|
|
Ahmed Ismail Mohamed wrote: I want to know is it possible to to make a constant variable to hold a value the user of my application at the run of the program choose and i want him to not not chang it again.
const is an official C++ qualifier.
|
|
|
|
|
but i think that a pointer have the address of const var can do changes, but when i make that i failed
Ahemd Ismail
|
|
|
|
|
If you are declaring a variable as const, and do not want to change it, why on earth are you trying to change it?
|
|
|
|
|
Hello
Ahmed Ismail Mohamed wrote: I want to know is it possible to to make a constant variable to hold a value the user of my application at the run of the program choose
No problem! Make an input and store in that variable.
Ahmed Ismail Mohamed wrote: and i want him to not not chang it again.
Don't call the above function again!!
PS.
I know you are talking about the const_cast operator, but why??
Here is a sample code for what you want quoted from MSDN:
#include <iostream>
using namespace std;
class CCTest {
public:
void setNumber( int );
void printNumber() const;
private:
int number;
};
void CCTest::setNumber( int num ) { number = num; }
void CCTest::printNumber() const {
cout << "\nBefore: " << number;
const_cast< CCTest * >( this )->number--;
cout << "\nAfter: " << number;
}
int main() {
CCTest X;
X.setNumber( 8 );
X.printNumber();
}
-- modified at 1:16 Sunday 3rd September, 2006
In addition to the above, I forgot to mention another more standard way of doing what you want:
1- Declare your data type as a const member of a class.
2- Take the input from the user in a temp variable.
3- Put the value in your const member using the initialization list of the class' constructor.
Regards
|
|
|
|
|
The Standard of English in the phrasing of this request was so bad, I still do not understand what it was all about. The Code snippet above is quite clear, but I've still no idea what question it was realy supposed to answer. I suggest that the poser of this question purchases a copy of Kernighan & Ritchie, and Read it from Cover to Cover.
If nothing else, this will introduce the poser of the question to the English Language
LateNightsInNewry
|
|
|
|
|
Hello
LateNightsInNewry wrote: I still do not understand what it was all about
I believe he was asking about casting a const variable to remove its constancy, change its value once, then recasting it back into a const .
LateNightsInNewry wrote: this will introduce the poser of the question to the English Language
Come on! You already have spent enough time around here to learn CP language!;)
Regards
|
|
|
|
|
Nader Elshehabi wrote: I believe he was asking about casting a const variable to remove its constancy
not so. i believe he wants to know how to initialize a const var whose value will be known only at runtime.
your answer though was correct. making the const var a class member can delay init till runtime. but it is expensive. a better way is to put it in a function as a static const and then init it with an incoming value which has a default. like this:
const int var_name (const int var_val = 0)<br />
{<br />
static const int var = var_val;<br />
return var;<br />
}
then you can use it like this:
var_name (temp1);
temp3 = var_name (temp2);
but beware that if your first call doesn't pass a value the default will be set.
one more advantage is that this way it is much more difficult to use a pointer and modify the value.
-- modified at 5:46 Sunday 3rd September, 2006
----------------------
Mayank Thakore
Learning C++ - since 1998
They didn't print my card right; so I resigned.
|
|
|
|
|
Hello
Thank you for your reply Mayank.
MayankT wrote: but it is expensive
You didn't specify in what exactly, but I'll assume you mean in excution speed. Why? What's the difference between declaring a variable as a class member or as a static in a function in a class??
Also in your way you store the true value in the static member. But from your code most of the work will be on the returned value not the original const . You can change temp3 to whatever you want after the call. While in the initialization list of a constructor -i hope you read my modified post-, you set that memebr to public and operate on it with no problem!
Again, about the whole idea!! I can't think of one good practical -not hypothtical- scenario where we should use this approach instead of just making our variable as private, or just use another approach!!
MayankT wrote: one more advantage is that this way it is much more difficult to use a pointer and modify the value.
I hope you don't mean reverse engineering? From what I heard there are monsters out there that can decipher assembly as easy as you read english! With a cup of coffee, some brains, and a good memory dump tool, you can't hide aything anywhere!!
Regards
|
|
|
|
|
Nader Elshehabi wrote: What's the difference between declaring a variable as a class member or as a static in a function in a class??
Actually I meant that a class might not be necessary. And if it is an existing class that is being used then it could be difficult to change the design in a manner as to provide the var at object creation time.
This function can be put outside a class or if inside it wont require change to construction code.
One difference is that the member var will be unique to each object but as static in a function in a class it will have to be shared.
Nader Elshehabi wrote: You can change temp3 to whatever you want after the call.
Right! I was tempted to return a val by ref but realized that the guy had already tried to use pointers to manipulate a const var. So this seems safer. Also, even a public class member can be assigned to a temp and anything can be done.
Nader Elshehabi wrote: scenario where we should use this approach
as in the differences above
Nader Elshehabi wrote: I hope you don't mean reverse engineering?
Not at all . Only in this case even if someone uses a pointer to the returned val they will not be changing the actual var storing the const.
Actually, only the first line was for you Yes your approach is excellent if there is an existing class which is being instantiated after the value is known. I should have thought about that. Thanks for the reply!
----------------------
Mayank Thakore
Learning C++ - since 1998
They didn't print my card right; so I resigned.
|
|
|
|
|
Hello
Thanks Mayank for your replies!
It was really nice to have this talk with you.
Regards
|
|
|
|
|
hi, i am loading a text file into this buffer , FILE *buffer = fopen("text.txt", "r");
i need to print it out again in a LPCTSTR pszText, i need a simple conversion for that thanks.
|
|
|
|
|
You will need to allocate a buffer and call fread()
FILE *infile;
if ((infile = _tfopen(sFilename,_T("rb") )) == NULL) {
return FALSE;
}
fseek(infile , 0 , SEEK_END);
long lSize = ftell(infile);
rewind(infile);
char* pszIn = new char[lSize];
fread(pszIn,1,lSize,infile);
fclose(infile);
|
|
|
|
|
Ok that worked but it triggers and character test assert (<= 256) and if i ignore it i get the blablaí0
the text file contained blabla
|
|
|
|
|
Could you show me the relevent code? Where is the failiure happening?
|
|
|
|
|
text is the final conversion, taken from bellow. m_pTextBuffer->InsertText(this, ptCursorPos.y, ptCursorPos.x, text, y, x, CE_ACTION_PASTE);
void OnScriptMission()
{
if (m_pTextBuffer == NULL)
return;
m_pTextBuffer->BeginUndoGroup();
FILE *buffer;
if ((buffer = _tfopen("text.txt",_T("rb") )) == NULL) {
return;
}
// obtain file size.
fseek(buffer , 0 , SEEK_END);
long lSize = ftell(buffer);
rewind(buffer);
//read in the contents of the input file
char *text = new char[lSize];
fread(text,1,lSize,buffer);
CPoint ptCursorPos = GetCursorPos();
ASSERT_VALIDTEXTPOS(ptCursorPos);
int x, y;
m_pTextBuffer->InsertText(this, ptCursorPos.y, ptCursorPos.x, text, y, x, CE_ACTION_PASTE);
ptCursorPos.x = x;
ptCursorPos.y = y;
ASSERT_VALIDTEXTPOS(ptCursorPos);
SetAnchor(ptCursorPos);
SetSelection(ptCursorPos, ptCursorPos);
SetCursorPos(ptCursorPos);
EnsureVisible(ptCursorPos);
m_pTextBuffer->FlushUndoGroup(this);
fclose(buffer);
}
|
|
|
|
|
It;s hard to tell where it's going wrong from the code you posted. What is the insertText() function? Also, since you're already using MFC, why not use the CFile class to read the file?
|
|
|
|
|
hardcoded char array works though
insertText is to keep track on undo \redo etc, but ive tested hardcoded data , and they work so i doubt there is anything wrong with it.
i can give CFile a try, thanks for your help.
|
|
|
|
|
this fixed the problem
//read in the contents of the input file
char *text = new char[lSize+1];
fread(text,1,lSize,buffer);
text[lSize] = 0;
// //
a newline ...
|
|
|
|
|
Ah yes, my mistake, you are dealing with strings so you would need to allow for the null terminator. That snippet comes from a binary file reader where the extra character would corrupt my data stream.
|
|
|
|
|
I want to develop several lines of text within my program. Then I want to display all the lines in an edit box at the same time. Say I have ten lines of text. I want to manipulate those lines, then display all ten lines at one time in the edit box. The edit box would never show more than ten lines at any one time.
Problem: I can't get more than one line at a time to display.
Edit box is set to multiline and want return.
Here is what I tried:
void CTestEditBoxDlg::OnButton1()
{
void CTestEditBoxDlg::OnButton1()
{
CString sString1 = "First Line\nSecond Line\n...Last Line\n";
m_Edit1 = sString1;
UpdateData(FALSE);
}
}
Any ideas? Is an edit box the wrong approach?
|
|
|
|
|
Sorry. I pasted too much code. Here is the code I used:
void CTestEditBoxDlg::OnButton1()
{
CString sString1 = "First Line\nSecond Line\n...Last Line\n";
m_Edit1 = sString1;
UpdateData(FALSE);
}
|
|
|
|
|
specify "\r\n" as a carriage-return/line-feed combination
|
|
|
|
|
The \r\n worked perfectly. Thanks.
|
|
|
|