|
class Text {
public:
void bad( const string &parm ) const;
private:
char *_text;
};
void Text::bad( const string &parm ) const
{
_text = parm.c_str(); // error: _text cannot be modified
for ( int ix = 0; ix < parm.size(); ++ix )
_text[ix] = parm[ix]; // bad style but not an error
}
this example show a pointer refer change a data member in the
const member function , why?
|
|
|
|
|
the function is declared as const (void Text::bad(const string& parm) const; ), that means that it is not allowed to modify any members of the Text class.
exception is with the mutable members however.
modified on Sunday, December 09, 2007 11:13:17 AM
|
|
|
|
|
_text is a char*. The const member promises not to change any of the member variables. Your first assignment changes _text to point to a different string. The second assignment (in the loop) does not change _text, but the content of the string that _text points to (btw, I assume that _text has been assigned to a valid string at some point in code not shown, otherwise the second assignment will corrupt your memory).
const pointers can point to mutable objects. You may change content of the object, but not the content of the pointer.
Cheers
Steen.
"Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006
"Of course, the next day it automatically updates, and your quick'n'dirty patches cause the new binaries to segfault all over your linoleum. If only you'd had the source..." Shog, 10/18/2006
"One day I realized that sadness is just another word for not enough coffee" Wally, 10/18/2006
|
|
|
|
|
Hello,
I have run this logic on turboc++ 3.0 and it is working fine on it but
its not running on msvs2008 c++.
the code does this::
the fun strrep takes 3 char arrays the i.e main,sub,rep.
the sub array is to be found out i the main array and if found its index is taken on found pos,(this is achieved by the searchsub function separately)
then the sub array is replaced in the main array by the rep array,and the rep array can be of different size from the sub array.
i am not able to assign the value like this *temp=*main,where main and
temp are char pointers.
there is a runtime access violation error on msvs..that i am not
able to work out how to resolve..
kind attention and feedback would be invaluable for me...
thanks...the code is as below..
<code>// C++Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
using namespace std;
int i;
int searchsub(char mainstr[],char fndstr[])
{
int mainlen=0,sublen=0,flag=-1,index=0;
for( i=0;mainstr[i]!='\0';i++)
mainlen++;
for( i=0;fndstr[i]!='\0';i++)
sublen++;
for(i=0;i<mainlen;i++)>
{
index=i;
if(mainstr[i]==fndstr[0])
{
for(int k=0;k<sublen;k++)>
{
if(mainstr[i++]!=fndstr[k])
{
flag=0;
break;
}
else flag=1;
}
if(flag==1)
break;
i=index;
}
}
if(flag==1)
return index;
else return -1;
}
char * strrep(char * main,char* sub,char* rep)
{
int sublen=0,mainlen=0,replen=0;
char* save=main;
char*temp;
for( i=0;*rep!='\0';i++,rep++)
replen++;
rep=rep-i;
int index=searchsub(main,sub);
for( i=0;*sub!='\0';i++,sub++)
sublen++;
sub=sub-i;
int reststrindex=index+sublen;
main=main+reststrindex;
for(i=0;*main!='\0';i++,temp++,main++)
*temp=*main;
*temp='\0';
main=main-i-reststrindex;
int templen=0;
temp=temp-i;
for( i=0;*temp!='\0';i++,temp++)
templen++;
temp=temp-i;
main=main+index;
for(i=index;i<index+replen;i++)>
{
*main=*rep;
main++;
rep++;
}
for(i=0;i<templen;i++)>
{
*main=*temp;
main++;
temp++;
}
*main='\0';
main=save;
return main;
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<<strrep("Abcdefghijk","cde","peternorton");
return 0;
}</templen;i++)></index+replen;i++)></sublen;k++)></mainlen;i++)></code>
|
|
|
|
|
how to post codes on this forum...my code is converting into html tags...
please look for this question on this link instead...
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/dddfd4d4b8098a4b#ce1679dc10ec66c0
thanks..
|
|
|
|
|
After you have written your question, select the code then click on the CODE label, below the Text entry window.
This causes the selected text to be treated as code.
|
|
|
|
|
Your code has two major problems: it tries to modify a literal string ("Abcdefghijk") and it will overwrite the area allocated for main if rep is longer than sub . Try this instead:
std::string& strrep(std::string& main, const char* sub, const char* rep) {
if(sub && rep) {
std::string::size_type idx = main.find(sub);
if(idx != std::string::npos) {
return main.replace(idx, strlen(sub), rep);
}
}
return main;
}
|
|
|
|
|
Thanks for replying markkuk,
Firstly i dont want to use any of the inbuilt functions.
secondly why cant i increase the memory space for the main char pointer if the rep>sub,as i think that pointers do give a programmer this ability.. or should i allocate a new memory space using new operator and then carry out the operations on main pointer??
and why this can be done without errors on turbo c++ 3.0 and not in msvs2008 c++??
thanks for showing interest and helping.
|
|
|
|
|
If I try to add some thing in my sample project the error displays like this:-> c:\documents and settings\administrator\desktop\mitumori\mitu\jpeg.h(21) : error C2370: 'kJFIF' : redefinition; different storage class
|
|
|
|
|
keyto wrote: error C2370: 'kJFIF' : redefinition; different storage class
See here.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Hi, if I start up a new dialog using DialogBox, how do I get access to its message pump (GetMessage, PeekMessage, or whatever it uses)? This is without MFC.
The problem is that I have a dialog that I load up that has a lot of other controls on it, and I need to access the messages for those controls specifically (like catching a WM_MOUSEMOVE on a listbox, etc).
Thanks!
KR
|
|
|
|
|
KellyR wrote: I have a dialog that I load up that has a lot of other controls on it, and I need to access the messages for those controls
You can subclass the controls you want to intercept messages for:
Subclassing Controls[^]
This can be done in your dialogproc in response to WM_INITDIALOG.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
|
I found the default wrapping behavior by Graphics::DrawString to be incompatible with the wrapping behavior of some legacy apps and it was impossible to achieve some other required functionality without resorting to handling the wrapping manually.
With that said, I am having a rough time with the fact that GDI+ insists on measuring the '\n' character during output. This makes paragraph output, centered aligned, come out a bit off. I can remove the StringFormatFlagsMeasureTrailingSpaces flag but then any trailing spaces do not get measured. This is a word processor so text selection measurements must match the text output for text selection feedback.
Is there a way, without drawing each line individually, to somehow get GDI+ to ignore the '\n' character in measurements and when calculating center alignment but to still wrap on them? Is there another "newline" character that gets measured with a zero width?
|
|
|
|
|
Hello,
This article here: http://www.codeproject.com/KB/miscctrl/AppControl.aspx shows how you can host external window on your application if you have handle to the window. It uses this piece of code to host the window:
SetParent(appWin, this.Handle);
SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);
What I want to do is to be able to release the hosted window after some time. In order to do that I use GetParent() function to get handle of previous parent and then restore it again but I'm stuck with SetWindowLong part. Any ideas?
Thanks
|
|
|
|
|
SetWindowLong() returns the previous value of the one you set.
You could save that returned value and restore it when you
release the window...
LONG OldWindowStyle;
...
OldWindowStyle = SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);
...
SetWindowLong(appWin, GWL_STYLE, OldWindowStyle);
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi all,
am writting a C++ application and I need to access my resource file really i use function
LoadLibrary() and it returns ahandle to the dll successfully but when I use the function
FindResource() and give it the resource id that I want to use it returns NULL, although when i use GetLastError() it tells that function endded successfully !!!.
Here I wanna tell something this code is successfull code when I use it on concole application, but when I write it on the citrix endpoint analysis SDK and generate the exe file when I run the exe simply it gives that FindResource() returns NULL,
Please can any one help???
1 - can i pass the exe name to LoadLibrary() ??
2 - FindResource problem ??
|
|
|
|
|
Somaiia wrote: 1 - can i pass the exe name to LoadLibrary() ??
Yes, you can pass the (path)name of an EXE to LoadLibrary().
Somaiia wrote: 2 - FindResource problem ??
Can you show the code calling FindResource()?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I'd like to try vs2008.
BTW: I think the only bad thing is that it needs too much computer resources.
|
|
|
|
|
I don't think VS2008 supports source control any more, or any less, than VS2005. VSS and VSTS are the two source control solutions from MS you can plug in.
Yes, every new MS product assumes a current generation PC.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
It is the rect of the selection rect of the icon, not the rect of the icon itself. How to get the rect of the icon? The size of the icon is defined arbitrarily in CImageList.
|
|
|
|
|
Hi
I've used files long time ago in C++.
I guess there is a certain new convention for using files in Visual C++ 2005,
I am using the class fstream with binary files and
I don't get the right file position when using tellp and tellg.
Is there any new class which I should use or any other method ?
I want still to work unmanaged not using the functions of system.dll which are used in .NET.
Thanks,
Clint
|
|
|
|
|
clint1982 wrote: I am using the class fstream with binary files and
I don't get the right file position when using tellp and tellg.
What do you get? Expect?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
well, I write bytes into a file, I seek to the end of the file and call (tellp or tellg) after flushing the stream, and get 2 as an answer. But if I close the file, I see that the size of the file is 188.
So I expect that the answer would be 188.
Clint
|
|
|
|
|
Usually these functions work well. Can you post the relevant code?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|