Showing posts from December, 2010

Difference between " and < for #include preprocessor directive

#include preprocessor directive is used to include a header file in C++.
This tells the preprocessor to open the named header file and insert its contents where the #include statement appears. 
For #include may name a file in two ways: using angle brackets (< >) or in double quotes(").

File names in angle brackets, such as:      #include
cause the preprocessor to search for the file in “include search path” that you specify in your environment or on the compiler command line.

File names in double quotes, such as:

    #include "header"
tell the preprocessor to search for the file in “current" directory. If the file is not found, then the include directive is reprocessed as if it had angle brackets instead of quotes.

Some services stop automatically if they have no work to do.

Sometimes it happens that you go and try to start any service and it gives you a nice message that "Some services stop automatically if they have no work to do.....".

There could be multiple reasons that this could happen..
1. The service crashed during startup.
2. Dependent service crashed/not started.
3. The service is not implemented properly.

In such scenario you can try following things:
1. Look at the event logs, if you find some error related to your service then try to resolve that.
2. Restart your machine in Safe mode, set the service properties as automatic and try to start the service. Sometimes this helps.
3. Try to completely uninstall and reinstall the service.

Private inheritance

The following code shows how we derive a class from other class.

class Base

class Derived: public Base

The Derived class is derived from Base with public level of inheritance. If you omit the public word while declaration such as 
class Derived : Base
the level of inheritance will be private.

The private inheritance is only supported in unmanaged C++. Its not supported in managed C++. if you declare the compiler will give error.


Encapsulation is the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data. 

Encapsulation clearly represents the ability to bundle related data and functionality within a single, autonomous entity called a class. 

Encapsulation helps by breaking a program down into small, self-contained entities. 

String cannot use this type here without a top-level '^'

If you are using String in unmanaged code with managed extension you should include it as

String^ str = "This is string";

Why static methods cant access non static members?

A static variable will be available for the entire lifetime of the program, even before the object of call is being created.See below code
class CreditCard { public:     static int noOfCards;     … }; int CreditCard::noOfCards = 99; int _tmain(int argc, _TCHAR* argv[]) {    Console::WriteLine("{0}", CreditCard::noOfCards);     // This line will print 99.    ... }
Same holds true for the static functions declared in a class, with the limitation that A static member function can only access static class members.  We will see why cant static member function cant access not static member data.
In the following code:
class CreditCard { public:    static int GetnoOfCards()    {        return noOfCards;    }    ... private:    static int noOfCards; }; int CreditCard::noOfCards = 99; int _tmain(int argc, _TCHAR* argv[]) {    Console::WriteLine("{0}", CreditCard::GetnoOfCards());

Fatal error C1190: managed targeted code requires a '/clr' option

In Visual Studio for C++ code you have two options, managed code or unmanaged code.

Managed code compiles to Intermediate Language that will run on the Common Language Runtime (CLR). Unmanaged code compiles to machine code that runs directly on the computer.
When you try to include any managed code in the unmanaged code and compile you will get this error message. 
To eliminate this error you should use the common runtime support for your code. 
To enable the /clr option for your project in Visual Studio go to:

Project --> Properties-->Configuration Properties-->General-->Common Language Runtime Support.

and select the "Common Language Runtime Support (/clr)". Now save and compile. 


Use of typedef

typedef is a user-defined synonym for an existing type. To create a synonym for a type, you use the keyword typedef followed by the name of the type and the new name you are defining.

E.g. typedef unsigned int uInt; 

Here uInt will be synonym for type unsigned int. 

The following line of code actually produces an int* which is and an int (not an int*) which is y. That is, the ‘*’ binds to the right, not the left.

int * x, y; 

However, if you use a typedef:

typedef int* intPtr; intPtr x, y; Then both x and y are of type int*.

C++ Constants

Constants are named data-storage locations.  C++ has two types of constants: literal and symbolic. 
Literal constant is simply a value typed into the code. In following example 40 and Cat are the known as a literal constants. E.g. nCounter = 40; strAnimalName = "Cat";
Symbolic constant is represented by a name.Its defined exactly the same way as a variable but the qualifier must start with the keyword const and the variable must be initialized. E.g. const int nMaxIterations = 49; const unsigned long nMaxCharInFile = 123234;

Advantages of using Symbolic Constants:
The symbolic names make the program more readable. It’s easier to change a single symbolic constant value than to find and replace all occurrences of a literal in a code.