0

Here it is my code. I don't get it why it doesn't print 3, even though in the class constructor param1 becomes 3.

#include <iostream>

using namespace std;

class A{

    int valoare;
public:

    A(int param1 = 3):valoare(param1){}
    int getValoare(){return this -> valoare;}
};

int main()
{

    A vector[] = {*(new A(3)), *(new A(4)), *(new A(5)), *(new A(6))};
    cout << vector[2].getValoare();
    return 0;
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • `param1` becomes `3` only if you do not provide an argument, which you actually do. For `vector[2]`, this argument is `5`. – Daniel Langr Jun 09 '18 at 22:25
  • 3
    You have immediate memory leaks! Do you perhaps come from a Java or C# background where `new` must be used to create new objects? In C++ you don't need that. Doing e.g. `A(3)` without `new` is perfectly fine. – Some programmer dude Jun 09 '18 at 22:25
  • 2
    As for your question, stop just guessing about what things mean in C++, and [get a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn properly. – Some programmer dude Jun 09 '18 at 22:28
  • `vector[2]` is initialised using `*(new A(5))` which gives its `valoare` member the value `5`. And also causes a memory leak - avoid the leak by initialising with `A(5)`. – Peter Jun 09 '18 at 23:35

3 Answers3

3

You might want to read about default arguments: https://en.cppreference.com/w/cpp/language/default_arguments

When you specify an argument for a function with a defualt argument it overrides that default value. Thus, your code will print out 5.

As a side note, your code has a memory leak becuase you allocated memory with the new keyword and never deleted it. You should change the declaration of your Vector, that is, allocate memory on the stack like follows:

Vector = {A(3), A(4), A(5), A(6)}
yakobyd
  • 572
  • 4
  • 12
2

The element at index 2 in the vector was constructed as A(5), so it's value ("valoare") is 5. The = 3 in the function definition is a default argument - which is used if you don't specify one yourself. So if you were to write:

std::cout << A().getValoare();

that would print 3.

But a few more observations are in order:

  1. Prefer English-language names. valoare means "value" in some Latin or European language, right? Romanian perhaps? But - people who don't speak that language won't know that. Since you have to know English to program anyways, that's a safe choice for names.
  2. Try not to use names for variables which are also names of classes in a different namespace. For example, your vector has the same name as std::vector, a class, or rather a class template, in the standard library. Try vec or my_vector or something else that's more distinctive.
  3. You're leaking memory! Why are you using new to create values? Just use the construct, i.e.

    A vector[] = { A(3), A(4), A(5), A(6) };
    

    is just fine.

  4. More generally, you should avoid calling new and delete explicitly, and instead prefer RAII-style classes - which allocate on construction and deallocate on destruction. The simplest thing to do is to switch to using smart pointers

einpoklum
  • 118,144
  • 57
  • 340
  • 684
1

Why the parameter doesn't change in the class constructor?

It doesn't print the value 3 because you're giving it another value.

from cppreference:

Default arguments are used in place of the missing trailing arguments in a function call:

void point(int x = 3, int y = 4);

point(1,2); // calls point(1,2)
point(1);   // calls point(1,4)
point();    // calls point(3,4)
Andreas DM
  • 10,685
  • 6
  • 35
  • 62