0
#include <iostream>


struct something{
    int i;

    something(int i) : i(i) {}
};

class otherthing {

    otherthing(){}

    static something foo(int value)
    {
        return { value };
    }
};

In this example what the foo function returns? Am I understand correctly that it creates an object on the heap and returns a pointer? Is this considered bad style? Or is it ok for structs and not classes?

I can feel that it might be a stupid question but I tried to google it and I could not find the answer. I apologize in advance if this is dumb.

OverDoze
  • 13
  • 4
  • 2
    Where do you see a pointer? Pointers don't appear out of thin air. Somewhere, somehow, a pointer needs to be declared. Where is it? – Sam Varshavchik Jan 25 '21 at 13:00
  • 1
    I suspect you're getting confused with a [factory function](https://stackoverflow.com/questions/5120768/how-to-implement-the-factory-method-pattern-in-c-correctly) which typically uses `new` in which case it would return a pointer. But you have `otherthing::foo()` returning an instance of `something` on the stack. – acraig5075 Jan 25 '21 at 13:03
  • You said it returns a `something` by value, so it does. It just uses [copy list initialisation](https://en.cppreference.com/w/cpp/language/list_initialization) to do so. – underscore_d Jan 25 '21 at 13:06
  • Here the `foo` class static function is a *factory* function that creates a `something`. But if the object produced was *polymorphic*, then it would really need to return a `something*` or better `std::unique_ptr` so that polymorphism will work. Not the case here, but that's one reason a pointer would be returned instead of return-by-value. – Eljay Jan 25 '21 at 20:05

3 Answers3

4

In this example what the foo function returns?

Object of type something, initialized with value value.

Am I understand correctly that it creates an object on the heap and returns a pointer?

No. It creates an object with automatic lifetime (typically associated with stack memory). It returns that object and no pointer.

Is this considered bad style?

It's quite useless in this example, but in general this approach is called factory pattern. It allows you to separate creation of object from other logic, and that would be good style actually.

Or is it ok for structs and not classes?

The only difference between struct and class in C++ is the default access modifier. In struct member are by default public, in class members are by default private.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
1

Am I understand correctly that it creates an object on the heap and returns a pointer?

No, it does not return a pointer, it returns an instance of something with automatic storage duration (which is commonly allocated on the stack) which for the return will utilize copy elision

Or is it ok for structs and not classes?

A struct and a class only differ in the default access modifier, besides that there is no difference.

t.niese
  • 39,256
  • 9
  • 74
  • 101
0

In C++ (unlike C#) classes and structs are identical but for one tiny detail: the default access level for a class is private; for a struct it's public.

In your example, a something object is created on the stack in foo. When foo is called, e.g.

something s = otherthing.foo();

then the object is copied into s on the stack.

No pointers are involved.

Jasper Kent
  • 3,546
  • 15
  • 21