4

I am (very) new to C++, and am having some issues understanding structs. I'm using example code from a course, so I have:

struct Point {
   int x;
   int y;
}

In another bit of code, I want to write the following:

drawLine(point1, new Point(x, y));

This does not work, and I understand that I am trying to declare a struct as if it were a class, but is there a way to do this? I have currently written a helper function which returns a Point from an x,y, but this seems roundabout.

woopf
  • 83
  • 1
  • 3
  • You've got a memory leak unless `drawLine()` takes ownership of the object. – sharptooth Apr 05 '11 at 07:17
  • 1
    Just add a constructor (works with struct just like class) `Point(int px,int py) : x(px), y(py) {}` – Martin York Apr 05 '11 at 07:18
  • 1
    C++ is fundamentally different from C# or Java -- you manage your own memory. Any time you say `new`, you need a corresponding `delete`. – tylerl Apr 05 '11 at 07:20
  • 2
    ... but usually you can just say `drawLine(point1, Point(x, y));`. The expression `Point(x,y)` is a temporary object whose lifetime ends at the `;`. – MSalters Apr 05 '11 at 07:54
  • I think you can do that with tuples, while in C++11 you can initialize structs as `drawLine(point1, Point{x, y});` – Quartz Oct 24 '14 at 17:05

3 Answers3

9

The problem isn't that you're trying to declare a struct as if it were a class (in fact, there's very little difference between the two in C++).

The problem is that you're trying to construct a Point from two ints, and there ins't a suitable constructor. Here is how you can add one:

struct Point {
   int x;
   int y;
   Point(int x, int y): x(x), y(y) {}
};
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • That way of initializing a struct is obsolete and will not compile with MSVC. –  Jul 04 '12 at 11:41
  • @IngeHenriksen there is nothing obsolete about the suggestion by NPE. all you need is a semicolon at the end of the struct's last bracket, but otherwise it is fine. – johnbakers Apr 09 '13 at 09:19
3

new returns a pointer. Just drawLine(point1, Point(x,y)) should work if you define the appropriate constructor for Point.

(drawLine(point1, *(new Point(x,y))) would also work, but would introduce a memory leak; every new should be balanced by a delete.)

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • When I try that, I get a very obscure "no suitable constructor exists from "int" to "Point". Specifically, I'm trying drawLine(point1, Point(x+1, y+2)), if relevant. – woopf Apr 05 '11 at 07:19
  • 1
    @woopf: Define the constructor as given by aix. – Fred Foo Apr 05 '11 at 07:19
0

The difference between struct and class in c++ is not that huge. Why don't you add a constructor to your struct?

tomsv
  • 7,207
  • 6
  • 55
  • 88
  • By "not that huge", I think you mean that members of a struct are public by default, while members of a class default to private. It's important to understand how they *do* in fact differ, as opposed to just being aware that the difference is minimal. More details are [here](http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c). – Cody Gray - on strike Apr 05 '11 at 07:55
  • @Cody Gray What's important to understand is that they don't really differ, except for some easily overridable defaults. A `struct` still defines a class type, and can have all of the attributes of a class type (private members, functions, even virtual, constructors, destructors, etc.). The choice between the keywords `struct` and `class` is purely one of convention and coding guidelines. – James Kanze Apr 05 '11 at 08:25
  • @James: Where did I argue differently? – Cody Gray - on strike Apr 05 '11 at 08:37
  • @James, @Cody: I'd say dontomaso's point is just that "even though" it's [just] a `struct` it can still have a constructor, letting `woopf` know C++ structs aren't restricted to C-like features. Other details of the differences aren't relevant to this specific question. – Tony Delroy Apr 05 '11 at 09:33