How to run non const methods on nested objects in Composition /Aggregation pattern in C++?












0














I came to from the C# world and, thanks to the "Properties", I can run a setter method on a nested object without leaking any internals like this:



objectA.objectB.objectC.SetSomething();


So far, I found that the best analog in C++ is to use getters and get nested object by value or const reference than use setters to set them by value. But this would result in the following code:



auto b = objectA().getObjectB();
auto c = b.getObjectC();
c.SetSomething();
b.setObjectC(c);
objectA.setObjectB(b);


Not only this is 5 lines instead of one, but it involves multiple copies of the nested objects in b.setObjectC(c); and objectA.setObjectB(b);



I can let the getters to return a non-const reference of a nested object (or use public variables) and will be able to write in C++:



objectA.objectB.objectC.SetSomething();


But, instantly I get the ability to set objectB without objectA knowledge like this:



objectA.objectB = b {};


And this is unacceptable in aggregation pattern where a parent object initializes the nested object.



Other two approaches I read about:




  • Instead of aggregation, use multiple protected inheritances and
    translate needed methods with "using". However, I also read in
    "Google C++ Style Guide" to never use protected inheritance..


  • Rewrite all required methods in the parent class manually. But this

    would require for me to write SetSomething() method in both ObjectB

    an objectA which, in my opinion, violates "Don't repeat

    yourself".



What am I missing? How can this be done? Could anyone please shed some light.



Thanks in advance.










share|improve this question









New contributor




dazipe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • provide both const and non-const getters? Or, if you happen to use particular nested method quite often, simply provide it in your most-outer class and implement it by calling the inner-object version?
    – Fureeish
    yesterday










  • Just write getters that return non-const references. It's not uncommon to end up writing 2 getters for some members, one returning a const reference (which should be the default) and one returning a non-const reference (sometimes people suffix the name getFooNC() to differentiate the two)
    – JMAA
    yesterday










  • This is whole point... Providing non const getter provides full access to the object. Not only can I call non const methods but I can also replace the object itself. Ex objectA.objectB = new b {}; In aggregation pattern where parent object initializes nested object with some special properties this should not be allowed.
    – dazipe
    22 hours ago










  • If getObjectB() returns a non-const b& reference, then objectA.getObjectB() = new b {} won't compile. You would, however, be able to write objectA.getObjectB().getObjectC().SetSomething(), Is this not what you want?
    – Igor Tandetnik
    19 hours ago










  • Since objectA.getObjectB() reterns a reference to an object it is an lvalue. The objectA.getObjectB() = b {}; (without new) compiles just fine. It is equivalent to b& tempB = objectA.getObjectB() ; tempB = b {};. And in the end I'm able to replace the ObjectB inside ObjectA skipping any type of internal logic.
    – dazipe
    17 hours ago


















0














I came to from the C# world and, thanks to the "Properties", I can run a setter method on a nested object without leaking any internals like this:



objectA.objectB.objectC.SetSomething();


So far, I found that the best analog in C++ is to use getters and get nested object by value or const reference than use setters to set them by value. But this would result in the following code:



auto b = objectA().getObjectB();
auto c = b.getObjectC();
c.SetSomething();
b.setObjectC(c);
objectA.setObjectB(b);


Not only this is 5 lines instead of one, but it involves multiple copies of the nested objects in b.setObjectC(c); and objectA.setObjectB(b);



I can let the getters to return a non-const reference of a nested object (or use public variables) and will be able to write in C++:



objectA.objectB.objectC.SetSomething();


But, instantly I get the ability to set objectB without objectA knowledge like this:



objectA.objectB = b {};


And this is unacceptable in aggregation pattern where a parent object initializes the nested object.



Other two approaches I read about:




  • Instead of aggregation, use multiple protected inheritances and
    translate needed methods with "using". However, I also read in
    "Google C++ Style Guide" to never use protected inheritance..


  • Rewrite all required methods in the parent class manually. But this

    would require for me to write SetSomething() method in both ObjectB

    an objectA which, in my opinion, violates "Don't repeat

    yourself".



What am I missing? How can this be done? Could anyone please shed some light.



Thanks in advance.










share|improve this question









New contributor




