Tuesday, July 28, 2009

In c++ ,what is meaning of " using namespace std" directive?

why a constructor can not be static and virtual?


Why objects with a constructor or destructor can not be used as a member of union?


Please answer if you know ans of any of these questions.

In c++ ,what is meaning of " using namespace std" directive?
Namespace is not a directive but a keyword which defines a new naming scope. The great feature of C++ has been to let the programmer use the same name for different variables and functions as well as class members. Using the same name for some distinct global functions (functions which are not a member of any class) is not difficult as long as you make one function differ from another by at least one argument. However, it is not the case for variables. As an example, having both an int count and a double count variable in the same global scope is ambiguous. When all the names are chosen by a single programmer, it is easy to manage the chosen names for the variables. But, what if the names are chosen by different programmers? Besides, it wouldn’t be a nice look of the program to add some excessive characters to the name of variables while they do not add any meaning to the purpose of a variable. The solution is to change the global scope of a variable by some means. The membership to a class works fine to define a new scope for variables, but the global variables do not belong to any class. So another concept has been invented, the namespace. Namespace defines a new scope for the variables while it keeps the concept and the purpose of the variable clear. Now, you could have a variable named cout in another namespace other than std without any confliction with the cout variable in the std namespace.





Obviously, a constructor cannot be static. A static member means that it is meant to describe something about the class (a property or a method of the class) itself rather than the objects of this class. A constructor is meant to create an object of the class; therefore, it refers to the object of the class and cannot be static.





A constructor may not be virtual like the way a destructor could be because the compiler must know exactly about the object it is creating, and there is no object yet to deduce which constructor to invoke. But you could easily get the same effect by a clever trick. Please refer to Bjarne Strostroup’s “The C++ Programming Language” (the book authored by of the inventor of C++) for a detailed description (Find section about virtual destructor from the index).





I am not easy with unions because they are not recommended at all. But I guess, it is because allocating space and values for union fields is not a run-time process but a compile-time one where it is not possible to invoke a constructor. The same idea is true for record (struct) and array initializations.
Reply:using namespace std means that the compiler will look in the "std" namespace if it can't find a definition for something.





The lets you do things like:


cout %26lt;%26lt; "asdf" %26lt;%26lt; endl;





instead of having to type:


std::cout %26lt;%26lt; "asdf" %26lt;%26lt; std::endl;





Constructors are used to set up an instance of the class and initialized data members. A normal static member function only has access to static members of the class -- a static constructor presumably would only have the same. But static members have to be explicitly assigned values anyway, so it doesn't seem like there would be a point.





A virtual constructor makes no sense -- when constructing objects, we always want the construct the exact object we're asking for. We dont' want to resolve to the most derived constructor.





Because the variables of a union share memory, a constructor setting one variable would potentially overwrite the value of another variable outside of the class, but in the union.
Reply:using namespace std; means that you are using the standard namespace. It tells the compiler to treat the names in the standard library as though we had defined them in the current program.


No comments:

Post a Comment