Documentation

    Table of Content
    • CodePorting.Translator Product Family
      • CodePorting.Translator Cs2Cpp
        • Getting Started
          • Product Overview
          • Installation
          • Licensing
          • How to use CodePorting.Translator Cs2Cpp
            • How to Use Command line to translate and build Projects
            • How to Use GUI to translate and build Projects
        • Developer Guide
          • Translating Simple C# Projects
            • Translating Simple Console Application
            • Translating Simple Library
            • Translating Simple NUnit Test
          • Translating Dependent C# Projects
            • Translating Dependent Console Application
            • Translating Dependent Library
            • Translating Dependent NUnit Test
          • Qt support
          • Translating Complex C# Projects
            • How to Convert Complex C# Console Application to C++
            • How to Convert Complex C# Library to C++
            • How to Convert Complex C# NUnit Test to C++
          • What Converts to What
            • AbstractClasses
            • BreakStatements
            • ClassAutoProperties
            • ClassConstructors
            • ClassEvents
            • ClassFinalizers
            • ClassGenericMethods
            • ClassIndexers
            • ClassMethods
            • ClassProperties
            • ClassStaticConstructor
            • ClassStaticMethods
            • ClassStaticProperties
            • ClassVirtualMethods
            • ContinueStatements
            • Delegates
            • DoWhileStatements
            • Enums
            • EnumTypeCast
            • Exceptions
            • ExpectedException
            • ForeachStatements
            • ForeachStatementsStd
            • ForStatements
            • GenericClasses
            • GenericDelegates
            • GenericInterfaces
            • GenericStructs
            • IfStatements
            • LambdaExpressions
            • NestedClasses
            • ReturnStatements
            • SimpleClass
            • SimpleInterface
            • SimpleStruct
            • SimpleTest
            • StandardTypeCast
            • StaticClass
            • SwitchStatements
            • TestWithSetupMethods
            • ThrowStatements
            • TryCatchFinallyStatements
            • TryCatchStatements
            • TryFinallyStatements
            • VarExpressions
            • WhileStatements
          • CodePorting Translator Cs2Cpp Attributes
          • CodePorting Translator Cs2Cpp Configuration File
            • CodePorting.Translator Cs2Cpp Configuration File Structure
            • Attributes in Configuration file
            • Configuration file Nodes
            • Configuration file Options
          • Memory Management Model
            • Memory Management Model Description
            • Using aliasing constructor to create a smart pointer
          • Cmake Support
          • C++ code injection
          • C++ user-defined exception classes
          • Limitations and Bugs
            • Translator Limitations and Bugs
            • Library Limitations and Bugs
            • Cpp Code Injection
        • Release Notes
          • 2022
            • CodePorting.Translator Cs2Cpp 22.6
            • CodePorting.Native Cs2Cpp 22.5
            • CodePorting.Native Cs2Cpp 22.4
            • CodePorting.Native Cs2Cpp 22.3
            • CodePorting.Native Cs2Cpp 22.2
            • CodePorting.Native Cs2Cpp 22.1
          • 2021
            • CodePorting.Native Cs2Cpp 21.12
            • CodePorting.Native Cs2Cpp 21.11
            • CodePorting.Native Cs2Cpp 21.10.1
            • CodePorting.Native Cs2Cpp 21.10
            • CodePorting.Native Cs2Cpp 21.9
            • CodePorting.Native Cs2Cpp 21.8
            • CodePorting.Native Cs2Cpp 21.7
            • CodePorting.Native Cs2Cpp 21.6
            • CodePorting.Native Cs2Cpp 21.5
            • CodePorting.Native Cs2Cpp 21.4
            • CodePorting.Native Cs2Cpp 21.3
            • CodePorting.Native Cs2Cpp 21.2
            • CodePorting.Native Cs2Cpp 21.1
          • 2020
            • CodePorting.Native Cs2Cpp 20.12
            • CodePorting.Native Cs2Cpp 20.11
            • CodePorting.Native Cs2Cpp 20.10
            • CodePorting.Native Cs2Cpp 20.9
            • CodePorting.Native Cs2Cpp 20.8
            • CodePorting.Native Cs2Cpp 20.7
            • CodePorting.Native Cs2Cpp 20.6
            • CodePorting.Native Cs2Cpp 20.5
            • CodePorting.Native Cs2Cpp 20.4
            • CodePorting.Native Cs2Cpp 20.3
            • CodePorting.Native Cs2Cpp 20.2
            • CodePorting.Native Cs2Cpp 20.1
          • 2019
            • CodePorting.Native Cs2Cpp 19.1
            • CodePorting.Native Cs2Cpp 19.2
            • CodePorting.Native Cs2Cpp 19.3
            • CodePorting.Native Cs2Cpp 19.4
            • CodePorting.Native Cs2Cpp 19.4.1
            • CodePorting.Native Cs2Cpp 19.5
            • CodePorting.Native Cs2Cpp 19.6
            • CodePorting.Native Cs2Cpp 19.7
            • CodePorting.Native Cs2Cpp 19.8
            • CodePorting.Native Cs2Cpp 19.9
            • CodePorting.Native Cs2Cpp 19.10
            • CodePorting.Native Cs2Cpp 19.11
            • CodePorting.Native Cs2Cpp 19.12
          • 2018
            • CodePorting.Native Cs2Cpp 18.9
            • CodePorting.Native Cs2Cpp 18.9.1
            • CodePorting.Native Cs2Cpp 18.10
            • CodePorting.Native Cs2Cpp 18.11
            • CodePorting.Native Cs2Cpp 18.12
    1. Home
    2. CodePorting.Translator Product Family
    3. CodePorting.Translator Cs2Cpp
    4. Developer Guide
    5. What Converts to What
    6. GenericClasses

    GenericClasses

    What's on this Page

      • Source C# Code
      • Translated Code
        • C++ Header

    This example demonstrates how generic classes are translated to C++. They become C++ template classes which are inherited from System::Object and have RTTI declared.

    Additional command-line options passed to CodePorting.Translator.Cs2Cpp: none.

    Source C# Code

    using System;
    
    namespace TypesPorting
    {
        public class GenericClass<TInner>
        {
        }
    
        public class GenericClassWithTypeConstraint<TInner> where TInner : ICloneable
        {
        }
    
        public class GenericClassWithClassConstraint<TInner> where TInner : class
        {
        }
    
        public class GenericClassWithStructConstraint<TInner> where TInner : struct
        {
        }
    
        public class GenericClassWithNewConstraint<TInner> where TInner : new()
        {
        }
    
        public class GenericClassWithSeveralConstraints<TInner> where TInner : class, ICloneable, new()
        {
        }
    }

    Translated Code

    C++ Header

    #pragma once
    
    #include <system/object.h>
    #include <system/icloneable.h>
    #include <system/details/pointer_collection_helpers.h>
    #include <system/constraints.h>
    #include <cstdint>
    
    namespace TypesPorting {
    
    template<typename TInner>
    class GenericClass : public System::Object
    {
        typedef GenericClass<TInner> ThisType;
        typedef System::Object BaseType;
        
        typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
        RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
        
        template<typename FT0> friend class GenericClass;
        
    public:
    
        void SetTemplateWeakPtr(uint32_t argument) override
        {
            switch (argument)
            {
                case 0:
                    break;
                    
            }
        }
        
    };
    
    template<typename TInner>
    class GenericClassWithTypeConstraint : public System::Object
    {
        typedef System::ICloneable BaseT_ICloneable;
        assert_is_base_of(BaseT_ICloneable, TInner);
        
        typedef GenericClassWithTypeConstraint<TInner> ThisType;
        typedef System::Object BaseType;
        
        typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
        RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
        
        template<typename FT0> friend class GenericClassWithTypeConstraint;
        
    public:
    
        void SetTemplateWeakPtr(uint32_t argument) override
        {
            switch (argument)
            {
                case 0:
                    break;
                    
            }
        }
        
    };
    
    template<typename TInner>
    class GenericClassWithClassConstraint : public System::Object
    {
        assert_is_cs_class(TInner);
        
        typedef GenericClassWithClassConstraint<TInner> ThisType;
        typedef System::Object BaseType;
        
        typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
        RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
        
        template<typename FT0> friend class GenericClassWithClassConstraint;
        
    public:
    
        void SetTemplateWeakPtr(uint32_t argument) override
        {
            switch (argument)
            {
                case 0:
                    break;
                    
            }
        }
        
    };
    
    template<typename TInner>
    class GenericClassWithStructConstraint : public System::Object
    {
        assert_is_cs_struct(TInner);
        
        typedef GenericClassWithStructConstraint<TInner> ThisType;
        typedef System::Object BaseType;
        
        typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
        RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
        
        template<typename FT0> friend class GenericClassWithStructConstraint;
        
    public:
    
        void SetTemplateWeakPtr(uint32_t argument) override
        {
            switch (argument)
            {
                case 0:
                    break;
                    
            }
        }
        
    };
    
    template<typename TInner>
    class GenericClassWithNewConstraint : public System::Object
    {
        assert_is_constructable(TInner);
        
        typedef GenericClassWithNewConstraint<TInner> ThisType;
        typedef System::Object BaseType;
        
        typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
        RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
        
        template<typename FT0> friend class GenericClassWithNewConstraint;
        
    public:
    
        void SetTemplateWeakPtr(uint32_t argument) override
        {
            switch (argument)
            {
                case 0:
                    break;
                    
            }
        }
        
    };
    
    template<typename TInner>
    class GenericClassWithSeveralConstraints : public System::Object
    {
        assert_is_cs_class(TInner);
        typedef System::ICloneable BaseT_ICloneable;
        assert_is_base_of(BaseT_ICloneable, TInner);
        assert_is_constructable(TInner);
        
        typedef GenericClassWithSeveralConstraints<TInner> ThisType;
        typedef System::Object BaseType;
        
        typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
        RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
        
        template<typename FT0> friend class GenericClassWithSeveralConstraints;
        
    public:
    
        void SetTemplateWeakPtr(uint32_t argument) override
        {
            switch (argument)
            {
                case 0:
                    break;
                    
            }
        }
        
    };
    
    } // namespace TypesPorting