C++ I want to avoid pointers and instead use STLs and References for maintaining a small data cache





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















#include <iostream>
#include <vector>
#include <string>


struct A
{
std::string a;
std::vector<A> avector;
};

typedef std::vector<A> Avector;

A& func(A& x)
{
A& ret = x.avector[0];
return ret;
}

int main()
{
A genesis = { "Parent", std::vector<A>() };
A l1 = { "Child1", std::vector<A>() };
A l2 = { "Child2", std::vector<A>() };

genesis.avector.push_back(l1);
genesis.avector.push_back(l2);

std::cout << "l1: " << l1.a << std::endl; //shows "Child1"
std::cout << "l2: " << l2.a << std::endl; //shows "Child2"

A& lx = func(genesis);
lx.a = "Childx";

std::cout << "l1: " << l1.a << std::endl; //!!still shows "Child1"

return 1;
}


So, basically what i want is to have a single copy of data overall i.e., Genesis object and two more objects l1 and l2 as objecst of Genesis.avector



However I'm unable to modify this later as every time I end up modifying the copies but not the actual data under Genesis object.



Thanks for your help!










share|improve this question




















  • 3





    How is A& lx = func(l); compiling? What is l?

    – NathanOliver
    Nov 16 '18 at 15:14











  • std::vector::push_back will always copy (or move) the object. You may consider storing references in your vectors or using e.g. std::shared_ptr

    – Yksisarvinen
    Nov 16 '18 at 15:16






  • 3





    @Yksisarvinen Note that references cannot be stored in a std::vector directly. It might cope with std::reference_wrapper.

    – Angew
    Nov 16 '18 at 15:17






  • 1





    "every time I end up modifying the copies but not the actual data under Genesis object." you are modifying actual data, just do not create or use l1 l2 - they are copies.

    – Slava
    Nov 16 '18 at 15:22











  • @NathanOliver sorry, changed the code now. it's 'genesis'

    – Avinash MK
    Nov 16 '18 at 15:41


















0















#include <iostream>
#include <vector>
#include <string>


struct A
{
std::string a;
std::vector<A> avector;
};

typedef std::vector<A> Avector;

A& func(A& x)
{
A& ret = x.avector[0];
return ret;
}

int main()
{
A genesis = { "Parent", std::vector<A>() };
A l1 = { "Child1", std::vector<A>() };
A l2 = { "Child2", std::vector<A>() };

genesis.avector.push_back(l1);
genesis.avector.push_back(l2);

std::cout << "l1: " << l1.a << std::endl; //shows "Child1"
std::cout << "l2: " << l2.a << std::endl; //shows "Child2"

A& lx = func(genesis);
lx.a = "Childx";

std::cout << "l1: " << l1.a << std::endl; //!!still shows "Child1"

return 1;
}


So, basically what i want is to have a single copy of data overall i.e., Genesis object and two more objects l1 and l2 as objecst of Genesis.avector



However I'm unable to modify this later as every time I end up modifying the copies but not the actual data under Genesis object.



Thanks for your help!










share|improve this question




















  • 3





    How is A& lx = func(l); compiling? What is l?

    – NathanOliver
    Nov 16 '18 at 15:14











  • std::vector::push_back will always copy (or move) the object. You may consider storing references in your vectors or using e.g. std::shared_ptr

    – Yksisarvinen
    Nov 16 '18 at 15:16






  • 3





    @Yksisarvinen Note that references cannot be stored in a std::vector directly. It might cope with std::reference_wrapper.

    – Angew
    Nov 16 '18 at 15:17






  • 1





    "every time I end up modifying the copies but not the actual data under Genesis object." you are modifying actual data, just do not create or use l1 l2 - they are copies.

    – Slava
    Nov 16 '18 at 15:22











  • @NathanOliver sorry, changed the code now. it's 'genesis'

    – Avinash MK
    Nov 16 '18 at 15:41














0












0








0








#include <iostream>
#include <vector>
#include <string>


struct A
{
std::string a;
std::vector<A> avector;
};

