-1

Consider this snippet:

#include <iostream>

typedef struct Test_ {
    float value1;
    float value2;
} Test;

int main()
{
    Test t = Test();
    std::cout << t.value1 << std::endl; // Prints 0 
    std::cout << t.value2 << std::endl; // Prints 0
}

What am I actually doing here Test t = Test(); (what is this called: Test())? And is it possible to use this syntax to inilize the member values of Test to something else?

Or do I have to do something like Test t = Test{.value1 = 1, .value2 = 2}; to get different init values?

Edit: Perhaps I was a bit vague in what I was asking about. My question was basically what is this syntax: Test t = Test();

mrfr
  • 1,724
  • 2
  • 23
  • 44
  • 1
    `Test t = Test();`is same has `Test t;` both using the default constructor => no initialization – bruno Jul 29 '20 at 13:25
  • 4
    FYI. In c++ your typedef is redundant. A struct is a type already – pm100 Jul 29 '20 at 13:25
  • 2
    It's not quite obvious what you want to do. You can assign different values at creation using `Test t{1.f, 5.f};`. You can add default values to all members or you can add a constructor that will assign default values. – Yksisarvinen Jul 29 '20 at 13:27
  • 1
    Please read a [good book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). This type of question and many others you will be asking are all covered in those. – rubenvb Jul 29 '20 at 13:27
  • 1
    `Prints 1` It doesn't. https://ideone.com/BmfSJs – n. m. could be an AI Jul 29 '20 at 13:27
  • @n.'pronouns'm. `Prints 0` it does ? ;) – bruno Jul 29 '20 at 13:36
  • @n.'pronouns'm. Yes, type error by me. – mrfr Jul 29 '20 at 13:44
  • @bruno Thanks. That answered my question. So by doing `Test()` i basically don't do anything? – mrfr Jul 29 '20 at 13:45
  • @FelixRosén `Test()` creates a temporary instance of Test calling the default constructor which does nothing – bruno Jul 29 '20 at 13:48
  • @bruno Alright, how can I modify the constructor of a typedef struct? – mrfr Jul 29 '20 at 13:52
  • 1
    @FelixRosén constructor of the *struct* (not the *typedef*), you can have `struct Test { float value1; float value2; Test(float v1 = 0, float v2 = 0) : value1(v1), value2(v2) {} };` allowing to do `Test t;` setting fields to 0 or ` Test t(1,2);` writting 1 then 2 etc you can also do `Test t = Test(1, 2);` but is same as `Test t(1,2);` in more complicated – bruno Jul 29 '20 at 13:58

1 Answers1

1

What you need is:

#include <iostream>

struct Test {
    float value1;
    float value2;
};

int main()
{
    Test t = {1, 2};
    std::cout << t.value1 << std::endl; // Prints 1. 
    std::cout << t.value2 << std::endl; // Prints 2.
}
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • considering the probable level of the OP in C++ what about to also speak about to have constructor(s) getting or not fields value in arg and may be with param default value ? – bruno Jul 29 '20 at 13:34
  • @bruno: feel free to add that in your own answer or in mine if you want. I am too lazy to write a tutorial on the C++ various initialization syntaxes... – Serge Ballesta Jul 29 '20 at 13:45
  • Yes I know how to initialize it. But yea perhaps I was a bit vauge in what I was asking about. My question was basically what is this syntax: `Test t = Test();` – mrfr Jul 29 '20 at 13:46
  • `Test()` do not initialize the fields (default constructor) so you affect *t* with non initialized values which is the same as doing `Test t;` alone – bruno Jul 29 '20 at 13:50
  • @SergeBallesta I was myself to lazy to put an answer this is why I proposed you do to :-) – bruno Jul 29 '20 at 13:52