What .NET Developers Must Know about C++ Classes: Listing 5

Delegating constructors in C++, C# and Java.

#include <string>
#include <stdlib.h>

class MyBase {
  MyBase(const int field) : field_(field) {};
  // The following overloading gets a string and delegates construction
  // to the version that gets int.
  MyBase(const string fieldStr) : MyBase(atoi(fieldStr.c_str())) {}
  // in C#, the line above is:
  // MyBase(string fieldStr) : this(Int32.Parse(fieldStr)) {}
  // in Java,
  // MyBase(String fieldStr) { this(Integer.parseInt(fieldStr)); }
  int field_;

class MyDerived : public MyBase {
  MyDerived(const int field) :  MyBase(field) {...}
  // in C#, MyDerived(int field) :  base(field) {...}
  // in Java, MyDerived(int field) { super(field); ...}

About the Author

Diego Dagum is a software architect and developer with more than 20 years of experience. He can be reached at [email protected]

comments powered by Disqus


  • VS Code Now Has Apple Silicon Builds for Native Mac Development

    Goodbye Rosetta, hello M1. Visual Studio Code has been updated with new builds that let it run natively on machines with Apple Silicon (M1), the company's own ARM64 chips.

  • Visual Studio 2019 for Mac v8.9 Ships with .NET 6 Preview 1 Support

    During its Ignite 2021 online event for IT pros and developers this week, Microsoft shipped Visual Studio 2019 for Mac v8.9, arriving with out-of-the-box support for .NET 6 Preview 1, which the company also released recently.

  • Analyst: TypeScript Now Firmly in Top 10 Echelon (Ruby, Not So Much)

    RedMonk analyst Stephen O'Grady believes TypeScript has achieved the rare feat of firmly ensconcing itself into the top 10 echelon of his ranking, now questioning how high it might go.

  • Black White Wave IMage

    Neural Regression Using PyTorch: Training

    The goal of a regression problem is to predict a single numeric value, for example, predicting the annual revenue of a new restaurant based on variables such as menu prices, number of tables, location and so on.

Upcoming Events