Click here to Skip to main content
15,910,886 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
I have part of a question that I cannot understand. I am supposed to modify code so that only class objects (objects that have constructors) can be a type. I don't understand what the second part of the question means, so here is my question:

Are there instances when ONLY class objects are allowed as types? If so, what are they?

What I have tried:

I have tried Google, looking through my notes, and looking through my text book
Posted
Updated 11-Dec-17 21:25pm
Comments
________________ 12-Dec-17 2:07am    
Each "programming thing (byte as smallest store unit)" has its own address in RAM (Random Assess Memory).
So, the problem how to store more than 1 byte was solved by defining "types of data" in high-level languages.
Simple types - integer for example, was converted to full stack class in .NET (and it has methods "ToString()"), but remains simple pointer to address of first byte in C++ or Java.

For programmer - "type" is only definition of how to save-read data from memory.
"Instance" - it is real place in memory, occupied by data.
________________ 12-Dec-17 2:12am    
So, in you sentence- "Class" - it is "Type" because it is not simple type - as integer, byte....
Correct meaning depends of what are we speaking about - Java, C, C++, C#....

I assume you mean in a Generic method or class:
C#
public T MyGenericMethod<T>(T myParameter)
   {
   ... 
   }
If so, then yes, it's pretty easy to restrict the class type to those with a constructor:
C#
public T MyGenericMethod<T>(T myParameter) where T: new()
   {
   ... 
   }
Unless the class you use the method with is a concrete class which has a public parameterless constructor, you will get an error.

If that isn't what you are trying to find out about, you will have to explain yourself in much greater detail.
 
Share this answer
 
There are programming languages where every instance variable is an object of a class (and the class represents its type). They are called pure OOP languages. An example is smalltalk. C# and Java, for instance, are NOT pure OOP languages.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900