dazipe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • provide both const and non-const getters? Or, if you happen to use particular nested method quite often, simply provide it in your most-outer class and implement it by calling the inner-object version?
    – Fureeish
    yesterday










  • Just write getters that return non-const references. It's not uncommon to end up writing 2 getters for some members, one returning a const reference (which should be the default) and one returning a non-const reference (sometimes people suffix the name getFooNC() to differentiate the two)
    – JMAA
    yesterday










  • This is whole point... Providing non const getter provides full access to the object. Not only can I call non const methods but I can also replace the object itself. Ex objectA.objectB = new b {}; In aggregation pattern where parent object initializes nested object with some special properties this should not be allowed.
    – dazipe
    22 hours ago










  • If getObjectB() returns a non-const b& reference, then objectA.getObjectB() = new b {} won't compile. You would, however, be able to write objectA.getObjectB().getObjectC().SetSomething(), Is this not what you want?
    – Igor Tandetnik
    19 hours ago










  • Since objectA.getObjectB() reterns a reference to an object it is an lvalue. The objectA.getObjectB() = b {}; (without new) compiles just fine. It is equivalent to b& tempB = objectA.getObjectB() ; tempB = b {};. And in the end I'm able to replace the ObjectB inside ObjectA skipping any type of internal logic.
    – dazipe
    17 hours ago
















0












0








0







I came to from the C# world and, thanks to the "Properties", I can run a setter method on a nested object without leaking any internals like this:



objectA.objectB.objectC.SetSomething();


So far, I found that the best analog in C++ is to use getters and get nested object by value or const reference than use setters to set them by value. But this would result in the following code:



auto b = objectA().getObjectB();
auto c = b.getObjectC();
c.SetSomething();
b.setObjectC(c);
objectA.setObjectB(b);


Not only this is 5 lines instead of one, but it involves multiple copies of the nested objects in b.setObjectC(c); and objectA.setObjectB(b);



I can let the getters to return a non-const reference of a nested object (or use public variables) and will be able to write in C++:



objectA.objectB.objectC.SetSomething();


But, instantly I get the ability to set objectB without objectA knowledge like this:



objectA.objectB = b {};


And this is unacceptable in aggregation pattern where a parent object initializes the nested object.



Other two approaches I read about:




  • Instead of aggregation, use multiple protected inheritances and
    translate needed methods with "using". However, I also read in
    "Google C++ Style Guide" to never use protected inheritance..


  • Rewrite all required methods in the parent class manually. But this

    would require for me to write SetSomething() method in both ObjectB

    an objectA which, in my opinion, violates "Don't repeat

    yourself".



What am I missing? How can this be done? Could anyone please shed some light.



Thanks in advance.










share|improve this question









New contributor




dazipe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I came to from the C# world and, thanks to the "Properties", I can run a setter method on a nested object without leaking any internals like this:



objectA.objectB.objectC.SetSomething();


So far, I found that the best analog in C++ is to use getters and get nested object by value or const reference than use setters to set them by value. But this would result in the following code:



auto b = objectA().getObjectB();
auto c = b.getObjectC();
c.SetSomething();
b.setObjectC(c);
objectA.setObjectB(b);


Not only this is 5 lines instead of one, but it involves multiple copies of the nested objects in b.setObjectC(c); and objectA.setObjectB(b);



I can let the getters to return a non-const reference of a nested object (or use public variables) and will be able to write in C++:



objectA.objectB.objectC.SetSomething();


But, instantly I get the ability to set objectB without objectA knowledge like this:



objectA.objectB = b {};


And this is unacceptable in aggregation pattern where a parent object initializes the nested object.



Other two approaches I read about:




  • Instead of aggregation, use multiple protected inheritances and
    translate needed methods with "using". However, I also read in
    "Google C++ Style Guide" to never use protected inheritance..


  • Rewrite all required methods in the parent class manually. But this

    would require for me to write SetSomething() method in both ObjectB

    an objectA which, in my opinion, violates "Don't repeat

    yourself".



What am I missing? How can this be done? Could anyone please shed some light.



Thanks in advance.







c++11






share|improve this question









New contributor




dazipe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




dazipe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 4 hours ago





















New contributor




dazipe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









dazipe

12




12




New contributor




dazipe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





dazipe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






