Advertisement Jump to content
Sign in to follow this  
Aspirer

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

This topic is 1867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by Aspirer

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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>"

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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;

Share this post


Link to post
Share on other sites

 

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; }
Edited by NightCreature83

Share this post


Link to post
Share on other sites

 

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);
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!