 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Marlene Miller Guest
|
Posted: Wed Nov 17, 2004 11:57 pm Post subject: const C g(); // const? |
|
|
class C {
int x;
public:
C() : x(0) {}
void f(int);
};
const C g(); // const?
Is it reasonable to say, as a general guideline, a non-member function that
returns a class type should *not* be declared *const* if the class has
non-const member functions?
Why? So that we can say g().f();
(This is a thought I had after reading a piece of Effective C++ Intro and
Item 21.)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Nov 18, 2004 12:25 pm Post subject: Re: const C g(); // const? |
|
|
Marlene Miller wrote:
| Quote: | class C {
int x;
public:
C() : x(0) {}
void f(int);
};
const C g(); // const?
Is it reasonable to say, as a general guideline, a non-member function that
returns a class type should *not* be declared *const* if the class has
non-const member functions?
Why? So that we can say g().f();
(This is a thought I had after reading a piece of Effective C++ Intro and
Item 21.)
|
The question is, what is achieved by calling 'f' on a temporary object?
If 'f', being a non-const function, modifies the object, the modifications
are effectively lost when the temporary is destroyed (at the semicolon).
So, there is no sense in calling 'f' in the first place.
If 'f' doesn't modify the object, it should have been declared 'const' in
the first place, but then for a non-const temporary object it won't matter
anyway because a const function can still be called for both const and
non-const objects.
So, I don't really see any point of arguing whether a temporary object
returned from a function should be const or non-const. In any case, there
can be no general conclusion. It has be decided based on individual
requirements. IMHO.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Fri Nov 19, 2004 12:52 pm Post subject: Re: const C g(); // const? |
|
|
Hi,
| Quote: | class C {
int x;
public:
C() : x(0) {}
void f(int);
};
const C g(); // const?
|
I don't understand this, sorry. As g() is not a member function, adding
const as indicated would just trigger a compiler error since the above
syntax only applies to member functions - and g() isn't.
| Quote: | Is it reasonable to say, as a general guideline, a non-member function that
returns a class type should *not* be declared *const* if the class has
non-const member functions?
|
No. Simply because it's invalid C++. (-;
| Quote: | Why? So that we can say g().f();
|
For that to work, either the return type of g shouldn't be const, or
the member function f should be const.
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Marlene Miller Guest
|
Posted: Fri Nov 19, 2004 3:32 pm Post subject: Re: const C g(); // const? |
|
|
Hello Victor,
Thank you very much for taking the time to consider my question and to
write
a response. Your explanation is very clear and complete and makes
sense. I
understand.
I see my mistake. In Java they say g().f() all the time, but g() is a
reference to an object. I've got to be alert for those C++ temporary
variables and returning a copy of an object. Thank you for your help.
g().f() might be useful if f() somehow caused the object ("this") to
become
bound to some pointer. That's not worthy of a general guideline. Just a
special case.
Regards, Marlene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Fri Nov 19, 2004 4:52 pm Post subject: Re: const C g(); // const? |
|
|
Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote
| Quote: | Marlene Miller wrote:
class C {
int x;
public:
C() : x(0) {}
void f(int);
};
const C g(); // const?
Is it reasonable to say, as a general guideline, a non-member
function that returns a class type should *not* be declared *const*
if the class has non-const member functions?
Why? So that we can say g().f();
(This is a thought I had after reading a piece of Effective C++
Intro and Item 21.)
The question is, what is achieved by calling 'f' on a temporary object?
|
That depends. If f has side effects, it could be useful. I've got a
lot of examples in my own code where I call non-const functions on
temporary objects.
In the end, I think it depends on the type of the object. For most
types, it doesn't make any sense, and having g() return a const type may
catch a few errors. For some types, however, I'd say that their very
raison d'être is to be temporaries on which non-const member functions
are called.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Marlene Miller Guest
|
Posted: Sat Nov 20, 2004 10:18 am Post subject: Re: const C g(); // const? |
|
|
"Thomas Richter" <thor (AT) cleopatra (DOT) math.tu-berlin.de> wrote
| Quote: | Hi,
class C {
int x;
public:
C() : x(0) {}
void f(int);
};
const C g(); // const?
I don't understand this, sorry. As g() is not a member function, adding
const as indicated would just trigger a compiler error since the above
syntax only applies to member functions - and g() isn't.
|
I tried compiling this code using the Borland 5.5 compiler. It's okay.
My guess is you are thinking about const member functions, such as void
C::f(int) const {}. I am considering a non-member function that returns a
constant value. const C g() returns an const object of type C.
Re: const objects: "Except that any class member declared mutable can be
modified, any attempt to modify a const object during its lifetime results
in undefined behavior." 7.1.5.1/4
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sat Nov 20, 2004 10:27 am Post subject: Re: const C g(); // const? |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: | Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:<ZIRmd.11089$Ae.4885 (AT) newsread1 (DOT) dllstx09.us.to.verio.net>...
Marlene Miller wrote:
class C {
int x;
public:
C() : x(0) {}
void f(int);
};
const C g(); // const?
Is it reasonable to say, as a general guideline, a non-member
function that returns a class type should *not* be declared *const*
if the class has non-const member functions?
Why? So that we can say g().f();
(This is a thought I had after reading a piece of Effective C++
Intro and Item 21.)
The question is, what is achieved by calling 'f' on a temporary object?
That depends. If f has side effects, it could be useful. I've got a
lot of examples in my own code where I call non-const functions on
temporary objects.
|
James, you make it sound that I was doubting needing to call a member
function for a temporary _in general_. I wasn't. My statement starting
with "The question is, ..." is simply the assertion that one should always
look at the functionality of 'f'. So, yes, "it depends". And I think,
judging from Marlene's reply to my post, it's not so unclear after all.
| Quote: | In the end, I think it depends on the type of the object. For most
types, it doesn't make any sense, and having g() return a const type may
catch a few errors. For some types, however, I'd say that their very
raison d'être is to be temporaries on which non-const member functions
are called.
|
Sure. I am not even certain why I'm replying except to say that I agree.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jakob Bieling Guest
|
Posted: Sat Nov 20, 2004 4:21 pm Post subject: Re: const C g(); // const? |
|
|
"Thomas Richter" <thor (AT) cleopatra (DOT) math.tu-berlin.de> wrote
| Quote: | Hi,
class C {
int x;
public:
C() : x(0) {}
void f(int);
};
const C g(); // const?
I don't understand this, sorry. As g() is not a member function, adding
const as indicated would just trigger a compiler error since the above
syntax only applies to member functions - and g() isn't.
|
You are confusing "const C g ();" and "C g () const;". The former
declares a function that returns an object of type C, that is const. The
latter declares a const function (implying this function is a member of a
class), that returns an object of type C, that is not const.
The (constness of the) return type has nothing to do with the function
being global or a member of a class.
regards
--
jb
(reply address in rot13, unscramble first)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Frey Guest
|
Posted: Sat Nov 20, 2004 6:22 pm Post subject: Re: const C g(); // const? |
|
|
Victor Bazarov wrote:
| Quote: | Marlene Miller wrote:
class C {
int x;
public:
C() : x(0) {}
void f(int);
};
const C g(); // const?
Is it reasonable to say, as a general guideline, a non-member function that
returns a class type should *not* be declared *const* if the class has
non-const member functions?
Why? So that we can say g().f();
(This is a thought I had after reading a piece of Effective C++ Intro and
Item 21.)
The question is, what is achieved by calling 'f' on a temporary object?
If 'f', being a non-const function, modifies the object, the modifications
are effectively lost when the temporary is destroyed (at the semicolon).
So, there is no sense in calling 'f' in the first place.
|
I disagree. That shouldn't be up to g()'s author to decide. The user has
to be aware of temporary objects anyway. If a function returns a
temporary object, it should IMHO give the user the freedom to use this
object in any way he wants. An example with a hypothetic string class
which has a modifying toUpper()-member-function:
class String
{
public:
...
String& toUpper() /*non-const*/
{
...
return *this;
}
};
String getName();
std::cout << getName().toUpper() << std::endl;
The last line wouldn't work if the author of getName() decides to return
a 'const String' to protect the temporary. By why? The above is IMHO a
reasonable example that happens in the real world quite often.
In fact, I'd tend to say the temporary is irrelevant to Marlene's
question. There are reasons to make a returned object const, but in my
experience, these reasons are rare. My advice would thus be to *not*
return a const object in general - only if you can identify a good
reason to do so. Freedom instead of nannyism - isn't that what C++ is
all about? ;)
Regards, Daniel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Mon Nov 22, 2004 11:08 am Post subject: Re: const C g(); // const? |
|
|
Hi,
| Quote: | const C g(); // const?
I don't understand this, sorry. As g() is not a member function, adding
const as indicated would just trigger a compiler error since the above
syntax only applies to member functions - and g() isn't.
You are confusing "const C g ();" and "C g () const;".
|
Well, I was actually mis-reading the question as:
const C g() const;
which is in fact - nonsense. (-;
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|