0

This simple object return works fine.

Bubbles& Foo() {
  static Bubbles foo(10);
  foo.print();
  return foo;
}

int main() {

  Bubbles *bar;
  bar = &Foo();

  bar->print();

  printf("Program Ends");
  return 0;
}

Now I need to know how to return an Array of Objects! I have 0 idea on how I should declare it

All I know is:

Bubbles& createBubbles() {
 static Bubbles *BubblesArray[arrayNumber];
 for (int i = 0; i < arrayNumber; i++) {
    BubblesArray[i] = new Bubbles(i);
    BubblesArray[i]->print();

  }

  return BubblesArray;
}

seems to create an array of Objects the way I need.
So how can I return this array so I can use it outside the function?

4
  • 4
    Use std::array or std::vector and this becomes trivial. Otherwise it's not happening unless you pass the array as an output parameter. Commented Jan 31, 2020 at 4:22
  • BubblesArray is a pointer to the start of an array. Hence you want the return type to be Bubbles* i.e. Bubbles* createBubbles(), then the return statement makes sense. Note that you using a static variable won't play nice with multi-threading and there's a lot of memory leak in the code. Consider using std:: functions and classes such as std::vector and std::unique_ptr or std::shared_ptr. Commented Jan 31, 2020 at 4:36
  • Why is the variable static? This is, except in narrow circumstances, a bad design. If you added an explanation of why you think that is necessary, then we could probably suggest a better approach. Commented Jan 31, 2020 at 4:47
  • ok so I guess the best approach is std:: functions. I will try std::vector Commented Jan 31, 2020 at 4:51

2 Answers 2

1

Your return type expects only 1 Bubbles object. If you want to return an array, you have to change the function return type. I would strongly recommend not playing with raw array and stick with the std library. Using C++11 (DISCLAIMER: CODE UNTESTED) will look something along the line of:

std::vector<Bubbles> createBubbles(const int& arrayNumber) {
 std::vector<Bubbles> bubblesVector;
 for (int i = 0; i < arrayNumber; i++) {
    bubblesVector.push_back(Bubbles(i));
    bubblesVector[i].print();    
  }    
  return bubblesVector;
}

Note that this assume your Bubbles object has appropriate default/copy/move constructor, destructor, assignment operator...

Simple return type std::vector<Bubbles> can take advantage of copy-elision which is extremely efficient.

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

2 Comments

Since the OP has no need for resizing the array, I think std::array<Bubbles, arrayNumber> would be better here.
Bubbles tend to pop. I'd suggest a vector.
1

You can get the same behavior as in your first example. You just have to specify the type correctly:

using BubblesArrayType = Bubbles*[arrayNumber];

BubblesArrayType& createBubbles() {
  static BubblesArrayType BubblesArray;
  //...
  return BubblesArray;
}

or you can let type deduction figure the type out for you:

auto& createBubbles() {
  static Bubbles *BubblesArray[arrayNumber];
  //...
  return BubblesArray;
}

But as mentioned in comments and other answers, this is unlikely to be a good design and style. Without further information it is not really clear though, why you are using static and return-by-reference in the first place.

4 Comments

Also possible without the typedef, though even uglier: Bubbles* (&createBubbles())[arrayNumber] { /* ... */ }
@N.Shead I hope I will never see that in any code I work with.
@N.Shead I suppose auto createBubbles() -> Bubbles*(&)[arrayNumber] could be an ok alternative though, or really just auto& createBubbles().
Yes, auto type deduction is definitely the way to go there. Though honestly I'd prefer to not be returning array types at all — I just felt it worthwhile to point out the possibility!

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.