Posts Tagged constructor

C++ For C# Developers: Part 19 – Dynamic Allocation

Tags: , , , ,

So far, all of the memory our C++ code has allocated has either been global or on the stack. For the many times when the amount of memory isn’t known at compile time, we’ll need dynamic allocation. Today we’ll go over both the basics of new and delete, but also dive into some advanced C++ features such as overloading new and “placement” new.

Read the rest of this article »

5 Comments

C++ For C# Developers: Part 13 – Initialization

Tags: , , , , ,

With constructors under our belts, we can now talk about initialization of structs and other types. This is a far more complex topic than in C#. Read on to learn a lot of nitty-gritty details!

Read the rest of this article »

1 Comment

C++ For C# Developers: Part 12 – Constructors and Destructors

Tags: , , ,

So far with structs we’ve covered data members, member functions, and overloaded operators. Now let’s talk about the main parts of their lifecycle: constructors and destructors. Destructors in particular are very different from C# and represent a signature feature of C++ that has wide-ranging effects on how we write and design code for the language. Read on to learn all about them!

Read the rest of this article »

10 Comments

C++ Scripting: Part 22 – Full Base Type Support

Tags: , , ,

Today we’ll complete our ability to use C++ classes to derive from C# classes and implement C# interfaces. So far we’ve been able to override methods, properties, and indexers. Today we’ll add the ability to override events and derive from classes that don’t have a default constructor.
Those are the last two pieces of the puzzle that will allow us to derive from any C# base type with a C++ class. Read on for all the details about how this works.

Read the rest of this article »

No Comments

C++ Scripting: Part 10 – Full Generics Support

Tags: , , , , , , ,

C# APIs are chock-full of generics. Generic types, generic method parameters, generic return types, generic fields, generic properties, deriving from generic types, and generic constructors. We can find all of these in the Unity and .NET APIs. Some are more frequent than others, but we’re going to need support for all of them to make C++ scripting a viable alternative to C#. Today’s article continues the series by adding just that: support for all of these kinds of generics. Let’s dive into how to use them as well as some bonus items added to the project this week.

Read the rest of this article »

No Comments

From AS3 to C#, Part 5: Static Classes, Destructors, and Constructor Tricks

Tags: , , ,

Last week’s article mostly covered abstract classes, but this week we’ll discuss an even more abstract type of class: static classes. We’ll also explore C#’s anti-constructor, known as a destructor, and some fancy ways to construct a class. Read on and learn some more class-related features that were never available to us in AS3.

Read the rest of this article »

9 Comments

The Right Way to Check An Object’s Type

Tags: , , , , ,

There are lots of ways to check the type of an object in AS3. These include the is operator, the deprecated instanceof operator, the constructor field, and a combination of getQualifiedClassName and getDefinitionByName. Which is fastest, cleanest, and most effective? Today’s article puts them all to the test to find out!

Read the rest of this article »

5 Comments

Constructing Arrays

Tags: , ,

In AS3, you can create an Array can be created with special syntax: myArray = []. You can even fill the Array with values all in one go: myArray = [1,2,3,4,5]. This is a nice shorthand that saves some typing compared to using the constructor: myArray = new Array(1,2,3,4,5). But, which way is faster? Today we find out! UPDATE: added a third way of creating arrays

Read the rest of this article »

9 Comments

Class Bootup Part 2

Tags: , , , ,

Today’s article follows up on an article I wrote way back in August of 2009 about the order of operations when you use a class. In the original article I showed the order of field initializers and constructors. Today I’m expanding on that to show three more chunks of code that are run. Can you guess what those chunks are?

Read the rest of this article »

7 Comments

Constructor Function

Tags: ,

It’s very nice in AS3 to simply state the name of a function and get a Function variable back, regardless of whether or not it is a method, static method, dynamic function, or plain function in a package. Most other languages do not allow this level of convenience. Java doesn’t allow it at all and C++ and AS2 require fanciness in order to pass the this pointer. However, when you want a Function for the constructor of a class, you get a Class variable back. This article will show you a way to get a Function variable that will create an instance of a class when called.

Read the rest of this article »

8 Comments