Beginner Question on Classes
Why do most namespace classes provide explicitly named constructor methods while others do not. For example, the File class and the Console class do not provide constructors. At least I don't see them while using my Object Browser in Visual Studio.
It appears from tradition that you call their methods using the scope resolution operator only. Why is it this way only for certain classes while most classes (ie. Filestream) require the use of object pointers to call their methods?
Also, can you call the methods of Filestream using the scope resolution operator instead of creating an object pointer?
I suppose these are very fundamental questions; but, these issues are not addressed in any of the C++ books that I have read.
[766 byte] By [
GOINMOBILE] at [2007-11-19 6:59:27]

# 1 Re: Beginner Question on Classes
Every class has a constructor, whether you put more code into the constructor depends. Their purpose is to set up the initial, or default values for the class. When you create an instance of your object in the program, the default constructor is automatically called. If your class requires nothing to be set up, the constructor is still called, but nothing extra is done.
Other classes have copy constructors, or constructors that will set up your class with your own specified defaults.
I'm not quite sure what you're asking about why some objects require the use of object pointers. I am guessing it's because they are managed classes. Meaning they probably have a __gc before the class. .NET uses this for its automatic garbage collection so you don't ever have to call delete on the object. The scope resolution , :: , is used to specify namespaces and what class a function belongs to. I don't believe you can use it to call methods.
# 2 Re: Beginner Question on Classes
Whenever you read a book explaining C++ NET you are introduced to the Console::WriteLine statement. This is an example of using a scope resolution operator to call a method. Console is the class and it is calling its method WriteLine using a scope resolution operator (as opposed to using an object pointer or tracking handle to call WriteLine).
Yet, whenever you use a StreamReader class method, you are taught to call the method using an object pointer. For example:
StreamReader^ sr = gcnew StreamReader (fs);
String^ line = sr-> ReadLine();
If you look up the Console class within mscorlib.dll in the System namespace (using the Microsoft VC Express 2005 IDE object browser) then you will see that there is no constructor method listed for this class.
My question is why are constructor methods listed for certain classes, but not for other classes? And, why do they teach you to call Console class methods using the scope resolution operator only? I'm sure that as I use C++ NET more I may figure it out myself sometime.
# 3 Re: Beginner Question on Classes
what do you mean by constructor methods not being listed? Do you mean they simply don't write in code for it, thus it is missing from the class?
If that's the case then the class doesn't need a custom constructor and the compiler just calls a default constructor to set up the class. Or, subclasses may initialize all the members within their constructors.
As far as the scope resolution is concerned, yes you can use that to access/call member functions within a class. However, the difference between the scope resolution and -> is that -> deals with pointer objects. Classes declared as pointers require that you use -> to access them. Because they are only a pointer, you need to refer directly to the member of an object pointed by a pointer.
The reason you are using Console::WriteLine() is because, first of all, there is no object pointer pointing to the Console object, you haven't declared any object, so in order to access WriteLine(), you need to specify for the compiler where to find the function. The actual extention is probably System::Console::WriteLine() but because you used the "using namespace System." In other words, the scope operator specifies the class in which that member is located.