Programming Tips 21
 

Tips on C, C++, VC++, Java, C#, .Net, VB, ASP 

Programmming

 

Tips 1

 

Tips 2

 

Tips 3

 

Tips 4


Tips 5


Tips 6

 

Tips 7

 

Tips 8

 

Tips 9  

 

Tips 10

 

Tips 11

 

Tips 12

 

Tips 13

 

Tips 14

 

Tips 15

 

Tips 16

 

Tips 17

 

Tips 18

 

Tips 19 

 

Tips 20

  

 Tips 21

 

 C Tip


What's the difference between these two declarations?

struct str1 { ... } ;
typedef struct { ... } str2 ;

Ans : The first form declares a structure tag whereas the second declares a typedef. The main difference is that the second declaration is of a slightly more abstract type -- its users don't necessarily know that it is a structure, and the keyword struct is not used when declaring instances of it.

Top


C++ Tip


Why is it necessary to use a reference in the argument to the copy constructor?

Ans : If we pass the copy constructor the argument by value, its copy would get constructed using the copy constructor. This means the copy constructor would call itself to make this copy. This process would go on and on until the compiler runs out of memory. This can be explained with the help of following example:

class sample
{

int i ;

public :

sample ( sample p ) 
{

i = p.i ;

}

} ;

void main( )
{

sample s ;
sample s1 ( s ) ;

}

While executing the statement sample s1 ( s ), the copy constructor would get called. As the copy construct here accepts a value, the value of s would be passed which would get collected in p. We can think of this statement as sample p = s. Here p is getting created and initialized. Means again the copy constructor would get called. This would result into recursive calls.Hence we must use a reference as an argument in a copy constructor. 


VC++ Tip


Accessing Objects from Multiple Threads...

MFC objects are thread-safe at the class level but not at the object level, due to size or the performance reasons. This means that we can have two separate threads manipulating two different CString objects, but not two threads manipulating the same CString object. However, if we want to have multiple threads manipulating the same object, protect such access with appropriate Win32 synchronization mechanisms, such as critical sections. 

Top


Managed C++ Tip


File and FileInfo classes...

.NET offers two classes for file operations - the File class and the FileInfo class. Both these classes are defined in the System::IO namespace hence we must declare the System::IO namespace in the program that uses File or FileInfo class. 

The File class is derived from the Object class. It contains only static methods. Since static methods can be called using the class name itself we are not allowed to create an object of the File class. The FileInfo class is derived from FileSystemInfo class. The FileSystemInfo class is derived from the MarshalByRefObject class which in turn is derived from the Object class.






.NET and C# Tip


Web Controls...

Web Controls are server controls. By server controls we mean those controls, which execute on the server and not on the client. In ASP, controls were mere HTML tags. Whenever the browser used to find these HTML tags, the controls used to get displayed. However, in ASP.NET the controls do not get added as HTML tags. Instead, they are objects within the page. Hence we can change properties and add events for these controls just like we would do for controls used in WinForms. This is quite a big advantage over ASP. All the Web Controls are derived from the WebControl class present in the System.Web.UI.Control namespace. The code that gets added for Web Controls in the '.aspx' file is based on XML. 



 XML Tip


What are DTDs?

Ans: One of the way for checking the well formness of the document, is to use DTDs. DTD means Document Type Definition. The DTDs within the XML Document are known as the internal DTDs and those outside the XML Document that use the separate file, are known is external DTDs.