Kiran's Blog

Ramblings from a dehydrated mind…

  • Archives

    • 14,897

Pointers to data members..

Posted by kiraninbng on July 7, 2006

Pointers to data members are pointers which help us to manipulate the data members of an object.The syntax is very similar to that of pointer to function,with the addition of the class name followed by the scope resolution operator(::).

Ex :

class CTest
{
 int nVal;
}

// pmCTest is a pointer to an int member of CTest
int CTest::*pmCTest;

You can initialize the pointer to data member by giving the address of member of the Class

Ex:  pmCTest = &CTest::nVal;

The data pointed can be manipulated either by using the object or the object pointer,

Ex :

CTest oCTest;
CTest* pCTest = new CTest;

int nTemp = oCTest.*pmcTest; // Using the object
pcTest->*pmcTest = nTemp; // Using the object pointer

In the next post i will talk about pointers to member functions.

Advertisements

Posted in General | Leave a Comment »

Why virtual destructors…

Posted by kiraninbng on July 6, 2006

Heres a great post on when and why the destructors should be made virtual.

Posted in General | 1 Comment »

Custom Draw and Owner draw

Posted by kiraninbng on July 6, 2006

Custom draw is a service provided by some of the common controls.Custom draw allows you to make minor changes to the way the items are displayed in the screen.
Currently, the following controls support custom draw functionality:

Header controls
List-view controls
Rebar controls
Toolbar controls
ToolTip controls
Trackbar controls
Tree-view controls

All common controls that support custom draw send NM_CUSTOMDRAW notification messages at specific points during drawing operations. These notifications
describe drawing operations that apply to the entire control as well as drawing operations specific to items within the control. Like many notification messages,
NM_CUSTOMDRAW notifications are sent as WM_NOTIFY messages.
Custom draw feature is somewhere in between the controls default drawing behaviour and the programs owner draw functionality

Owner draw feature allows the customizing the existing controls or lets drawing the control from the scratch.

Currently, the following controls support OwnerDraw functionality:

Buttons
ComboBoxes
ListBoxes
ListView controls
Menus
Static controls
Tab controls

Whenever the item must be drawn Windows sends the WM_DRAWITEM message to the window procedure of the item’s owner window. This message contains a pointer to
a DRAWITEMSTRUCT structure.The application can customize the controls by handling this message.

Posted in MFC | 5 Comments »

sizeof…

Posted by kiraninbng on July 5, 2006

Here is a simple way to find the size of an object without using the sizeof operator,

class CTest { … }

CTest oCTest;
CTest* pCTest;
unsigned char* pStart = NULL;
unsigned char* pEnd = NULL;

pCTest = &oCTest;
pStart = (unsigned char*)pCTest;
pEnd = (unsigned char*)(++pCTest);

//size gives the size of the object oCTest.
size_t size =  pEnd – pStart;

Posted in General | 1 Comment »

Sealed class…

Posted by kiraninbng on July 3, 2006

One way to create a sealed class is to make the constructor private and use a friend class.

Ex :

class BaseClass
{
private:
 BaseClass();
 virtual ~BaseClass();

     friend class DerivedClass;
};

class DerivedClass : private virtual BaseClass 
{
public:
 DerivedClass();
 virtual ~DerivedClass();
};

Now since the constructor of baseclass is not accesible to any class other than the friend class,the derived class cannot be inherited by any other class.

I know there are other ways of acheiving the same,if you know them do pitch in with your suggestiions.

Posted in General | 3 Comments »

First & Second chance exceptions…

Posted by kiraninbng on July 3, 2006

If the application is in the debug mode,the debugger will see all the exceptions before the application does.When an exception occurs the debugger will get the first chance to see the exception(First-Chance Exception).It will either handle it(go into the debug mode) based on the debugger settings or will pass it on to the application.

If the exception is not handled by the application the debugger will see the exception again,it will get a second chance(Second-Chance exception).The unhandled exception for the second time usually will terminate the application,if the application is running in the debug mode,it will break into the debugger.

Posted in General | Leave a Comment »

Good post on the hex values of variables

Posted by kiraninbng on July 3, 2006

Here is nice link on what actually the hex values of variables mean in the watch window…a very useful tip !!!

Posted in General | Leave a Comment »

PDBs….

Posted by kiraninbng on July 3, 2006

Ohayou gozaimasu !!! 

PDB stands for Program Database.This is a proprietary format for storing debugging information introduced by microsoft.When the linker is run a pdb file for the exe or the dll is created,by default with the same name as the exe/dll.The pdb file contrains information about the typedefs,global variables,function signatures.The absolute path of the pdb file is embedded in the dll/exe.
The PDB file can be divided into 4 parts,

1. File Header — Contains general pdb information

2. Allocation bit table — specifies which parts of file is in use

3. The Root Stream

4. Data Streams

The PDB stream consists of pages usually of 1kb size.The Pages can be located anywhere in the stream in any arbitrary order.The stream directory is responsible for telling an application which pages need to be accessed and in which order. This directory is itself stored in a stream called the root stream. The Allocation Bit Table keeps track of used and unused pages.This scheme is borrowed from simple operating systems such as MS-DOS with its FAT file system, in which a similar table specifies which disk sectors are allocated to files.The PDB header occupies the first file page, and is followed by one or more pages containing the allocation bits.

[Courtesy]
http://www.awprofessional.com/articles/article.asp?p=22429&seqNum=3&rl=1

Posted in General | Leave a Comment »

Deleting a NULL pointer..

Posted by kiraninbng on June 29, 2006

Whats wrong with the below code,

void CTestDelDlg::OnOK()
{
    CTry* pTry = NULL;
    delete pTry;
}

Nothing, except the function is pointless and without any logic.The point is that deleting null pointer will not throw an exception.If the pointer is NULL/0 delete assumes that allocation or new has failed.The C++ language guarantees that delete p will do nothing if p is equal to NULL.

The best way of deleting a pointer is to call delete first and set the pointer to NULL. delete. 

Ex :

void CTestDelDlg::OnOK()
{
    CTry* pTry = new CTry;
    delete pTry;
    pTry = NULL;
}
For more info on delete chk out http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/express_29.asp

Posted in MFC | 1 Comment »

Works in debug..but not in release

Posted by kiraninbng on June 29, 2006

So Why does my program run in debug but not in release ? Well it could be for any of the following reasons

1.Variables are initalized in debug build but not in release
    Soln : Initialize every variable before using them in the code.
 
2. Assert statements are ignored in the release build
    Soln : Use verify instead of assert,verify behaves like assert in debug build and in release it will just execute the condition in the verify.Better explicitly handle the error conditions,it is good to not to rely on verify.

3. Give complete and correct declarations in the message map entries(basically avoid copy/paste,use class wizard).It can hurt real bad in the release version.

Posted in MFC | Leave a Comment »