Function's const meaning for the return data type
What is the real const meaning for the 2nd declaration foo:B() ?
int foo::A() const {
return m_var;
}
int const foo::B() {
return m_var;
}
For the 1st declaration I know for sure that it "protects" the member variable ie m_var.
But what the whole meaning of the 2nd declaration, which it just returns a constant int to the caller probably non-constant variable ? I mean does this make sense for any reason ?
c++ function const class-design
|
show 6 more comments
What is the real const meaning for the 2nd declaration foo:B() ?
int foo::A() const {
return m_var;
}
int const foo::B() {
return m_var;
}
For the 1st declaration I know for sure that it "protects" the member variable ie m_var.
But what the whole meaning of the 2nd declaration, which it just returns a constant int to the caller probably non-constant variable ? I mean does this make sense for any reason ?
c++ function const class-design
4
Returning aconst
value makes no sense at all.
– Some programmer dude
Nov 18 '18 at 9:19
1
Not only that, the type of the expressionf.B()
for a made upfoo f;
isint
. So I would say it makes negative sense for fundamentals value types. Pre C++11 there was marginal sense to do it for some user defined types. Now there is none.
– StoryTeller
Nov 18 '18 at 9:22
with or without theconst
,f.B() = 5;
is an error, so its meaningless
– user463035818
Nov 18 '18 at 9:33
that is the wrong dupe. OP has two examples. First is a const method, the question is about the second. Voting to reopen
– user463035818
Nov 18 '18 at 9:44
@user463035818 I seriously disagree. The two dupes perfectly explained what theconst
keyword does for both of the code examples.
– πάντα ῥεῖ
Nov 18 '18 at 9:48
|
show 6 more comments
What is the real const meaning for the 2nd declaration foo:B() ?
int foo::A() const {
return m_var;
}
int const foo::B() {
return m_var;
}
For the 1st declaration I know for sure that it "protects" the member variable ie m_var.
But what the whole meaning of the 2nd declaration, which it just returns a constant int to the caller probably non-constant variable ? I mean does this make sense for any reason ?
c++ function const class-design
What is the real const meaning for the 2nd declaration foo:B() ?
int foo::A() const {
return m_var;
}
int const foo::B() {
return m_var;
}
For the 1st declaration I know for sure that it "protects" the member variable ie m_var.
But what the whole meaning of the 2nd declaration, which it just returns a constant int to the caller probably non-constant variable ? I mean does this make sense for any reason ?
c++ function const class-design
c++ function const class-design
edited Nov 18 '18 at 11:39
Christophe
39.3k43576
39.3k43576
asked Nov 18 '18 at 9:16
MaverickMaverick
396115
396115
4
Returning aconst
value makes no sense at all.
– Some programmer dude
Nov 18 '18 at 9:19
1
Not only that, the type of the expressionf.B()
for a made upfoo f;
isint
. So I would say it makes negative sense for fundamentals value types. Pre C++11 there was marginal sense to do it for some user defined types. Now there is none.
– StoryTeller
Nov 18 '18 at 9:22
with or without theconst
,f.B() = 5;
is an error, so its meaningless
– user463035818
Nov 18 '18 at 9:33
that is the wrong dupe. OP has two examples. First is a const method, the question is about the second. Voting to reopen
– user463035818
Nov 18 '18 at 9:44
@user463035818 I seriously disagree. The two dupes perfectly explained what theconst
keyword does for both of the code examples.
– πάντα ῥεῖ
Nov 18 '18 at 9:48
|
show 6 more comments
4
Returning aconst
value makes no sense at all.
– Some programmer dude
Nov 18 '18 at 9:19
1
Not only that, the type of the expressionf.B()
for a made upfoo f;
isint
. So I would say it makes negative sense for fundamentals value types. Pre C++11 there was marginal sense to do it for some user defined types. Now there is none.
– StoryTeller
Nov 18 '18 at 9:22
with or without theconst
,f.B() = 5;
is an error, so its meaningless
– user463035818
Nov 18 '18 at 9:33
that is the wrong dupe. OP has two examples. First is a const method, the question is about the second. Voting to reopen
– user463035818
Nov 18 '18 at 9:44
@user463035818 I seriously disagree. The two dupes perfectly explained what theconst
keyword does for both of the code examples.
– πάντα ῥεῖ
Nov 18 '18 at 9:48
4
4
Returning a
const
value makes no sense at all.– Some programmer dude
Nov 18 '18 at 9:19
Returning a
const
value makes no sense at all.– Some programmer dude
Nov 18 '18 at 9:19
1
1
Not only that, the type of the expression
f.B()
for a made up foo f;
is int
. So I would say it makes negative sense for fundamentals value types. Pre C++11 there was marginal sense to do it for some user defined types. Now there is none.– StoryTeller
Nov 18 '18 at 9:22
Not only that, the type of the expression
f.B()
for a made up foo f;
is int
. So I would say it makes negative sense for fundamentals value types. Pre C++11 there was marginal sense to do it for some user defined types. Now there is none.– StoryTeller
Nov 18 '18 at 9:22
with or without the
const
, f.B() = 5;
is an error, so its meaningless– user463035818
Nov 18 '18 at 9:33
with or without the
const
, f.B() = 5;
is an error, so its meaningless– user463035818
Nov 18 '18 at 9:33
that is the wrong dupe. OP has two examples. First is a const method, the question is about the second. Voting to reopen
– user463035818
Nov 18 '18 at 9:44
that is the wrong dupe. OP has two examples. First is a const method, the question is about the second. Voting to reopen
– user463035818
Nov 18 '18 at 9:44
@user463035818 I seriously disagree. The two dupes perfectly explained what the
const
keyword does for both of the code examples.– πάντα ῥεῖ
Nov 18 '18 at 9:48
@user463035818 I seriously disagree. The two dupes perfectly explained what the
const
keyword does for both of the code examples.– πάντα ῥεῖ
Nov 18 '18 at 9:48
|
show 6 more comments
1 Answer
1
active
oldest
votes
Case 1: The const
after the function signature says that the function will not change the object. So you can use it on const
objects (or with pointer to const
or a const
reference).
Case 2: The const
before the function name is indeed about the return type. You are completely right: in practice it doesn't change anything for the object, since the return is in this snippet done by value, and this value in a temp that cannot be changed (e.g. a ++
or a --
would not be valid anyway because there's no lvalue).
Case 3: The const in the return type would make more sense with the return of a pointer to const or a const reference. In this case it would prevent the object state to be changed from outside.
Here a summary:
class foo {
public:
int A() const { // const function
return m_var;
}
int const B() { // non const function, but const return type
return m_var;
}
int const& C() const { // non const function, but const reference return type
return m_var;
}
private:
int m_var;
};
int main() {
const foo x{};
x.A(); // ok
//x.B(); // not ok -> function B() doesn't guarantee to leave x unchanged.
x.C(); // ok
const int& y = x.C(); // ok (y will not alter m_var.
//int& z = x.C(); // not ok since z is not const
return 0;
}
online demo
Case 4: (thanks to HolyBlackCat for pointing it out). What make no difference for scalars in case 2 could perfectly make sense for classes. Suppose m_var
would be of class bar
:
class bar {
public:
void change_it() {}
void read_it() const {}
};
Then the const return value would make a difference:
foo u{};
u.B(); // ok
u.B().read_it(); // ok
u.B().change_it(); // not ok because of constness of B().
online demo
If I remember correctly,const
indeed has no effect when returning arithmetic types. But that's different for classes (e.g. const class instances can't be meaningfully moved from). Though I don't remember the details.
– HolyBlackCat
Nov 18 '18 at 10:45
@HolyBlackCat Thanks for pointing it out ! Indeed, it would make sense for a class. I've edited my answer and gave you credit for this additional case :-)
– Christophe
Nov 18 '18 at 11:08
Maybe it's worth mentioning that const class return values preventsmove
ing from them, so their usage is discouraged.
– geza
Nov 18 '18 at 11:17
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%2f53359378%2ffunctions-const-meaning-for-the-return-data-type%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
Case 1: The const
after the function signature says that the function will not change the object. So you can use it on const
objects (or with pointer to const
or a const
reference).
Case 2: The const
before the function name is indeed about the return type. You are completely right: in practice it doesn't change anything for the object, since the return is in this snippet done by value, and this value in a temp that cannot be changed (e.g. a ++
or a --
would not be valid anyway because there's no lvalue).
Case 3: The const in the return type would make more sense with the return of a pointer to const or a const reference. In this case it would prevent the object state to be changed from outside.
Here a summary:
class foo {
public:
int A() const { // const function
return m_var;
}
int const B() { // non const function, but const return type
return m_var;
}
int const& C() const { // non const function, but const reference return type
return m_var;
}
private:
int m_var;
};
int main() {
const foo x{};
x.A(); // ok
//x.B(); // not ok -> function B() doesn't guarantee to leave x unchanged.
x.C(); // ok
const int& y = x.C(); // ok (y will not alter m_var.
//int& z = x.C(); // not ok since z is not const
return 0;
}
online demo
Case 4: (thanks to HolyBlackCat for pointing it out). What make no difference for scalars in case 2 could perfectly make sense for classes. Suppose m_var
would be of class bar
:
class bar {
public:
void change_it() {}
void read_it() const {}
};
Then the const return value would make a difference:
foo u{};
u.B(); // ok
u.B().read_it(); // ok
u.B().change_it(); // not ok because of constness of B().
online demo
If I remember correctly,const
indeed has no effect when returning arithmetic types. But that's different for classes (e.g. const class instances can't be meaningfully moved from). Though I don't remember the details.
– HolyBlackCat
Nov 18 '18 at 10:45
@HolyBlackCat Thanks for pointing it out ! Indeed, it would make sense for a class. I've edited my answer and gave you credit for this additional case :-)
– Christophe
Nov 18 '18 at 11:08
Maybe it's worth mentioning that const class return values preventsmove
ing from them, so their usage is discouraged.
– geza
Nov 18 '18 at 11:17
add a comment |
Case 1: The const
after the function signature says that the function will not change the object. So you can use it on const
objects (or with pointer to const
or a const
reference).
Case 2: The const
before the function name is indeed about the return type. You are completely right: in practice it doesn't change anything for the object, since the return is in this snippet done by value, and this value in a temp that cannot be changed (e.g. a ++
or a --
would not be valid anyway because there's no lvalue).
Case 3: The const in the return type would make more sense with the return of a pointer to const or a const reference. In this case it would prevent the object state to be changed from outside.
Here a summary:
class foo {
public:
int A() const { // const function
return m_var;
}
int const B() { // non const function, but const return type
return m_var;
}
int const& C() const { // non const function, but const reference return type
return m_var;
}
private:
int m_var;
};
int main() {
const foo x{};
x.A(); // ok
//x.B(); // not ok -> function B() doesn't guarantee to leave x unchanged.
x.C(); // ok
const int& y = x.C(); // ok (y will not alter m_var.
//int& z = x.C(); // not ok since z is not const
return 0;
}
online demo
Case 4: (thanks to HolyBlackCat for pointing it out). What make no difference for scalars in case 2 could perfectly make sense for classes. Suppose m_var
would be of class bar
:
class bar {
public:
void change_it() {}
void read_it() const {}
};
Then the const return value would make a difference:
foo u{};
u.B(); // ok
u.B().read_it(); // ok
u.B().change_it(); // not ok because of constness of B().
online demo
If I remember correctly,const
indeed has no effect when returning arithmetic types. But that's different for classes (e.g. const class instances can't be meaningfully moved from). Though I don't remember the details.
– HolyBlackCat
Nov 18 '18 at 10:45
@HolyBlackCat Thanks for pointing it out ! Indeed, it would make sense for a class. I've edited my answer and gave you credit for this additional case :-)
– Christophe
Nov 18 '18 at 11:08
Maybe it's worth mentioning that const class return values preventsmove
ing from them, so their usage is discouraged.
– geza
Nov 18 '18 at 11:17
add a comment |
Case 1: The const
after the function signature says that the function will not change the object. So you can use it on const
objects (or with pointer to const
or a const
reference).
Case 2: The const
before the function name is indeed about the return type. You are completely right: in practice it doesn't change anything for the object, since the return is in this snippet done by value, and this value in a temp that cannot be changed (e.g. a ++
or a --
would not be valid anyway because there's no lvalue).
Case 3: The const in the return type would make more sense with the return of a pointer to const or a const reference. In this case it would prevent the object state to be changed from outside.
Here a summary:
class foo {
public:
int A() const { // const function
return m_var;
}
int const B() { // non const function, but const return type
return m_var;
}
int const& C() const { // non const function, but const reference return type
return m_var;
}
private:
int m_var;
};
int main() {
const foo x{};
x.A(); // ok
//x.B(); // not ok -> function B() doesn't guarantee to leave x unchanged.
x.C(); // ok
const int& y = x.C(); // ok (y will not alter m_var.
//int& z = x.C(); // not ok since z is not const
return 0;
}
online demo
Case 4: (thanks to HolyBlackCat for pointing it out). What make no difference for scalars in case 2 could perfectly make sense for classes. Suppose m_var
would be of class bar
:
class bar {
public:
void change_it() {}
void read_it() const {}
};
Then the const return value would make a difference:
foo u{};
u.B(); // ok
u.B().read_it(); // ok
u.B().change_it(); // not ok because of constness of B().
online demo
Case 1: The const
after the function signature says that the function will not change the object. So you can use it on const
objects (or with pointer to const
or a const
reference).
Case 2: The const
before the function name is indeed about the return type. You are completely right: in practice it doesn't change anything for the object, since the return is in this snippet done by value, and this value in a temp that cannot be changed (e.g. a ++
or a --
would not be valid anyway because there's no lvalue).
Case 3: The const in the return type would make more sense with the return of a pointer to const or a const reference. In this case it would prevent the object state to be changed from outside.
Here a summary:
class foo {
public:
int A() const { // const function
return m_var;
}
int const B() { // non const function, but const return type
return m_var;
}
int const& C() const { // non const function, but const reference return type
return m_var;
}
private:
int m_var;
};
int main() {
const foo x{};
x.A(); // ok
//x.B(); // not ok -> function B() doesn't guarantee to leave x unchanged.
x.C(); // ok
const int& y = x.C(); // ok (y will not alter m_var.
//int& z = x.C(); // not ok since z is not const
return 0;
}
online demo
Case 4: (thanks to HolyBlackCat for pointing it out). What make no difference for scalars in case 2 could perfectly make sense for classes. Suppose m_var
would be of class bar
:
class bar {
public:
void change_it() {}
void read_it() const {}
};
Then the const return value would make a difference:
foo u{};
u.B(); // ok
u.B().read_it(); // ok
u.B().change_it(); // not ok because of constness of B().
online demo
edited Nov 18 '18 at 11:06
answered Nov 18 '18 at 10:39
ChristopheChristophe
39.3k43576
39.3k43576
If I remember correctly,const
indeed has no effect when returning arithmetic types. But that's different for classes (e.g. const class instances can't be meaningfully moved from). Though I don't remember the details.
– HolyBlackCat
Nov 18 '18 at 10:45
@HolyBlackCat Thanks for pointing it out ! Indeed, it would make sense for a class. I've edited my answer and gave you credit for this additional case :-)
– Christophe
Nov 18 '18 at 11:08
Maybe it's worth mentioning that const class return values preventsmove
ing from them, so their usage is discouraged.
– geza
Nov 18 '18 at 11:17
add a comment |
If I remember correctly,const
indeed has no effect when returning arithmetic types. But that's different for classes (e.g. const class instances can't be meaningfully moved from). Though I don't remember the details.
– HolyBlackCat
Nov 18 '18 at 10:45
@HolyBlackCat Thanks for pointing it out ! Indeed, it would make sense for a class. I've edited my answer and gave you credit for this additional case :-)
– Christophe
Nov 18 '18 at 11:08
Maybe it's worth mentioning that const class return values preventsmove
ing from them, so their usage is discouraged.
– geza
Nov 18 '18 at 11:17
If I remember correctly,
const
indeed has no effect when returning arithmetic types. But that's different for classes (e.g. const class instances can't be meaningfully moved from). Though I don't remember the details.– HolyBlackCat
Nov 18 '18 at 10:45
If I remember correctly,
const
indeed has no effect when returning arithmetic types. But that's different for classes (e.g. const class instances can't be meaningfully moved from). Though I don't remember the details.– HolyBlackCat
Nov 18 '18 at 10:45
@HolyBlackCat Thanks for pointing it out ! Indeed, it would make sense for a class. I've edited my answer and gave you credit for this additional case :-)
– Christophe
Nov 18 '18 at 11:08
@HolyBlackCat Thanks for pointing it out ! Indeed, it would make sense for a class. I've edited my answer and gave you credit for this additional case :-)
– Christophe
Nov 18 '18 at 11:08
Maybe it's worth mentioning that const class return values prevents
move
ing from them, so their usage is discouraged.– geza
Nov 18 '18 at 11:17
Maybe it's worth mentioning that const class return values prevents
move
ing from them, so their usage is discouraged.– geza
Nov 18 '18 at 11:17
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%2f53359378%2ffunctions-const-meaning-for-the-return-data-type%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
4
Returning a
const
value makes no sense at all.– Some programmer dude
Nov 18 '18 at 9:19
1
Not only that, the type of the expression
f.B()
for a made upfoo f;
isint
. So I would say it makes negative sense for fundamentals value types. Pre C++11 there was marginal sense to do it for some user defined types. Now there is none.– StoryTeller
Nov 18 '18 at 9:22
with or without the
const
,f.B() = 5;
is an error, so its meaningless– user463035818
Nov 18 '18 at 9:33
that is the wrong dupe. OP has two examples. First is a const method, the question is about the second. Voting to reopen
– user463035818
Nov 18 '18 at 9:44
@user463035818 I seriously disagree. The two dupes perfectly explained what the
const
keyword does for both of the code examples.– πάντα ῥεῖ
Nov 18 '18 at 9:48