10

Suppose I want to pass a temporary object into a function. Is there a way to do that in 1 line of code vs. 2, with a struct?


With a class, I can do:

class_func(TestClass(5, 7));

given:

class TestClass
{
private:
    int a;
    short b;

public:
    TestClass(int a_a, short a_b) : a(a_a), b(a_b)
    {
    }

    int A() const
    {
        return a;
    }

    short B() const
    {
        return b;
    }
};

void class_func(const TestClass & a_class)
{
    printf("%d %d\n", a_class.A(), a_class.B());
}

Now, how do I do that with a struct? The closest I've got is:

test_struct new_struct = { 5, 7 };
struct_func(new_struct);

given:

struct test_struct
{
    int a;
    short b;
};

void struct_func(const test_struct & a_struct)
{
    printf("%d %d\n", a_struct.a, a_struct.b);
}

The object is more simple, but I wonder if there's a way to do the struct member initialization right in line with the function call, without giving the struct a constructor. (I don't want a constructor. The whole reason I'm using a struct is to avoid the boilerplate get/set class conventions in this isolated case.)

Matthew
  • 101
  • 1
  • 1
  • 3
  • 3
    You can still write a constructor keeping the member variables public. So you don't require get/set methods. Is there any problem in doing that? – Naveen Feb 06 '10 at 07:07
  • Did you mean to achieve the struct initialization in C? In C++ a struct can also do whatever you are doing in the class. – sand Feb 06 '10 at 10:43

5 Answers5

10

An alternative to providing a constructor in your struct, would be to provide a make_xxx free function:

struct Point {int x; int y;};

Point makePoint(int x, int y) {Point p = {x, y}; return p;}

plot(makePoint(12, 34));

One reason why you might want to avoid constructors in structs is to allow brace-initialization in arrays of structs:

// Not allowed when constructor is defined
const Point points[] = {{12,34}, {23,45}, {34,56}};

vs

const Point points[] = {Point(12,34), Point(23,45), Point(34,56)};
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
  • 3
    Note that in C++11, use can use brace-initialization even if the `Point` constructor is defined. In that case, the arguments within the braces will be passed to the constructor. This feature is called [Uniform Initialization](https://en.wikipedia.org/wiki/C%2B%2B11#Uniform_initialization). – Emile Cormier Jun 27 '15 at 17:55
7

This is possible in the C++11 standard. Basically, you are able to do this:

struct_func(test_struct{5, 7});

This is already available in GCC as of version 4.4.

Melebius
  • 6,183
  • 4
  • 39
  • 52
Manuel
  • 12,749
  • 1
  • 27
  • 35
  • 2
    In fact, you don't even need to explicitly declare the type, unless there's ambiguity about the types that can be created by the braced init list: `#include struct S { int i; double d; }; void f(S s) { std::cout << s.i << ' ' << s.d << std::endl; } int main(int, char **) { f( {7, 42} ); }` I don't think I'd ever have gotten by without uniform initialisation or all the other massive upgrades made in C++11 that I completely take for granted. – underscore_d Jun 27 '16 at 15:09
2

You can do it the same way as you do with the class. Just give your struct a constructor and you can create it inline just like the struct. Your objections about using a constructor are unfounded. The primary difference between a class and a struct is the default visibility associated with it. For classes, it's private; for structs, public. There's no "boilerplate," and you don't have to obey any "conventions" that you don't want to.

struct test_struct
{
    int a;
    short b;

    test_struct(int a_, int b_): a(a_), b(b_) { }
};

struct_func(test_struct(5, 7));
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • @Terry Mahaffey, you definitely can use brace-initialization with a class. The constraints are the same as for struct. The difference between `struct` and `class` keyword *is* default visibility only (for members and base classes). You can even mix the keywords: `struct t {}; class t v;`. – avakar Feb 06 '10 at 07:45
  • Yep, you're right. I thought only structs could be PODs for some reason. I withdraw my comment – Terry Mahaffey Feb 06 '10 at 08:29
0

I'm not c++ expert, but couldn't you just place the creation statement inside the argument list of your function call?

joejoeson
  • 1,107
  • 1
  • 10
  • 14
0

Structs can also have constructors, so you can do the same thing with them you do with the class example.

Terry Mahaffey
  • 11,775
  • 1
  • 35
  • 44