0

So I have a class Star which inherits another class CelestialBody . There's a third class Planet as well.

In my class Star in the private section I have an array of Planet objects Planet* system and an int n standing for the number of the planets.

Problem is - how do I construct it? I've tried this:

Star(char* newName, double newX, double newY, double newDiam, Planet* newSystem, int newN) : CelestialBody(newName, newX, newY, newDiam)
    {
        system = new Planet[newN];  
        system = newSystem;
        n = newN;
    }

but apparently it doesn't work...

Thanks for helping my out in advance!

13
  • 3
    Assigning two times in a row different values to system does not make much sense... Commented Jul 15, 2014 at 19:03
  • @WojtekSurowka Well... I went for the logic that I'm constructing with a char* which usually goes the following way: name = new char[strlen(newName) + 1]; name = newName; Commented Jul 15, 2014 at 19:05
  • "Usually"? Never not usually. What you think about I guess is strcpy(name, newName) instead of the second assignment. Commented Jul 15, 2014 at 19:07
  • Why not just use std::vector and forget about the pointers? Commented Jul 15, 2014 at 19:10
  • @WojtekSurowka strcpy(name, newName), yes, that's exactly what I meant. It's really late here, sorry. Can't believe I did so many mistakes in such a little time span. You get the point now, though. I hope... Commented Jul 15, 2014 at 19:11

1 Answer 1

1

This re-assignment is wrong:

system = new Planet[newSystem]; // <<== should be newN
system = newSystem;

you created an array, and then immediately "leaked" it. You can fix this by adding a loop, like this:

system = new Planet[newN]; 
for (int i = 0 ; i != newN ; i++) {
    system[i] = newSystem[i];
}

Although this would work, it is not the best solution: one reason is that your code needs to manage the system[] array, deleting it when you are done with the Star. C++ standard library offers much better choices.

For example, you could make your system an std::vector<shared_ptr<Planet>> - i.e. a vector of shared pointers to a Planet. The caller would pass you a container of planets (it could be a pointer and a count, in the way that you did, but it could also be a real C++ container).

Sign up to request clarification or add additional context in comments.

4 Comments

It's for a school assignment.. And we haven't covered std::vector yet. Thanks for the answer, though. Works like a charm!
@user3213110 - It's for a school assignment The issue with this is that you're going to hand in an assignment that seems to work, possibly be graded an "A", but is riddled with bugs that you won't know exist. That shouldn't be the goal of a C++ course (create buggy programs).
@PaulMcKenzie I completely get your point. My university is a little odd though. Out of 8 classes 1 is programming-related and the other 7 are Maths. I guess proper coding isn't a priority around here. Will definitely check out std::vector though.
@PaulMcKenzie Creating a buggy program may very well be the goal of an assignment, assuming that the instructor goes through all the bugs with the student, and shows how C++ library helps avoiding these bugs. Students often learn more from their mistakes than from their textbooks :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.