Return a Struct from a class

Hi Folks,

I think I've successfully written a class that returns a struct. However I am having problems making use of the struct when the class is called. I've written this code below just to exemplify the problem...

class Pizza{
  int radius;
  int pie;
  
  struct pizzaStruct{
    int Circumference;
    int Area;
  };

  struct pizzaStruct pizzaData;

  public:
  Pizza(int R){
    radius = R;
    pie = 3.14159;
  }

  struct pizzaStruct Update(){
    pizzaData.Circumference = pie*radius;
    pizzaData.Area = pie*radius^2;
    return pizzaData;
  }
};

Pizza medium(10);

void setup() {
Serial.begin(9600);
}

void loop() {

medium.Update();
// I want to access the values in the struct that should be returned. Something like this... 
//struct medPizza = medium.Update();
//Serial.println(medPizza.CSA);

delay(1000);
}

Be great to get your thoughts.

Cheers.

In general, caller allocates works better. Especially on memory constrained devices.

Not sure I understand, I'm pretty new to programming. Are you suggesting I don't use a struct?

yep fair point. Probably just change it to 3 for now. Crux of my problem us using the Struct..

Delta_G:
Why not just call update and then use the copy that lives in the class?

medium.Update();

int someVar = medium.pizzaData.Area;

This looks exactly like what I want to do but I get error..

struct_func:10: error: 'Pizza::pizzaStruct Pizza::pizzaData' is private

Chris102:
Are you suggesting I don't use a struct?

There is no part of my post that even implies such a thing.

Not sure I understand...

The caller is loop so loop provides storage for the struct (local variable named result).

...
  void Update(pizzaStruct & result){
    result.Circumference = pie*radius;
    result.Area = pie*radius^2;
  }
...

void loop( void )
{
  Pizza::pizzaStruct result;

  medium.Update(result);

  Serial.println(result.Circumference);
  Serial.println(Area);
}

^2 is not raise to the power of two.

PI is a predefined macro that expands to a reasonable value of π for the Arduino environment.

Delta_G:
All class data is private unless you make it public.

Put

public:

At the beginning of your class definition or even better leave it private and write a getter method that returns a pointer to it.

TBH I don't get why the separate struct. It has exactly the same data you already have. If you know radius then you know circumference and area, you shouldn't need to store those separately. But even if you do, why wouldn't they be members of the same class that radius is?

Google "Code Smells" and study the parts about OOP.

Thanks - I have it doing what I want now.

I actually just made up this code as a simplified example, so its function doesn't necessarily make sense. Basically I want to be able to return multiple values from a class I have written. So I get it to return a Struct. Not sure if this is the best way to do things? Will have a look into methods using pointers.

Thanks for the tip looks a useful website haha