Developer forums (C::B DEVELOPMENT STRICTLY!) > Development

wxString const& or const wxString&

(1/2) > >>

Martin K.:
Hi,

I know that this is not really a Code::Blocks related question, but maybe it is some type of Code::Blocks coding standard:

Given is a function that accepts a reference to a wxString. This function will never change this string. What is your prefered const handling?

function(const wxString& str)
{
}

or

function(wxString const& str)
{
}


I can found the first and the second form in Code::Blocks source, as far as i can see the second form in newer code.
GCC seems to handle all forms as the same, a str += _T("test") will fail.
In my opinion the 2. form should be the right form (reference to constant wxString), what do you mean about this?

Martin

P.S. GCC don't like (const wxString const & str)

carra:
They don't mean the same:

const wxString& str
This is a reference to a constant string

wxString const& str
This is a constant reference to a string. The string can still be changed, just not the reference to it.

In general you will want the first alternative.

oBFusCATed:
A const &a and const A &a are the same.
In C::B the standard form is the latter - const A &a.
If you see the former, probably it is added by me because I like it more and I've forgotten to obey to the standard.

See here for the coding style guide: http://wiki.codeblocks.org/index.php?title=Coding_style

@carra: You are in deep misunderstanding. wxstring & const str is const reference to the string, but this is invalid C++ see here: http://www.parashift.com/c++-faq/const-ref-nonsense.html
Some const correctness FAQs can be found here: http://www.parashift.com/c++-faq/const-correctness.html

This same question discussed in stackoverflow: http://stackoverflow.com/questions/3694630/c-const-reference-before-vs-after-type-specifier

Martin K.:
again what learned  ;)

further investigations tell me that a reference itself is const by design, it can't be changed. So "wxString const&" is redundant. "const wxString&" is the right way, although gcc seems to handel them equally.

Thank you.

carra:
You were actually right, obfuscated. What I said had no sense because in fact, once created, C++ references cannot be repointed to another variable! If you try to change it, it performs assignment on the pointed variable instead.

I just wrote a small program to show the differences.
This should give compile errors in the last 2 appendings:


--- Code: ---#include <iostream>     // [ C++ STL ] I/O Streams
#include <string>       // [ C++ STL ] Strings

using namespace std;

int main()
{
    string str = "string";
    string another;
   
    string& const StringRefConst = str;
    string const& StringConstRef = str;
    const string& ConstStringRef = str;
   
    // attempts to change the string
    StringRefConst += " changed from StringRefConst";   // allowed
    StringConstRef += " changed from StringConstRef";   // error
    ConstStringRef += " changed from ConstStringRef";   // error
   
    return 0;
}

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version