0

In C++ how make a class variable in one line? For example: I have a class:

class point{
public:
 int x;
 int y;
};

How to make a variable in one line like java you can do new point(x, y), currently I do make a tmp and then push back to vector or something, are the simply way like java can do what I do in one line?

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 1
    Try `point{x, y}` – 0x5453 Nov 18 '21 at 13:32
  • 2
    Or in a function call you can even omit the type, e.g.: `yourPointVector.push_back({x, y});` – UnholySheep Nov 18 '21 at 13:33
  • `point myPoint{1,2};` – drescherjm Nov 18 '21 at 13:33
  • 1
    If you had trouble using a search engine to find this information, it might be because you have the terminology wrong. You are trying to *instantiate* the class. There is no such thing as "a class variable", and anyway the *variable* isn't the important part of what you're trying to do - that's just a name you're giving to the thing. The hard part is creating the thing. You may also find it useful to follow a tutorial - although many C++ tutorials are out of date. – Karl Knechtel Nov 18 '21 at 13:34
  • DONT use `new` !!! `new` creates an object on the `heap`, which comes with a whole lot of implications you don't want to deal with just now. – Hajo Kirchhoff Nov 18 '21 at 13:35
  • There is a list of good books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Don't make the mistake of believing that C++ is similar to Java; learn things from the beginning. – molbdnilo Nov 18 '21 at 13:38
  • 4
    You will do yourself a big, big favor if you completely forget everything you know about Java, while learning C++. C++ is not Java, and objects in C++ work in fundamentally different ways than they do in Java. Although Java uses somewhat similar syntax, and there are many concepts in common, the similarities are mostly superficial, and random Google searches will not fully explain why. The only way to thoroughly understand how objects in C++ work, on a fundamental level, the only way to do that is to get a C++ textbook, open to chapter 1, and to start reading. – Sam Varshavchik Nov 18 '21 at 13:39
  • @HajoKirchhoff The `new` in the question is a piece of Java code, not C++. The rules are different in Java. – JaMiT Nov 18 '21 at 13:57

2 Answers2

1

For creating a variable of type point on the stack you can use:

point myVariable{5,6};//this creates a point type variable on stack with x=5 and y=6;

So the complete program would look like:

#include <iostream>
class point{
    public:
        int x;
        int y;
};

int main()
{
    point myVariable{5,6};

    return 0;
}

The output of the above program can be seen here. If you want to create a vector of point objects and then add objects into it, then you can use:

//create point objects
point p1{5,6};
point p2{7,8};

//create a vector
std::vector<point> myVector;

//add p1 and p2 into the vector
myVector.push_back(p1);
myVector.push_back(p2);
Jason
  • 36,170
  • 5
  • 26
  • 60
0

Build a constructor Point(int x, int y) : x(x), y(y) {} And then push to vector as usual vec.push_back(Point(x,y))

001
  • 13,291
  • 5
  • 35
  • 66