-1

I'm pretty new to c++ language however I have experience with python and a little bit with java.

Since c++ is pretty overwhelming and there is like always few ways to achieve certain behavior I have 2 questions related to oop.

  1. What is the best way to create class constructor with parameters? For example let's say we have sth like that:

    struct Test { int x; int y; };

And there are 2 popular ways for constructor:

1.

   Test(int x, int y) : x{x}, y{y} { // rest of constructor};
   Test(int x1, int y1) {
     x = x1;
     y = y1;
     // rest of constructor
   }

Which one should I use and why?

  1. Which style is more like c++ to create class object and why?

    Test* testptr = new Test(1, 1);

vs

Test test = Test(1, 1);
  • 2
    For this struct you probably don't need or want a constructor. You can create a variable like `Test myTest{2,3};` which will initialize x to 2 and y to 3. – drescherjm Apr 14 '21 at 16:59
  • Yes I know but I just wanted to ilustrate what I mean cause my english is pretty bad. Also I want to know if there maybe both constructor are correct but the solution depends on complexity of the class? – Robert Kwiatkowski Apr 14 '21 at 17:12
  • 1
    For a struct like this I would likely use neither option for question #2. Again I would use `Test myTest{2,3};` most of the time. The use of `new` in `c++` is discouraged and this is a small struct. – drescherjm Apr 14 '21 at 17:13
  • Man do you even read what I'm writing? – Robert Kwiatkowski Apr 14 '21 at 17:14
  • Yes I did read what you wrote and even gave you 1 reason for the why part. – drescherjm Apr 14 '21 at 17:14
  • This struct is just EXAMPLE. I don't want to write long and complex code. I just want to know which solution to use when the class is complex, and which for simple ones. – Robert Kwiatkowski Apr 14 '21 at 17:18
  • 1
    It depends on the situation which option to prefer. You may want `std::unique_ptr` also – drescherjm Apr 14 '21 at 17:18
  • Yes I know and thats why I want to know why since I can't find any article about which option to use in which situation. – Robert Kwiatkowski Apr 14 '21 at 17:20
  • 1
    This may help a little but it came before c++11 which included smart pointers and a shift to discourage the use of `new` and raw owning pointers: [https://stackoverflow.com/questions/1021138/differences-between-dynamic-memory-and-ordinary-memory](https://stackoverflow.com/questions/1021138/differences-between-dynamic-memory-and-ordinary-memory) – drescherjm Apr 14 '21 at 17:23
  • I hoped to find a better link but I don't have time at the moment. – drescherjm Apr 14 '21 at 17:24
  • 1
    Please ask one question per SO question. You have two *very* different questions. – n. m. could be an AI Apr 14 '21 at 17:34
  • Thank you @drescherjm. – Robert Kwiatkowski Apr 14 '21 at 17:37
  • Ok @n.'pronouns'm. my mistake. It's good that you try to keep the order in here, but if you aren't answering any of mine questions it's hipocritical... – Robert Kwiatkowski Apr 14 '21 at 17:38

1 Answers1

2
  1. The most C++-ish way to create a struct is struct_name{value1, value2}. And you don't need to declare your own constructor.

If you really want to use a constructor, use the one with initializer syntax. Here is the difference. Let's consider a structure with two fields. So it looks like: {one, two}. When you use initializer syntax, you create the needed struct immediately before the actual constructor code you wrote between curly braces. So it looks like: {1, 2} (we have values now). However, when you initialize those fields inside a constructor's body, it will create a structure with default values first and then change them. So it looks like: {0, 0} ...constructor is working... {1,2}. Ok, the last thing about it is parameter declaration. You should use constant references as it prevents a programmer from changing those parameters inside the function body, and these variables are passed by reference and not copied. Let's look at two examples:

void fun1(int a, int b);
void fun2(const int& a, const int& b);

fun1 here copies those two parameters before using them inside the body. However, fun2 gets only references to the variables and work with their values directly. Also, because of the const, you cannot change them, so it is completely safe.

  1. Modern C++ does not like the new operator. We use it only inside constructors and calling the delete operator in destructors. However, it is not recommended to use the new operator in other cases as it is much harder to prevent memory leaks, and it violates the RAII idiom. If you really must use raw pointers, then consider putting them inside smart pointers.
  • Great! That's what I wanted to hear. How about using constructor when I have complex class with many fields to initalize, but also some methods to call. Should I still use the initalizer syntax? 2. Ok so it's risky and unnecessary to use raw pointer+new. However `Test test = Test(1, 1);` is probably ineffective, How about unique_ptr to create class instance? – Robert Kwiatkowski Apr 14 '21 at 17:56
  • 1
    The part about using `const int&` instead of `int` is nonsense. Also, while the advice to use init list is good, the explanation about `{0, 0} ...constructor is working... {1,2}` is inaccurate for this specific case. – Eugene Apr 14 '21 at 18:01
  • 1
    @RobertKwiatkowski Do not use pointers, either plain or smart, unless you have a good reason. `new` is inefficient. `Test test = Test(1, 1);` is efficient but too verbose. – Eugene Apr 14 '21 at 18:05
  • OK but please keep in mind this test is just oversimplified. I wanted to know how should I initalize class instance when I have many fields, methods etc and how to deal with simple structs what I know now thanks to You guys. – Robert Kwiatkowski Apr 14 '21 at 18:06
  • @RobertKwiatkowski, 1. It is always better to initialize fields immediately if you can. If you need to do additional computations, put that code inside a constructor's body. 2. No, actually it is much more effective, than using pointers. Dereferencing causes performance issues so you should not use it every time, By the way, the compiler usually chooses where to place a variable on its own. Check [this](https://stackoverflow.com/questions/8754854/when-is-a-c-class-instantiated-on-the-stack) thread for more info. – Mykhailo Mushynskyi Apr 14 '21 at 18:07
  • @Eugene what is the problem with verbosity? I've always thought it's important to have clear code which is easy to read. Since you say it's efficient option why should the verbosity be a problem? – Robert Kwiatkowski Apr 14 '21 at 18:08
  • @RobertKwiatkowski, it is much clearer in C++ to write this way: `Test tst(1,2);`. Or `auto tst = Test(1,2)` if you get that values from a fuction. – Mykhailo Mushynskyi Apr 14 '21 at 18:11
  • You are completly right, the option `Test test(1, 2)` is fine. I'm just subconsciously trying to write python/java code all the time. I really appreciate your help, thank you! – Robert Kwiatkowski Apr 14 '21 at 18:13