typedef std::vector<A> Avector;

A& func(A& x)
{
A& ret = x.avector[0];
return ret;
}

int main()
{
A genesis = { "Parent", std::vector<A>() };
A l1 = { "Child1", std::vector<A>() };
A l2 = { "Child2", std::vector<A>() };

genesis.avector.push_back(l1);
genesis.avector.push_back(l2);

std::cout << "l1: " << l1.a << std::endl; //shows "Child1"
std::cout << "l2: " << l2.a << std::endl; //shows "Child2"

A& lx = func(genesis);
lx.a = "Childx";

std::cout << "l1: " << l1.a << std::endl; //!!still shows "Child1"

return 1;
}


So, basically what i want is to have a single copy of data overall i.e., Genesis object and two more objects l1 and l2 as objecst of Genesis.avector



However I'm unable to modify this later as every time I end up modifying the copies but not the actual data under Genesis object.



Thanks for your help!










share|improve this question
















#include <iostream>
#include <vector>
#include <string>


struct A
{
std::string a;
std::vector<A> avector;
};

typedef std::vector<A> Avector;

A& func(A& x)
{
A& ret = x.avector[0];
return ret;
}

int main()
{
A genesis = { "Parent", std::vector<A>() };
A l1 = { "Child1", std::vector<A>() };
A l2 = { "Child2", std::vector<A>() };

genesis.avector.push_back(l1);
genesis.avector.push_back(l2);

std::cout << "l1: " << l1.a << std::endl; //shows "Child1"
std::cout << "l2: " << l2.a << std::endl; //shows "Child2"

A& lx = func(genesis);
lx.a = "Childx";

std::cout << "l1: " << l1.a << std::endl; //!!still shows "Child1"

return 1;
}


So, basically what i want is to have a single copy of data overall i.e., Genesis object and two more objects l1 and l2 as objecst of Genesis.avector



However I'm unable to modify this later as every time I end up modifying the copies but not the actual data under Genesis object.



Thanks for your help!







c++ reference stl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 15:41







Avinash MK

















asked Nov 16 '18 at 15:12









Avinash MKAvinash MK

11




11








  • 3





    How is A& lx = func(l); compiling? What is l?

    – NathanOliver
    Nov 16 '18 at 15:14











  • std::vector::push_back will always copy (or move) the object. You may consider storing references in your vectors or using e.g. std::shared_ptr

    – Yksisarvinen
    Nov 16 '18 at 15:16






  • 3





    @Yksisarvinen Note that references cannot be stored in a std::vector directly. It might cope with std::reference_wrapper.

    – Angew
    Nov 16 '18 at 15:17






  • 1





    "every time I end up modifying the copies but not the actual data under Genesis object." you are modifying actual data, just do not create or use l1 l2 - they are copies.

    – Slava
    Nov 16 '18 at 15:22











  • @NathanOliver sorry, changed the code now. it's 'genesis'

    – Avinash MK
    Nov 16 '18 at 15:41














  • 3





    How is A& lx = func(l); compiling? What is l?

    – NathanOliver
    Nov 16 '18 at 15:14











  • std::vector::push_back will always copy (or move) the object. You may consider storing references in your vectors or using e.g. std::shared_ptr

    – Yksisarvinen
    Nov 16 '18 at 15:16






  • 3





    @Yksisarvinen Note that references cannot be stored in a std::vector directly. It might cope with std::reference_wrapper.

    – Angew
    Nov 16 '18 at 15:17






  • 1





    "every time I end up modifying the copies but not the actual data under Genesis object." you are modifying actual data, just do not create or use l1 l2 - they are copies.

    – Slava
    Nov 16 '18 at 15:22











  • @NathanOliver sorry, changed the code now. it's 'genesis'

    – Avinash MK
    Nov 16 '18 at 15:41








3




3





How is A& lx = func(l); compiling? What is l?

– NathanOliver
Nov 16 '18 at 15:14





How is A& lx = func(l); compiling? What is l?

– NathanOliver
Nov 16 '18 at 15:14