dazipe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • provide both const and non-const getters? Or, if you happen to use particular nested method quite often, simply provide it in your most-outer class and implement it by calling the inner-object version?
    – Fureeish
    yesterday










  • Just write getters that return non-const references. It's not uncommon to end up writing 2 getters for some members, one returning a const reference (which should be the default) and one returning a non-const reference (sometimes people suffix the name getFooNC() to differentiate the two)
    – JMAA
    yesterday










  • This is whole point... Providing non const getter provides full access to the object. Not only can I call non const methods but I can also replace the object itself. Ex objectA.objectB = new b {}; In aggregation pattern where parent object initializes nested object with some special properties this should not be allowed.
    – dazipe
    22 hours ago










  • If getObjectB() returns a non-const b& reference, then objectA.getObjectB() = new b {} won't compile. You would, however, be able to write objectA.getObjectB().getObjectC().SetSomething(), Is this not what you want?
    – Igor Tandetnik
    19 hours ago










  • Since objectA.getObjectB() reterns a reference to an object it is an lvalue. The objectA.getObjectB() = b {}; (without new) compiles just fine. It is equivalent to b& tempB = objectA.getObjectB() ; tempB = b {};. And in the end I'm able to replace the ObjectB inside ObjectA skipping any type of internal logic.
    – dazipe
    17 hours ago




















  • provide both const and non-const getters? Or, if you happen to use particular nested method quite often, simply provide it in your most-outer class and implement it by calling the inner-object version?
    – Fureeish
    yesterday










  • Just write getters that return non-const references. It's not uncommon to end up writing 2 getters for some members, one returning a const reference (which should be the default) and one returning a non-const reference (sometimes people suffix the name getFooNC() to differentiate the two)
    – JMAA
    yesterday










  • This is whole point... Providing non const getter provides full access to the object. Not only can I call non const methods but I can also replace the object itself. Ex objectA.objectB = new b {}; In aggregation pattern where parent object initializes nested object with some special properties this should not be allowed.
    – dazipe
    22 hours ago










  • If getObjectB() returns a non-const b& reference, then objectA.getObjectB() = new b {} won't compile. You would, however, be able to write objectA.getObjectB().getObjectC().SetSomething(), Is this not what you want?
    – Igor Tandetnik
    19 hours ago










  • Since objectA.getObjectB() reterns a reference to an object it is an lvalue. The objectA.getObjectB() = b {}; (without new) compiles just fine. It is equivalent to b& tempB = objectA.getObjectB() ; tempB = b {};. And in the end I'm able to replace the ObjectB inside ObjectA skipping any type of internal logic.
    – dazipe
    17 hours ago


















provide both const and non-const getters? Or, if you happen to use particular nested method quite often, simply provide it in your most-outer class and implement it by calling the inner-object version?
– Fureeish
yesterday




provide both const and non-const getters? Or, if you happen to use particular nested method quite often, simply provide it in your most-outer class and implement it by calling the inner-object version?
– Fureeish
yesterday












Just write getters that return non-const references. It's not uncommon to end up writing 2 getters for some members, one returning a const reference (which should be the default) and one returning a non-const reference (sometimes people suffix the name getFooNC() to differentiate the two)
– JMAA
yesterday




Just write getters that return non-const references. It's not uncommon to end up writing 2 getters for some members, one returning a const reference (which should be the default) and one returning a non-const reference (sometimes people suffix the name getFooNC() to differentiate the two)
– JMAA
yesterday












This is whole point... Providing non const getter provides full access to the object. Not only can I call non const methods but I can also replace the object itself. Ex objectA.objectB = new b {}; In aggregation pattern where parent object initializes nested object with some special properties this should not be allowed.
– dazipe
22 hours ago




This is whole point... Providing non const getter provides full access to the object. Not only can I call non const methods but I can also replace the object itself. Ex objectA.objectB = new b {}; In aggregation pattern where parent object initializes nested object with some special properties this should not be allowed.
– dazipe
22 hours ago












If getObjectB() returns a non-const b& reference, then objectA.getObjectB() = new b {} won't compile. You would, however, be able to write objectA.getObjectB().getObjectC().SetSomething(), Is this not what you want?
– Igor Tandetnik
19 hours ago




If getObjectB() returns a non-const b& reference, then objectA.getObjectB() = new b {} won't compile. You would, however, be able to write objectA.getObjectB().getObjectC().SetSomething(), Is this not what you want?
– Igor Tandetnik
19 hours ago












Since objectA.getObjectB() reterns a reference to an object it is an lvalue. The objectA.getObjectB() = b {}; (without new) compiles just fine. It is equivalent to b& tempB = objectA.getObjectB() ; tempB = b {};. And in the end I'm able to replace the ObjectB inside ObjectA skipping any type of internal logic.
– dazipe
17 hours ago






Since objectA.getObjectB() reterns a reference to an object it is an lvalue. The objectA.getObjectB() = b {}; (without new) compiles just fine. It is equivalent to b& tempB = objectA.getObjectB() ; tempB = b {};. And in the end I'm able to replace the ObjectB inside ObjectA skipping any type of internal logic.
– dazipe
17 hours ago



















active

oldest

votes











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
});


}
});






dazipe is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943066%2fhow-to-run-non-const-methods-on-nested-objects-in-composition-aggregation-patte%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








dazipe is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















dazipe is a new contributor. Be nice, and check out our Code of Conduct.













dazipe is a new contributor. Be nice, and check out our Code of Conduct.












dazipe is a new contributor. Be nice, and check out our Code of Conduct.
















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.





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:


  • 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%2f53943066%2fhow-to-run-non-const-methods-on-nested-objects-in-composition-aggregation-patte%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas