C++ vector instance is operational when function returns it

C++ vector instance is operational when function returns it



In my class, I have two vector objects:


std::vector<std::string> vecteur_noms_A_;
std::vector<std::string> vecteur_noms_B_;



I also have the following function:


std::vector<std::string> & get_vecteur_noms_A_()
return vecteur_noms_A_;



but I do not have the same for the B one!


B



When I instantiate my class, and do the following instructions in my class constructor; I can see (via debugging or when It throws an error) that:


vecteur_noms_A_.push_back("some value"); // is ok

vecteur_noms_B_.push_back("some other value"); // is not ok <- throws an error.



Debugging let me see that an object (or instance ?)(empty vector) is existing for vecteur_noms_A_ but not for vecteur_noms_B_.


vecteur_noms_A_


vecteur_noms_B_



It is not clear why I observe that comportment I did not expect, I would be happy if I get an explanation.



Does the fact that I define a function forces the compiler to instanciate the object?



Here is an example :


#include <iostream>
#include <vector>

using namespace std;
class GP

public:
//Constructeur
GP(std::vector<std::string> list_names_A, std::vector<std::string> list_names_B
);

//Fonctions
void construction_vecteur_A_B_(std::vector<std::string> list_names_A, std::vector<std::string> list_names_B);


std::vector<std::string>& get_vecteur_noms_A()
return vecteur_noms_A_;


std::vector<std::string> vecteur_noms_A_;
std::vector<std::string> vecteur_noms_B_;
;



GP::GP(std::vector<std::string> list_names_a, std::vector<std::string> list_names_b)

construction_vecteur_A_B_(list_names_a, list_names_b);




void GP::construction_vecteur_A_B_(std::vector<std::string> list_names_a, std::vector<std::string> list_names_b)

for (int i = 0; i < list_names_a.size(); i++)
vecteur_noms_A_.push_back(list_names_a[i]);

for (int i = 0; i < list_names_b.size(); i++)
vecteur_noms_B_.push_back(list_names_b[i]);




int main()


std::vector<std::string> list_names_A = std::vector<std::string>();
std::vector<std::string> list_names_B = std::vector<std::string>();
list_names_A.push_back("A");
list_names_B.push_back("B");

GP(list_names_A, list_names_B);

return 0;



Since the program written here weems to throw no error, I would like to uderstand why :


vecteur_noms_A_



and


vecteur_noms_B_



are existing at execution so that I can do the pushback.
Why isn't it necessary to apply this to them in the constructor:


vecteur_noms_A_ = std::vector<std::string>();
vecteur_noms_B_ = std::vector<std::string>();



Thanks for your help.





Please provide Minimal, Complete, and Verifiable example.
– Algirdas Preidžius
Sep 3 at 13:12





I think we'll need the shortest complete program you can conjure up to be able to make sense of this.
– Bathsheba
Sep 3 at 13:12





Ok, I'll provide one. Thanks for asking for this
– StudentInFinance
Sep 3 at 13:14





You should read more about the "Complete" and "Verifiable" parts of the Minimal, Complete, and Verifiable example.
– molbdnilo
Sep 3 at 13:41





Please provide a complete program, with main(), #includes and everything else. It's hard to figure out what is going on when you only give snippets.
– PlinyTheElder
Sep 3 at 14:57




1 Answer
1



With regards to your last question. It is not necessary to assign a value, such as std::vector<std::string>(), to the member vectors, vecteur_noms_A_, vecteur_noms_B_ as they are default constructed, when your class GP is used, before the body of the constructor is executed.


std::vector<std::string>()


vecteur_noms_A_


vecteur_noms_B_


GP



Notice therefore that if you were to do the following in the body of the constructor of GP:


GP


vecteur_noms_A_ = std::vector<std::string>();
vecteur_noms_B_ = std::vector<std::string>();



It would be equivalent to the following operation:


vecteur_noms_A_.operator=(std::vector<std::string>());
vecteur_noms_B_.operator=(std::vector<std::string>());



That is you are assigning an empty vector (using the copy assignment operator) to your already default constructed empty vector, which is redundant.





Thanks Alexander for these elements. I understand that members of a class will be default constructed unless a specific construction is set after the ":" in the constructor definition. Do you know a way, that could cause a non-defualt construction of vecteur_noms_B_ ? It seems it is what happened to me 3 days ago, but I can't reproduce what caused it (cf. 1st part of this StackOverflow question). Thanks
– StudentInFinance
Sep 3 at 16:02





Unfortunately, as has been mentioned by others in the comments, without a way of reproducing the error you experienced, or more clearly understand the steps required to throw the error, it's not possible to say much. When the vector is created you need not use the default constructor, the vector class has other constructors (en.cppreference.com/w/cpp/container/vector/vector) but it is not clear what that has to do with the problem you reported.
– Alexander
Sep 3 at 16:13




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)