Saturday, June 30, 2007

What is a Copy Constructor?

The copy constructor lets you create a new object from an existing one by initialization. A copy constructor of a class A is a nontemplate constructor in which the first parameter is of type A&, const A&, volatile A&, or const volatile A&, and the rest of its parameters (if there are any) have default values.

If you do not declare a copy constructor for a class A, the compiler will implicitly declare one for you, which will be an inline public member.

The following example demonstrates implicitly defined and user-defined copy constructors:

#include
using namespace std;

struct A {
int i;
A() : i(10) { }
};

struct B {
int j;
B() : j(20) {
cout << "Constructor B(), j = " << j << endl;
}

B(B& arg) : j(arg.j) {
cout << "Copy constructor B(B&), j = " << j << endl;
}

B(const B&, int val = 30) : j(val) {
cout << "Copy constructor B(const B&, int), j = " << j << endl;
}
};

struct C {
C() { }
C(C&) { }
};

int main() {
A a;
A a1(a);
B b;
const B b_const;
B b1(b);
B b2(b_const);
const C c_const;
// C c1(c_const);
}

The following is the output of the above example:

Constructor B(), j = 20
Constructor B(), j = 20
Copy constructor B(B&), j = 20
Copy constructor B(const B&, int), j = 30

The statement A a1(a) creates a new object from a with an implicitly defined copy constructor. The statement B b1(b) creates a new object from b with the user-defined copy constructor B::B(B&). The statement B b2(b_const) creates a new object with the copy constructor B::B(const B&, int). The compiler would not allow the statement C c1(c_const) because a copy constructor that takes as its first parameter an object of type const C& has not been defined.

The implicitly declared copy constructor of a class A will have the form A::A(const A&) if the following are true:

The direct and virtual bases of A have copy constructors whose first parameters have been qualified with const or const volatile
The nonstatic class type or array of class type data members of A have copy constructors whose first parameters have been qualified with const or const volatile
If the above are not true for a class A, the compiler will implicitly declare a copy constructor with the form A::A(A&).

The compiler cannot allow a program in which the compiler must implicitly define a copy constructor for a class A and one or more of the following are true:

Class A has a nonstatic data member of a type which has an inaccessible or ambiguous copy constructor.
Class A is derived from a class which has an inaccessible or ambiguous copy constructor.
The compiler will implicitly define an implicitly declared constructor of a class A if you initialize an object of type A or an object derived from class A.

An implicitly defined copy constructor will copy the bases and members of an object in the same order that a constructor would initialize the bases and members of the object.

What is assembly in .NET?

You must have heard the word assembly many times in .NET documentation. In this article I will share some thing about .NET assemblies.


What is an assembly?

  • An Assembly is a logical unit of code

  • Assembly physically exist as DLLs or EXEs

  • One assembly can contain one or more files

  • The constituent files can include any file types like image files, text files etc. along with DLLs or EXEs

  • When you compile your source code by default the exe/dll generated is actually an assembly

  • Unless your code is bundled as assembly it can not be used in any other application

  • When you talk about version of a component you are actually talking about version of the assembly to which the component belongs.

  • Every assembly file contains information about itself. This information is called as Assembly Manifest.




What is assembly manifest?

  • Assembly manifest is a data structure which stores information about an assembly

  • This information is stored within the assembly file(DLL/EXE) itself

  • The information includes version information, list of constituent files etc.




What is private and shared assembly?


The assembly which is used only by a single application is called as private assembly. Suppose you created a DLL which encapsulates your business logic. This DLL will be used by your client application only and not by any other application. In order to run the application properly your DLL must reside in the same folder in which the client application is installed. Thus the assembly is private to your application.
Suppose that you are creating a general purpose DLL which provides functionality which will be used by variety of applications. Now, instead of each client application having its own copy of DLL you can place the DLL in 'global assembly cache'. Such assemblies are called as shared assemblies.



What is Global Assembly Cache?


Global assembly cache is nothing but a special disk folder where all the shared assemblies will be kept. It is located under :\WinNT\Assembly folder.