Why "double declare" classes? (Lack of better terminology)

Started by
19 comments, last by brunopava 10 years, 4 months ago

I tried google, but can't find a good way to ask this question... lol

For instance:

myClass anObject = new myClass;

Why do you declare a myClass, then specify "new myClass"

In my mind, it's the same as:

int myNumber = new int.

"No shit, it's a new int! If I wanted a f***ing float, it woulda said 'FLOAT myNumber'!!!"

If I wanted a "new yourClass", I would've typed "yourClass anObject"

And for that matter, what would be a good search phrase to find info on this, cause "why double declare class" and "why declare a class instance as new" and all the related phrases that come to mind don't turn up a damn thing....

Thanks, cause this is really bugging the shit outta me.

Advertisement

Well, most languages have evolved so that you don't actually need to do that. Ex C# has had the var keyword since 3.0 and C++11 introduced the use of the auto keyword to implicitly define a type.

Well, what brought it up was reading up on using lists in Unity, and their example was

List<T> myList = new List<T>();

To me, it should be a no brainer, "Hey, I said myList was "List<T>" so it should probably be a "new List<T>"

In implicitly typed C# syntax, that would be expressed as "var myList = new List<T>();", and would be exactly equivalent in the subsequent code.

Niko Suni

In C++ you'd write:


int anInt;
myClass anObject;

or for a pointer to an object (can by null) in C++98:


int* anIntPointer = new int;
myClass* aClassPointer = new myClass;

or in C++11 you can make the above not repeat itself:


auto anIntPointer = new int;
auto aClassPointer = new myClass;

Say you have a Base class and a derived class. Unless I'm mistaken, the following code is valid


Base base=new Derived;

Say you have a Base class and a derived class. Unless I'm mistaken, the following code is valid





Base base=new Derived;

Precisely. This is the main reason that prevents type declarations from being eradicated entirely in statically typed languages. Even "var" and "auto" are picking a specific static type. They just allow you to use them to reduce typing.

If you use C#, use 'var' to reduce redundant typing, ESPECIALLY for long types like Dictionary<string,object>. But don't forget that being able to designate a variable's type manually when you need to is also very important.

In C++ you'd write:


int anInt;
myClass anObject;

or for a pointer to an object (can by null) in C++98:


int* anIntPointer = new int;
myClass* aClassPointer = new myClass;

or in C++11 you can make the above not repeat itself:


auto anIntPointer = new int;
auto aClassPointer = new myClass;

And in C++14 you can write this as long as the range supports a push_back method of value type


auto add_value_to_range(auto range, auto value) { range.push_back(value); return true; }

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Say you have a Base class and a derived class. Unless I'm mistaken, the following code is valid


Base base=new Derived;

An example of this is that you might want to have a for loop that goes through all of your Shapes and changes their colour to green. The Shape class has a colour, and derrived classes like Square and Circle have what ever extra properties they need. You would have an array of type shape that contains Cirles and Squares.


Shape [] arr = new Shape [2];
arr[0] = new Circle();
arr[1] = new Square();

for(int i = 0; i < arr.length; ++i) {
    arr[i].setColour(GREEN);
}
Speaking of C# that's because variable declaration and object instantiation are two different things

Here we are just declaring a variable of type List<T>
List<T> myList;

It has no value, and is not initialized, it can't be used unless it's used in an out parameter

What would happen if it automatically create a new instance of the object just by declaring it? If it was a value type, like an int or float there would be no problem at all, but it would never work for a reference type.

First of all a reference type may not have a parameterless constructor, or it could be an abstract class or even an Interface, there would be no way to initialize then this way, or it could have no public constructors at all, what if it uses some kind of factory class to initialize the objects?

And of course, what if you just want the variable to receive an instance from somewhere else? You do not always want a new object, sometimes you will receive it as a parameter or from calling some other function

So you say it's a "List<T>", but how can the compiler know you really want a "new List<T>" and not an existing one, or some other object that inherits from List<T>? What if you really want a new List<T> but you want it to be created by a function that initializes it with some values?

This topic is closed to new replies.

Advertisement