std::vector::push_back will always copy (or move) the object. You may consider storing references in your vectors or using e.g. std::shared_ptr

– Yksisarvinen
Nov 16 '18 at 15:16





std::vector::push_back will always copy (or move) the object. You may consider storing references in your vectors or using e.g. std::shared_ptr

– Yksisarvinen
Nov 16 '18 at 15:16




3




3





@Yksisarvinen Note that references cannot be stored in a std::vector directly. It might cope with std::reference_wrapper.

– Angew
Nov 16 '18 at 15:17





@Yksisarvinen Note that references cannot be stored in a std::vector directly. It might cope with std::reference_wrapper.

– Angew
Nov 16 '18 at 15:17




1




1





"every time I end up modifying the copies but not the actual data under Genesis object." you are modifying actual data, just do not create or use l1 l2 - they are copies.

– Slava
Nov 16 '18 at 15:22





"every time I end up modifying the copies but not the actual data under Genesis object." you are modifying actual data, just do not create or use l1 l2 - they are copies.

– Slava
Nov 16 '18 at 15:22













@NathanOliver sorry, changed the code now. it's 'genesis'

– Avinash MK
Nov 16 '18 at 15:41





@NathanOliver sorry, changed the code now. it's 'genesis'

– Avinash MK
Nov 16 '18 at 15:41












2 Answers
2






active

oldest

votes


















1














In your code:



A genesis = { "Parent", std::vector<A>() };
A l1 = { "Child1", std::vector<A>() };
A l2 = { "Child2", std::vector<A>() };

genesis .avector.push_back(l1);
genesis .avector.push_back(l2);


You have 5 A instances. The 3 that you declare and the 2 copies you have in your vector.



So the more correct (but possible buggy) way to do it would be:



A genesis = { "Parent", std::vector<A>() };
genesis .avector.emplace_back("Child1", std::vector<A>());
genesis .avector.emplace_back("Child2", std::vector<A>());

A& l1 = genesis.avector[0];
A& l2 = genesis.avector[1];


Now you only have 3 instances. l1 and l2 are references to the items in the vector, so changes to them will be refleced in the vector as well.



I said this is possibly buggy. When you alter the vector (adding something else), the vector might have to reallocate so any references you have will be invalid and the result is undefined behavior.



If you do need to modify the vector, I would do vector<unique_ptr<A>>. Then A& l1 = *genesis.avector[0] and this will remain valid until the item get's removed from the vector.



You can also try std::reference_wrapper instead of unique_ptr if you want the objects to live on the stack.
I would try to avoid this as it's more obvious that something get destroyed when you erase from a vector than when a local variable goes out of scope.






share|improve this answer
























  • I'm using C++ 98 so used push_back and got these errors: test46_1.cc:23: error: no matching function for call to 'std::vector<A, std::allocator<A> >::push_back(const char [7], std::vector<A, std::allocator<A> >)' /usr/include/c++/4.3/bits/stl_vector.h:686: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A, _Alloc = std::allocator<A>] Is this due to push_back() ?

    – Avinash MK
    Nov 16 '18 at 15:55













  • @AvinashMK emplace_back takes the parameters to construct the elemnt in place. Use push_back({...}) to make it work. Your setup is strange, brace initialization A l1 = {...} is a C++11 feature.

    – Sorin
    Nov 16 '18 at 16:08











  • Sorry for that, even i'm unsure of this possibility. I just tried to have a constructor and am now able to overcome the issue. I'm posting my answer below. Please let me know if it looks fine

    – Avinash MK
    Nov 16 '18 at 16:15





















0














I was able to make this work with introducing a constructor and some help from @Sorin.

#include <iostream>
#include <vector>
#include <string>


struct A
{
std::string a;
std::vector<A> avector;
A(std::string aa)
{
a = aa;
}
};

typedef std::vector<A> Avector;

A& func(A& x)
{
return x.avector[0];
}

