const access and non-const access
I have a class which internally owns a vector of foo
class bar {
private:
vector<Foo> foos_;
}
Now I want to design public access to this vector. I am thinking of two versions of the function:
Foo& getFoo(int index) {
// first do size checking, return ref
return foos[index];
}
and
const Foo& getFoo(int index) const {
// first do size checking, return const reference
return foos[index];
}
Any downside of this approach? One obvious downside is I copy the almost identical code simply twice. Is there a better way to do this?
----- edit -----
the second accessor forgets const, updated
c++ design-patterns
|
show 9 more comments
I have a class which internally owns a vector of foo
class bar {
private:
vector<Foo> foos_;
}
Now I want to design public access to this vector. I am thinking of two versions of the function:
Foo& getFoo(int index) {
// first do size checking, return ref
return foos[index];
}
and
const Foo& getFoo(int index) const {
// first do size checking, return const reference
return foos[index];
}
Any downside of this approach? One obvious downside is I copy the almost identical code simply twice. Is there a better way to do this?
----- edit -----
the second accessor forgets const, updated
c++ design-patterns
2
why not call the non-const function from within the const one?
– dave
Nov 24 '18 at 1:46
2
Because you can't call a non-const method from a const method. C++ does not work this way.
– Sam Varshavchik
Nov 24 '18 at 1:49
Avoid the situation. You have a design flaw.
– Ted Lyngmo
Nov 24 '18 at 1:57
A common technique goes like this:Foo& getFoo(int index) { const bar* constThis = this; return const_cast<Foo&>(constThis->getFoo(index)); }
– Igor Tandetnik
Nov 24 '18 at 1:58
3
@TedLyngmo Then so doesstd::vector. Itsoperatoruses the same approach.
– Igor Tandetnik
Nov 24 '18 at 1:59
|
show 9 more comments
I have a class which internally owns a vector of foo
class bar {
private:
vector<Foo> foos_;
}
Now I want to design public access to this vector. I am thinking of two versions of the function:
Foo& getFoo(int index) {
// first do size checking, return ref
return foos[index];
}
and
const Foo& getFoo(int index) const {
// first do size checking, return const reference
return foos[index];
}
Any downside of this approach? One obvious downside is I copy the almost identical code simply twice. Is there a better way to do this?
----- edit -----
the second accessor forgets const, updated
c++ design-patterns
I have a class which internally owns a vector of foo
class bar {
private:
vector<Foo> foos_;
}
Now I want to design public access to this vector. I am thinking of two versions of the function:
Foo& getFoo(int index) {
// first do size checking, return ref
return foos[index];
}
and
const Foo& getFoo(int index) const {
// first do size checking, return const reference
return foos[index];
}
Any downside of this approach? One obvious downside is I copy the almost identical code simply twice. Is there a better way to do this?
----- edit -----
the second accessor forgets const, updated
c++ design-patterns
c++ design-patterns
edited Nov 24 '18 at 2:03
WhatABeautifulWorld
asked Nov 24 '18 at 1:43
WhatABeautifulWorldWhatABeautifulWorld
89211123
89211123
2
why not call the non-const function from within the const one?
– dave
Nov 24 '18 at 1:46
2
Because you can't call a non-const method from a const method. C++ does not work this way.
– Sam Varshavchik
Nov 24 '18 at 1:49
Avoid the situation. You have a design flaw.
– Ted Lyngmo
Nov 24 '18 at 1:57
A common technique goes like this:Foo& getFoo(int index) { const bar* constThis = this; return const_cast<Foo&>(constThis->getFoo(index)); }
– Igor Tandetnik
Nov 24 '18 at 1:58
3
@TedLyngmo Then so doesstd::vector. Itsoperatoruses the same approach.
– Igor Tandetnik
Nov 24 '18 at 1:59
|
show 9 more comments
2
why not call the non-const function from within the const one?
– dave
Nov 24 '18 at 1:46
2
Because you can't call a non-const method from a const method. C++ does not work this way.
– Sam Varshavchik
Nov 24 '18 at 1:49
Avoid the situation. You have a design flaw.
– Ted Lyngmo
Nov 24 '18 at 1:57
A common technique goes like this:Foo& getFoo(int index) { const bar* constThis = this; return const_cast<Foo&>(constThis->getFoo(index)); }
– Igor Tandetnik
Nov 24 '18 at 1:58
3
@TedLyngmo Then so doesstd::vector. Itsoperatoruses the same approach.
– Igor Tandetnik
Nov 24 '18 at 1:59
2
2
why not call the non-const function from within the const one?
– dave
Nov 24 '18 at 1:46
why not call the non-const function from within the const one?
– dave
Nov 24 '18 at 1:46
2
2
Because you can't call a non-const method from a const method. C++ does not work this way.
– Sam Varshavchik
Nov 24 '18 at 1:49
Because you can't call a non-const method from a const method. C++ does not work this way.
– Sam Varshavchik
Nov 24 '18 at 1:49
Avoid the situation. You have a design flaw.
– Ted Lyngmo
Nov 24 '18 at 1:57
Avoid the situation. You have a design flaw.
– Ted Lyngmo
Nov 24 '18 at 1:57
A common technique goes like this:
Foo& getFoo(int index) { const bar* constThis = this; return const_cast<Foo&>(constThis->getFoo(index)); }– Igor Tandetnik
Nov 24 '18 at 1:58
A common technique goes like this:
Foo& getFoo(int index) { const bar* constThis = this; return const_cast<Foo&>(constThis->getFoo(index)); }– Igor Tandetnik
Nov 24 '18 at 1:58
3
3
@TedLyngmo Then so does
std::vector. Its operator uses the same approach.– Igor Tandetnik
Nov 24 '18 at 1:59
@TedLyngmo Then so does
std::vector. Its operator uses the same approach.– Igor Tandetnik
Nov 24 '18 at 1:59
|
show 9 more comments
1 Answer
1
active
oldest
votes
Having both const and non-const accessors is somewhat common in C++. There is no language feature to combine the code for both--you really do need to write it twice.
By the way, you don't need to do bounds checking yourself, you can use foos_.at(index) instead of foos_[index] and then you'll have automatic bounds checking.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454489%2fconst-access-and-non-const-access%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Having both const and non-const accessors is somewhat common in C++. There is no language feature to combine the code for both--you really do need to write it twice.
By the way, you don't need to do bounds checking yourself, you can use foos_.at(index) instead of foos_[index] and then you'll have automatic bounds checking.
add a comment |
Having both const and non-const accessors is somewhat common in C++. There is no language feature to combine the code for both--you really do need to write it twice.
By the way, you don't need to do bounds checking yourself, you can use foos_.at(index) instead of foos_[index] and then you'll have automatic bounds checking.
add a comment |
Having both const and non-const accessors is somewhat common in C++. There is no language feature to combine the code for both--you really do need to write it twice.
By the way, you don't need to do bounds checking yourself, you can use foos_.at(index) instead of foos_[index] and then you'll have automatic bounds checking.
Having both const and non-const accessors is somewhat common in C++. There is no language feature to combine the code for both--you really do need to write it twice.
By the way, you don't need to do bounds checking yourself, you can use foos_.at(index) instead of foos_[index] and then you'll have automatic bounds checking.
answered Nov 24 '18 at 3:53
John ZwinckJohn Zwinck
153k17177294
153k17177294
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454489%2fconst-access-and-non-const-access%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
why not call the non-const function from within the const one?
– dave
Nov 24 '18 at 1:46
2
Because you can't call a non-const method from a const method. C++ does not work this way.
– Sam Varshavchik
Nov 24 '18 at 1:49
Avoid the situation. You have a design flaw.
– Ted Lyngmo
Nov 24 '18 at 1:57
A common technique goes like this:
Foo& getFoo(int index) { const bar* constThis = this; return const_cast<Foo&>(constThis->getFoo(index)); }– Igor Tandetnik
Nov 24 '18 at 1:58
3
@TedLyngmo Then so does
std::vector. Itsoperatoruses the same approach.– Igor Tandetnik
Nov 24 '18 at 1:59