Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a Java-like C++ enum as a member variable of another class?

Tags:

c++

I'm trying to use the solution for java-like enums in C++.

My Problem is that I'm trying to use the enum as a member in another class. So first we start with the familiar Planet enum:

#ifndef PLANETS_H
#define PLANETS_H

class Planet {
  public:
    static const double G = 6.67300E-11;
    // Enum value DECLARATIONS - they are defined later
    static const Planet MERCURY;
    static const Planet VENUS;
    // ...

  private:
    double mass;   // in kilograms
    double radius; // in meters

  private:
    Planet(double mass, double radius) {
        this->mass = mass;
        this->radius = radius;
    }

  public:

    double surfaceGravity() {
        return G * mass / (radius * radius);
    }
};

// Enum value DEFINITIONS
// The initialization occurs in the scope of the class,
// so the private Planet constructor can be used.
const Planet Planet::MERCURY = Planet(3.303e+23, 2.4397e6);
const Planet Planet::VENUS = Planet(4.869e+24, 6.0518e6);

#endif // PLANETS_H

Then we have a SolarSystem object that takes Planet objects.

#ifndef SOLARSYSTEM_H
#define SOLARSYSTEM_H

#include "Planets.h"
class SolarSystem {
  public:
    SolarSystem(int distance, const Planet& planet) {
        this->distance = distance;
        this->planet = planet;
    }

  private:
    int distance;   // in kilometers
    Planet planet;

};


#endif // SOLARSYSTEM_H

Now if we try to compile this we get the following errors:

SolarSystem.h: In constructor 'SolarSystem::SolarSystem(int, const Planet&)':
SolarSystem.h:7:53: error: no matching function for call to 'Planet::Planet()'
SolarSystem.h:7:53: note: candidates are:
Planets.h:17:5: note: Planet::Planet(double, double)
Planets.h:17:5: note:   candidate expects 2 arguments, 0 provided
Planets.h:4:7: note: Planet::Planet(const Planet&)
Planets.h:4:7: note:   candidate expects 1 argument, 0 provided

The problem can be fixed by including an empty Planet() constructor.

I was wondering if that is the most appropriate fix or if there is a solution that does not involve an empty constructor.

like image 498
James Oltmans Avatar asked Jan 29 '26 11:01

James Oltmans


1 Answers

You should make Planet planet a reference and initialize it in the initializer list. Otherwise, C++ tries to make a copy of an instance of the Planet - precisely the thing that you wanted to avoid.

class SolarSystem {
public:
    SolarSystem(int distance, const Planet& planet)
    :   distance(distance)
    ,   planet(planet) {
    }

private:
    int distance;   // in kilometers
    const Planet& planet;

};
like image 142
Sergey Kalinichenko Avatar answered Jan 31 '26 04:01

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!