int main()
{
A genesis("Parent");
genesis.avector.push_back(static_cast<A>("Child1"));
genesis.avector.push_back(static_cast<A>("Child2"));

A& l1 = genesis.avector[0];
A& l2 = genesis.avector[1];

std::cout << "l1: " << l1.a << std::endl;
std::cout << "l2: " << l2.a << std::endl;

A& lx = func(genesis);
lx.a = "Childx";

std::cout << "l1: " << genesis.avector[0].a << std::endl;
std::cout << "l2: " << l2.a << std::endl;
return 1;
}


Output:



l1: Child1



l2: Child2



l1: Childx



l2: Child2






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340535%2fc-i-want-to-avoid-pointers-and-instead-use-stls-and-references-for-maintaining%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    In your code:



    A genesis = { "Parent", std::vector<A>() };
    A l1 = { "Child1", std::vector<A>() };
    A l2 = { "Child2", std::vector<A>() };

    genesis .avector.push_back(l1);
    genesis .avector.push_back(l2);


    You have 5 A instances. The 3 that you declare and the 2 copies you have in your vector.



    So the more correct (but possible buggy) way to do it would be:



    A genesis = { "Parent", std::vector<A>() };
    genesis .avector.emplace_back("Child1", std::vector<A>());
    genesis .avector.emplace_back("Child2", std::vector<A>());

    A& l1 = genesis.avector[0];
    A& l2 = genesis.avector[1];


    Now you only have 3 instances. l1 and l2 are references to the items in the vector, so changes to them will be refleced in the vector as well.



    I said this is possibly buggy. When you alter the vector (adding something else), the vector might have to reallocate so any references you have will be invalid and the result is undefined behavior.



    If you do need to modify the vector, I would do vector<unique_ptr<A>>. Then A& l1 = *genesis.avector[0] and this will remain valid until the item get's removed from the vector.



    You can also try std::reference_wrapper instead of unique_ptr if you want the objects to live on the stack.
    I would try to avoid this as it's more obvious that something get destroyed when you erase from a vector than when a local variable goes out of scope.






    share|improve this answer
























    • I'm using C++ 98 so used push_back and got these errors: test46_1.cc:23: error: no matching function for call to 'std::vector<A, std::allocator<A> >::push_back(const char [7], std::vector<A, std::allocator<A> >)' /usr/include/c++/4.3/bits/stl_vector.h:686: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A, _Alloc = std::allocator<A>] Is this due to push_back() ?

      – Avinash MK
      Nov 16 '18 at 15:55













    • @AvinashMK emplace_back takes the parameters to construct the elemnt in place. Use push_back({...}) to make it work. Your setup is strange, brace initialization A l1 = {...} is a C++11 feature.

      – Sorin
      Nov 16 '18 at 16:08











    • Sorry for that, even i'm unsure of this possibility. I just tried to have a constructor and am now able to overcome the issue. I'm posting my answer below. Please let me know if it looks fine

      – Avinash MK
      Nov 16 '18 at 16:15


















    1














    In your code:



    A genesis = { "Parent", std::vector<A>() };
    A l1 = { "Child1", std::vector<A>() };
    A l2 = { "Child2", std::vector<A>() };

    genesis .avector.push_back(l1);
    genesis .avector.push_back(l2);


    You have 5 A instances. The 3 that you declare and the 2 copies you have in your vector.



    So the more correct (but possible buggy) way to do it would be:



    A genesis = { "Parent", std::vector<A>() };
    genesis .avector.emplace_back("Child1", std::vector<A>());
    genesis .avector.emplace_back("Child2", std::vector<A>());

    A& l1 = genesis.avector[0];
    A& l2 = genesis.avector[1];


    Now you only have 3 instances. l1 and l2 are references to the items in the vector, so changes to them will be refleced in the vector as well.



    I said this is possibly buggy. When you alter the vector (adding something else), the vector might have to reallocate so any references you have will be invalid and the result is undefined behavior.



    If you do need to modify the vector, I would do vector<unique_ptr<A>>. Then A& l1 = *genesis.avector[0] and this will remain valid until the item get's removed from the vector.



    You can also try std::reference_wrapper instead of unique_ptr if you want the objects to live on the stack.
    I would try to avoid this as it's more obvious that something get destroyed when you erase from a vector than when a local variable goes out of scope.






    share|improve this answer
























    • I'm using C++ 98 so used push_back and got these errors: test46_1.cc:23: error: no matching function for call to 'std::vector<A, std::allocator<A> >::push_back(const char [7], std::vector<A, std::allocator<A> >)' /usr/include/c++/4.3/bits/stl_vector.h:686: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A, _Alloc = std::allocator<A>] Is this due to push_back() ?

      – Avinash MK
      Nov 16 '18 at 15:55













    • @AvinashMK emplace_back takes the parameters to construct the elemnt in place. Use push_back({...}) to make it work. Your setup is strange, brace initialization A l1 = {...} is a C++11 feature.

      – Sorin
      Nov 16 '18 at 16:08











    • Sorry for that, even i'm unsure of this possibility. I just tried to have a constructor and am now able to overcome the issue. I'm posting my answer below. Please let me know if it looks fine

      – Avinash MK
      Nov 16 '18 at 16:15
















    1












    1








    1







    In your code:



    A genesis = { "Parent", std::vector<A>() };
    A l1 = { "Child1", std::vector<A>() };
    A l2 = { "Child2", std::vector<A>() };

    genesis .avector.push_back(l1);
    genesis .avector.push_back(l2);


    You have 5 A instances. The 3 that you declare and the 2 copies you have in your vector.



    So the more correct (but possible buggy) way to do it would be:



    A genesis = { "Parent", std::vector<A>() };
    genesis .avector.emplace_back("Child1", std::vector<A>());
    genesis .avector.emplace_back("Child2", std::vector<A>());

    A& l1 = genesis.avector[0];
    A& l2 = genesis.avector[1];


    Now you only have 3 instances. l1 and l2 are references to the items in the vector, so changes to them will be refleced in the vector as well.



    I said this is possibly buggy. When you alter the vector (adding something else), the vector might have to reallocate so any references you have will be invalid and the result is undefined behavior.



    If you do need to modify the vector, I would do vector<unique_ptr<A>>. Then A& l1 = *genesis.avector[0] and this will remain valid until the item get's removed from the vector.



    You can also try std::reference_wrapper instead of unique_ptr if you want the objects to live on the stack.
    I would try to avoid this as it's more obvious that something get destroyed when you erase from a vector than when a local variable goes out of scope.






    share|improve this answer













    In your code:



    A genesis = { "Parent", std::vector<A>() };
    A l1 = { "Child1", std::vector<A>() };
    A l2 = { "Child2", std::vector<A>() };

    genesis .avector.push_back(l1);
    genesis .avector.push_back(l2);


    You have 5 A instances. The 3 that you declare and the 2 copies you have in your vector.



    So the more correct (but possible buggy) way to do it would be:



    A genesis = { "Parent", std::vector<A>() };
    genesis .avector.emplace_back("Child1", std::vector<A>());
    genesis .avector.emplace_back("Child2", std::vector<A>());

    A& l1 = genesis.avector[0];
    A& l2 = genesis.avector[1];


    Now you only have 3 instances. l1 and l2 are references to the items in the vector, so changes to them will be refleced in the vector as well.



    I said this is possibly buggy. When you alter the vector (adding something else), the vector might have to reallocate so any references you have will be invalid and the result is undefined behavior.



    If you do need to modify the vector, I would do vector<unique_ptr<A>>. Then A& l1 = *genesis.avector[0] and this will remain valid until the item get's removed from the vector.



    You can also try std::reference_wrapper instead of unique_ptr if you want the objects to live on the stack.
    I would try to avoid this as it's more obvious that something get destroyed when you erase from a vector than when a local variable goes out of scope.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 15:39









    SorinSorin

    9,8191322




    9,8191322













    • I'm using C++ 98 so used push_back and got these errors: test46_1.cc:23: error: no matching function for call to 'std::vector<A, std::allocator<A> >::push_back(const char [7], std::vector<A, std::allocator<A> >)' /usr/include/c++/4.3/bits/stl_vector.h:686: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A, _Alloc = std::allocator<A>] Is this due to push_back() ?

      – Avinash MK
      Nov 16 '18 at 15:55













    • @AvinashMK emplace_back takes the parameters to construct the elemnt in place. Use push_back({...}) to make it work. Your setup is strange, brace initialization A l1 = {...} is a C++11 feature.

      – Sorin
      Nov 16 '18 at 16:08











    • Sorry for that, even i'm unsure of this possibility. I just tried to have a constructor and am now able to overcome the issue. I'm posting my answer below. Please let me know if it looks fine

      – Avinash MK
      Nov 16 '18 at 16:15





















    • I'm using C++ 98 so used push_back and got these errors: test46_1.cc:23: error: no matching function for call to 'std::vector<A, std::allocator<A> >::push_back(const char [7], std::vector<A, std::allocator<A> >)' /usr/include/c++/4.3/bits/stl_vector.h:686: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A, _Alloc = std::allocator<A>] Is this due to push_back() ?

      – Avinash MK
      Nov 16 '18 at 15:55













    • @AvinashMK emplace_back takes the parameters to construct the elemnt in place. Use push_back({...}) to make it work. Your setup is strange, brace initialization A l1 = {...} is a C++11 feature.

      – Sorin
      Nov 16 '18 at 16:08











    • Sorry for that, even i'm unsure of this possibility. I just tried to have a constructor and am now able to overcome the issue. I'm posting my answer below. Please let me know if it looks fine

      – Avinash MK
      Nov 16 '18 at 16:15



















    I'm using C++ 98 so used push_back and got these errors: test46_1.cc:23: error: no matching function for call to 'std::vector<A, std::allocator<A> >::push_back(const char [7], std::vector<A, std::allocator<A> >)' /usr/include/c++/4.3/bits/stl_vector.h:686: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A, _Alloc = std::allocator<A>] Is this due to push_back() ?

    – Avinash MK
    Nov 16 '18 at 15:55







    I'm using C++ 98 so used push_back and got these errors: test46_1.cc:23: error: no matching function for call to 'std::vector<A, std::allocator<A> >::push_back(const char [7], std::vector<A, std::allocator<A> >)' /usr/include/c++/4.3/bits/stl_vector.h:686: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A, _Alloc = std::allocator<A>] Is this due to push_back() ?

    – Avinash MK
    Nov 16 '18 at 15:55















    @AvinashMK emplace_back takes the parameters to construct the elemnt in place. Use push_back({...}) to make it work. Your setup is strange, brace initialization A l1 = {...} is a C++11 feature.

    – Sorin
    Nov 16 '18 at 16:08





    @AvinashMK emplace_back takes the parameters to construct the elemnt in place. Use push_back({...}) to make it work. Your setup is strange, brace initialization A l1 = {...} is a C++11 feature.

    – Sorin
    Nov 16 '18 at 16:08













    Sorry for that, even i'm unsure of this possibility. I just tried to have a constructor and am now able to overcome the issue. I'm posting my answer below. Please let me know if it looks fine

    – Avinash MK
    Nov 16 '18 at 16:15







    Sorry for that, even i'm unsure of this possibility. I just tried to have a constructor and am now able to overcome the issue. I'm posting my answer below. Please let me know if it looks fine

    – Avinash MK
    Nov 16 '18 at 16:15















    0














    I was able to make this work with introducing a constructor and some help from @Sorin.

    #include <iostream>
    #include <vector>
    #include <string>


    struct A
    {
    std::string a;
    std::vector<A> avector;
    A(std::string aa)
    {
    a = aa;
    }
    };

    typedef std::vector<A> Avector;

    A& func(A& x)
    {
    return x.avector[0];
    }

    int main()
    {
    A genesis("Parent");
    genesis.avector.push_back(static_cast<A>("Child1"));
    genesis.avector.push_back(static_cast<A>("Child2"));

    A& l1 = genesis.avector[0];
    A& l2 = genesis.avector[1];

    std::cout << "l1: " << l1.a << std::endl;
    std::cout << "l2: " << l2.a << std::endl;

    A& lx = func(genesis);
    lx.a = "Childx";

    std::cout << "l1: " << genesis.avector[0].a << std::endl;
    std::cout << "l2: " << l2.a << std::endl;
    return 1;
    }


    Output:



    l1: Child1



    l2: Child2



    l1: Childx



    l2: Child2






    share|improve this answer




























      0














      I was able to make this work with introducing a constructor and some help from @Sorin.

      #include <iostream>
      #include <vector>
      #include <string>


      struct A
      {
      std::string a;
      std::vector<A> avector;
      A(std::string aa)
      {
      a = aa;
      }
      };

      typedef std::vector<A> Avector;

      A& func(A& x)
      {
      return x.avector[0];
      }

      int main()
      {
      A genesis("Parent");
      genesis.avector.push_back(static_cast<A>("Child1"));
      genesis.avector.push_back(static_cast<A>("Child2"));

      A& l1 = genesis.avector[0];
      A& l2 = genesis.avector[1];

      std::cout << "l1: " << l1.a << std::endl;
      std::cout << "l2: " << l2.a << std::endl;

      A& lx = func(genesis);
      lx.a = "Childx";

      std::cout << "l1: " << genesis.avector[0].a << std::endl;
      std::cout << "l2: " << l2.a << std::endl;
      return 1;
      }


      Output:



      l1: Child1



      l2: Child2



      l1: Childx



      l2: Child2






      share|improve this answer


























        0












        0








        0







        I was able to make this work with introducing a constructor and some help from @Sorin.

        #include <iostream>
        #include <vector>
        #include <string>


        struct A
        {
        std::string a;
        std::vector<A> avector;
        A(std::string aa)
        {
        a = aa;
        }
        };

        typedef std::vector<A> Avector;

        A& func(A& x)
        {
        return x.avector[0];
        }

        int main()
        {
        A genesis("Parent");
        genesis.avector.push_back(static_cast<A>("Child1"));
        genesis.avector.push_back(static_cast<A>("Child2"));

        A& l1 = genesis.avector[0];
        A& l2 = genesis.avector[1];

        std::cout << "l1: " << l1.a << std::endl;
        std::cout << "l2: " << l2.a << std::endl;

        A& lx = func(genesis);
        lx.a = "Childx";

        std::cout << "l1: " << genesis.avector[0].a << std::endl;
        std::cout << "l2: " << l2.a << std::endl;
        return 1;
        }


        Output:



        l1: Child1



        l2: Child2



        l1: Childx



        l2: Child2






        share|improve this answer













        I was able to make this work with introducing a constructor and some help from @Sorin.

        #include <iostream>
        #include <vector>
        #include <string>


        struct A
        {
        std::string a;
        std::vector<A> avector;
        A(std::string aa)
        {
        a = aa;
        }
        };

        typedef std::vector<A> Avector;

        A& func(A& x)
        {
        return x.avector[0];
        }

        int main()
        {
        A genesis("Parent");
        genesis.avector.push_back(static_cast<A>("Child1"));
        genesis.avector.push_back(static_cast<A>("Child2"));

        A& l1 = genesis.avector[0];
        A& l2 = genesis.avector[1];

        std::cout << "l1: " << l1.a << std::endl;
        std::cout << "l2: " << l2.a << std::endl;

        A& lx = func(genesis);
        lx.a = "Childx";

        std::cout << "l1: " << genesis.avector[0].a << std::endl;
        std::cout << "l2: " << l2.a << std::endl;
        return 1;
        }


        Output:



        l1: Child1



        l2: Child2



        l1: Childx



        l2: Child2







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 16:18









        Avinash MKAvinash MK

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


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




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340535%2fc-i-want-to-avoid-pointers-and-instead-use-stls-and-references-for-maintaining%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine