|
I have to add a comment like //<amrit.agrawal> Added 02/02/2011 in somewhere in the code.
When we made some some changes in any file and want to add a tag so that it can be track afterwords to know which developer is made changes on that particular file or line, in a shortcut key like Alt + a.
|
|
|
|
|
Read the file using CStdioFile(or some other class or file object) and insert the comment at the location "somewhere in the code". Also add the developer name..
BTW your requirement seems to be just like reading and writing to a file or is there something else that you intend to do? Clarify please.
I am a HUMAN. I have that keyword in my name........
_AnsHUMAN_
|
|
|
|
|
Suppose I am working to solve a bug in some function. I want to add a chunk of code in that function. Above that chunk I have to write like( While Press a shortcut key like Alt + s ), //<developer_name> Added <current date=""> so that I can identify that this changes has done by me. Otherwise I have to do that manually every time.
|
|
|
|
|
I am really under the impression that you need a version control system (VCS), like I suggested below. Comments in code just won't do it, no matter how clever your tricks to get them inserted. The huge advantage of a VCS is that you will not only see which line had been changed by whom (and the related comment), but also what else had been changed at the same time, and even what, exactly, the changes were. You can also undo the changes, or restore any previous version that you want, even deleted files can be restored this way.
In case of Subversion I could give you some pointers. E. g. on Windows, you can just install the GUI client TortoiseSVN instead. it comes with terrific documentation that will get you started quite easily. (not saying the Subversion manual is bad, it's just targeted a bit more at advanced users). There are also plugins for integration with VisualStudio, and Eclipse supports it out of the box.
If you have Visual Studio 2010, TFS would be an option (the version of TFS delivered with earlier versions of VS weren't all that good). AFAIK it's free if you have the subscription.
There are other systems such as Mercurial and GIT, but I know too little of these to give any further advice.
|
|
|
|
|
Adding comments won't do it, because it may be gone if someone later deletes a function that has become obsolete, all the comments therein are gone as well. Of course, if that function became obsolete, you may no longer be interested in the changes going into it.
Also, the (in)famous habit of copy and paste may obfuscate or invalidate some of these comments, when replicating code to different contexts.
The best way to track changes is using a version control system, such as subversion. These systems not only allow you to add comments on your changes outside your source code, they also let you see all files that have been changed at the same time for a particular fix. It's also much easier to use: change 15 files to fix a problem, then check in and add a comment about what you've done just once. Much easier than adding comments on 50 lines in 15 different files!
I've suggested subversion because that is what I am familiar with, and I know it works on many platforms and there are lots of addons and plugins to integrate it with your development environment. But there are many other good version control systems as well.
|
|
|
|
|
How to set Background color
Header File "MyButton.h"
#pragma once
#include "afxwin.h"
const COLORREF bkColor= RGB(0,0,0);
class CMyButton :
public CButton
{
public:
CMyButton(void);
public:
~CMyButton(void);
protected:
afx_msg void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
DECLARE_MESSAGE_MAP()
};
Cpp File "MyButton.cpp"
#include "StdAfx.h"
#include "MyButton.h"
CMyButton::CMyButton(void)
{
}
CMyButton::~CMyButton(void)
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
ON_WM_DRAWITEM()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
BOOL CMyButton :: OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetClientRect(rect);
pDC->FillSolidRect(rect,bkColor);
return true;
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {
}
Now u can use above class in your Dialog As:-
BOOL CChessGameDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
CMyButton* m_pbtn13 = new CMyButton ();
m_pbtn13 ->Create(NULL, WS_CHILD | WS_VISIBLE |BS_OWNERDRAW ,CRect(150, 250, 250, 300), this, NZWR_BUTTON_13);
return TRUE;
}
HOW to set an Icon to CButton
BOOL CChessGameDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
CButton m_pbtn12 = new CButton ();
m_pbtn12 ->Create(NULL, WS_CHILD | WS_VISIBLE |BS_ICON ,CRect(150, 50, 250, 200), this, NZWR_BUTTON_12);
if (m_pbtn12->GetIcon() == NULL)
m_pbtn12->SetIcon(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE( IDI_ICON1)));
return TRUE;
}
How to use CBitmapButton Class
BOOL CChessGameDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
m_pbtn11= new CBitmapButton ();
m_pbtn11->Create(NULL, WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,CRect(50, 50, 200, 200), this, NZWR_BUTTON_11);
m_pbtn11->LoadBitmaps(IDB_BITMAP2, IDB_BITMAP1, IDB_BITMAP2, IDB_BITMAP1);
m_pbtn11->SizeToContent();
return TRUE;
}
/pre;
|
|
|
|
|
Were you intended to post 'Tip/Tricks' to CP ?
|
|
|
|
|
m_pbtn11= new CBitmapButton ();???
This should be changed to the class derived from the CBitmapButton(in your case CMyButton). Also, have you set the owner drawn property of the button to true
[EDITED]: Is this is a question or you intend to write an article?
I am a HUMAN. I have that keyword in my name........
_AnsHUMAN_
modified on Wednesday, February 2, 2011 2:06 AM
|
|
|
|
|
Hi all
how to do function like cout<<"" but without using cout and with different name.
I want to say how to create a print function without using cout and name it as you like.
I hear that i can do it by overloading or overriding or anything else .. i don't know.
Thank's
-*-*-*-*-*-*-*-*-*
To Be Or Not To Be
(KARFER)
-*-*-*-*-*-*-*-*-*
|
|
|
|
|
See here[^] for information on deriving your own stream classes in C++.
I must get a clever new signature for 2011.
|
|
|
|
|
cout is not a function.
cout is an object of a class called basic_ostream .
This class has overloaded the operator << to accept many different data types as parameters like string , char* , short , long etc.
|
|
|
|
|
As correctly pointed out, the problem at hand is deriving your own stream class. Just entering that into a search on CP (using the tag "C++") gave me a list of articles, the third of which looked promising: audio_ostream - A Text-to-Speech ostream. If nothing else it's a working example with code.
|
|
|
|
|
Hello, I've got this little program in Visual Studio C++ 2008; console application.
When I run it into debug mode, it works perfectly. If I compile for release, the instructions:
<br />
<br />
while(nVector[nPos + 1] == nVector[nPos] + 1)<br />
<br />
or
<br />
<br />
for(;nVector[nPos + 1] == nVector[nPos] + 1;)<br />
<br />
are not working...
The only one that works for both debug and release is:
<br />
<br />
nPos--;<br />
<br />
do<br />
{<br />
nPos++;<br />
}<br />
while(nVector[nPos + 1] == nVector[nPos] + 1);<br />
<br />
Here is the whole code:
<br />
<br />
<br />
#include "stdafx.h"<br />
<br />
#define SIZE_VECTOR 10<br />
#define _DEBUG_WHILE_USE<br />
<br />
<br />
unsigned int random(const unsigned int &uMin, const unsigned int &uMax);<br />
void printVector(unsigned int nVector[]);<br />
void swap(unsigned int &uFirst, unsigned int &uSecond);<br />
void randomize(unsigned int nVector[], const int &nPos);<br />
<br />
<br />
int _tmain(int argc, _TCHAR* argv[])<br />
{<br />
unsigned int nVector[SIZE_VECTOR];<br />
unsigned int nStep = 0;<br />
int nPos = 0;<br />
<br />
for(int i = 0; i < SIZE_VECTOR; i++)<br />
{<br />
nVector[i] = i;<br />
}<br />
<br />
printf("Start...\r\n\r\n");<br />
<br />
while(nPos < SIZE_VECTOR - 1)<br />
{<br />
randomize(nVector, nPos + 1);<br />
printVector(nVector);<br />
nStep++;<br />
<br />
#ifdef _DEBUG<br />
#ifdef _DEBUG_WHILE_USE<br />
while(nVector[nPos + 1] == nVector[nPos] + 1)<br />
{<br />
nPos++;<br />
}<br />
#else // _DEBUG_WHILE_USE<br />
for(;nVector[nPos + 1] == nVector[nPos] + 1;)<br />
nPos++;<br />
#endif // _DEBUG_WHILE_USE<br />
#else // _DEBUG<br />
nPos--;<br />
<br />
do<br />
{<br />
nPos++;<br />
}<br />
while(nVector[nPos + 1] == nVector[nPos] + 1);<br />
#endif // _DEBUG<br />
}<br />
<br />
printf("\r\nSteps: %d.\r\n\r\n", nStep);<br />
return 0;<br />
}<br />
<br />
unsigned int random(const unsigned int &uMin, const unsigned int &uMax)
{<br />
unsigned int uRand = 0;<br />
<br />
if((uMin <= uMax) && (rand_s(&uRand) == 0))<br />
{<br />
double dDiv = (double)uRand / (double)UINT_MAX;<br />
double dMul = dDiv * (double)(uMax - uMin + 1);<br />
uRand = (unsigned int)(dMul + uMin);<br />
}<br />
<br />
return uRand;<br />
}<br />
<br />
void printVector(unsigned int nVector[])<br />
{<br />
for(int i = 0; i < SIZE_VECTOR; i++)<br />
{<br />
printf("%2d%s", nVector[i], (i == SIZE_VECTOR - 1) ? ".\r\n" : ", ");<br />
}<br />
}<br />
<br />
void swap(unsigned int &uFirst, unsigned int &uSecond)<br />
{<br />
unsigned int uTemp = uFirst;<br />
uFirst = uSecond;<br />
uSecond = uTemp;<br />
}<br />
<br />
void randomize(unsigned int nVector[], const int &nPos)<br />
{<br />
for(int i = nPos; i < SIZE_VECTOR - 1; i++)<br />
{<br />
swap(nVector[nPos], nVector[random(i, SIZE_VECTOR - 1)]);<br />
}<br />
}<br />
<br />
with
<br />
<br />
#define _CRT_RAND_S<br />
<br />
#include <stdlib.h><br />
<br />
in stdafx.h
Does anyone have any idea why this is happening?
Thank you!
|
|
|
|
|
what do you mean "not working" ? does it crash? do you get an error message?
for(;nVector[nPos + 1] == nVector[nPos] + 1;)
my first guess is that nPos + 1 >= SIZE_VECTOR, so you're going off the end of the array and you get an access violation.
|
|
|
|
|
I mean, the instruction is as if it's not there...
No, there is no violation...
For example...
I have the vector: 1, 2, 4, 3..
In debug, he analyzes 1 and 2 and changes nPos so that in the next cycle it analyzes 2 and 4...
In release, it's as if the instruction is not there...
If I replace "while" with "if", it works for both debug and release, in:
<br />
while(nVector[nPos + 1] == nVector[nPos] + 1)<br />
{<br />
nPos++;<br />
}<br />
|
|
|
|
|
Khan Shere wrote: In release, it's as if the instruction is not there...
That is because the instructions are not there. Take a look at your source; all the code between #ifdef _DEBUG and its corresponding #endif will be excluded from the release build.
NB please also use <pre></pre> tags (not code) to format your code blocks.
I must get a clever new signature for 2011.
|
|
|
|
|
Ooops, I'm sorry, I forgot to mention: those #ifdefs I've placed there in order for the app to run in both debug and release modes.. but if you run the _DEBUG part of code in release, it won't work..
|
|
|
|
|
I have tried running your code with the various different configurations but cannot see how to tell that a particular part of the release version does not work. In each case the output is similar, although when using the for statement the program crashes.
I must get a clever new signature for 2011.
|
|
|
|
|
This sounds like an optamisation bug.
there is a similar bug with the following:
int x = 0;
int y = ++x + ++x;
You would expect this to be y = 1 + 2 (=3) which it is when optimisations are disabled, but the value of y when optimisations are turned on is 4
Try disabling optimisations in the release build. They don't help all that much unless you are after extreme speed or a tiny image.
If this helps, you can try re-enabling them 1 at a time if you want optimisation.
|
|
|
|
|
It's not a bug, it's standard behaviour. If you write statements with more than one sub-expression in it that writes to a variable then the compiler can generate any old rubbish it wants.
modified on Tuesday, February 1, 2011 2:32 PM
|
|
|
|
|
I agree, it could very well be an optimisation 'feature'.
It might be worth it to give this a shot:
while(nVector[nPos + 1] == (nVector[nPos] + 1))
modified 13-Sep-18 21:01pm.
|
|
|
|
|
It still doesn't work.
I also suspect it's an optimization 'feature'.. just read the other day about the bogosort on wikipedia (http://en.wikipedia.org/wiki/Bogosort) and wanted to play a little with it and discovered this little gem between debug and release versions.. and this is by no means an application, just a little fun I was having, but such an 'optimization' in a big and important program could really have a serious problematic impact. 
|
|
|
|
|
hi guys
probably already discussed but I did not find the explenation:
in the above code:
My question is why I have to use reference to the pointer
when returning from mGetPt?
If this pointer whould have been used inside the function,
and for example used in constructor to allocate memory
if I have an Get function like CInner* mGetPt(void)
I am able to use the pointer as I want.
but in the above example it is required to return a reference.
(this is the return value, is not a function param; OK I know in case of function params I have to use reference to pinter)
Please someone clarify me why?
class CInner
{
public:
int mx;
CInner()
{
mx=1;
}
};
class CTest
{
private:
CInner *pt;
public:
CTest()
{
pt= NULL;
}
CInner*& mGetPt(void)
{
return pt;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CTest *obj= new CTest();
obj->mGetPt() = new CInner();
cout<<obj->mGetPt()->mx;
cin.get();
return 0;
}
|
|
|
|
|
You need to return a reference to the pointer if you want an outsider to be able to change the value of that pointer. It looks like an ugly attempt to make dependency injection. A setter/getter would IMO be clearer. Who's responsible for deallocating the instance? The contract isn't clear at all. If trying something like that, a massive comment block describing responsibilities would be a minimum.
|
|
|
|
|
Firstly, I agree with Niklas, this is terrible style and structure. A class should be responsible for allocating and deallocating all its memory.
To answer your question, returning a pointer allows you to modify the data that is pointed to (in your case this is NULL, so it would result in an access violation).
Returning a reference allows you to modify the value of the original variable returned (think of reference as "another name for the variable X")
So, in order to modify the value of the variable pt rather than the data it points to, you can either return a reference to the variable, or a pointer to the variable itself.
You are returning the reference, to return a pointer you would use it like:
CInner** mGetPt(void) {
return &pt;
}
*obj->mGetPt() = new CInner();
Having said that, this is still bad style, and you would be much better off with something like:
class CInner {
public:
CInner() : mx(1) { }
int GetMX() { return mx; }
void SetMX(int nNewMx) { mx = nNewMx; }
private:
int mx;
};
For CTest you have 2 choices, you can either allocate pt = new CInner in the constructor or when it is needed.
class CTest {
public:
CTest() : pt(new CInner) { }
~CTest() {
if (pt != NULL) {
delete pt;
}
}
CInner *mGetPt() { return pt; }
private:
CInner *pt;
};
class CTest {
public:
CTest() : pt(NULL) { }
~CTest() {
if (pt != NULL) {
delete pt;
}
}
CInner *mGetPt() {
if (pt == NULL) {
pt = new CInner;
}
return pt;
}
private:
CInner *pt;
};
You can then use it like:
int _tmain(int argc, TCHAR *argv[]) {
CTest *obj = new CTest();
cout << obj->mGetPt()->GetMX();
cin.get();
return 0;
}
|
|
|
|
|