我真的很佩服java功能,我不想放弃使用它来解决下一个问题:
我有一个可能被继承的类,并且它内部是一个private ArrayList arr;所以setter函数是可以的,但是getter函数return arr;返回对该变量的引用,任何人都能够编辑我不想要的整个数组并且私有不会'没有任何意义!
在C++中,我只是return const arr;会返回对变量的常量引用.
我非常需要变量不被克隆或手动复制,因为有太多的计算需要(只读变量)为什么在java中没有返回const?有什么方法可以逃避复制吗?
ps (final ArrayList<Integer> arr;)不是一个选项,因为该数组总是更改大小或元素值.
如果我找不到解决方法,我威胁要回到C++或公开所有内容,你永远不应该得到我的软件:D
编辑:一个更重要的问题:我要求的东西不好(软件工程明智)我的意思是如果JAVA创建者认为没有const引用(返回只读引用)那么我必须要求可以在其他地方处理的东西办法.或者我的程序设计错了我很困惑.
C++ 03中的第12.2.5节说" 在构造函数的ctor-initializer(12.6.2)中绑定到引用成员的临时绑定一直存在,直到构造函数退出 "
所以我尝试了以下程序
#include<iostream>
using namespace std;
struct foo
{
foo()
{
cout<<"foo c'tor"<<endl;
}
~foo()
{
cout<<"foo d'tor"<<endl;
}
};
struct bar
{
const foo &ref;
bar():ref(foo())
{
cout<<"bar c'tor"<<endl;
}
};
int main()
{
bar obj;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
foo c'tor
foo d'tor
bar c'tor
Run Code Online (Sandbox Code Playgroud)
现在根据标准,在条形码c'tor的c'tor init-list中由foo()生成的临时性将在bar'c'tor之后被销毁,所以foo d'tor应该在之后打印,bar c'tor
但它是相反的.
请解释原因.
我在anwser中看到过: 通过右值引用返回更有效率吗?
成员函数定义:
Beta_ab const& getAB() const& { return ab; }
Run Code Online (Sandbox Code Playgroud)
我熟悉成员函数的cv-qualifier(const),但不熟悉const&.
最后的const&意思是什么?
内联函数是否需要在性能方面通过const引用传递其参数,如
foo(const T & a, const T &b)
Run Code Online (Sandbox Code Playgroud)
与价值相比
foo(T a, T b)
Run Code Online (Sandbox Code Playgroud)
如果我不改变函数中a和b的值?C++ 11是否会更改此处的具体内容?
我有TestClass一个const&成员变量.我从各个地方和自己的经验中知道const&,通过引用临时值来初始化它是一个坏主意.所以我很惊讶的是,下面的代码将编译罚款(经测试gcc-4.9.1,clang-3.5和scan-build-3.5),但不能正常运行.
class TestClass {
public:
// removing the "reference" would remove the temporary-problem
const std::string &d;
TestClass(const std::string &d)
: d(d) {
// "d" is a const-ref, cannot be changed at all... if it is assigned some
// temporary value it is mangled up...
}
};
int main() {
// NOTE: the variable "d" is a
// temporary, whose reference is not valid... what I don't get in …Run Code Online (Sandbox Code Playgroud) 任何人都可以向我解释为什么这两个陈述之间存在差异?
class A{};
const A& a = A(); // correct
A& b = A(); // wrong
Run Code Online (Sandbox Code Playgroud)
它表示类型A&的临时类型的非const引用的无效初始化A
为什么const这里很重要?
考虑以下代码:
const QString& MyClass::getID(int index) const
{
if (i < myArraySize && myArray[i]) {
return myArray[i]->id; // id is a QString
} else {
return my_global_empty_qstring; // is a global empty QString
}
}
Run Code Online (Sandbox Code Playgroud)
如何在不更改方法的返回类型的情况下避免使用空QString?(似乎返回在堆栈上分配的空QString是一个坏主意)
谢谢.
我foo通过const ref以下方式调用方法:
// method, which is being called
void foo(const Entity & ent);
// call
Entity* e = new Entity;
foo(e); // wrong: missing * but compiles
Run Code Online (Sandbox Code Playgroud)
这段代码不仅编译,而且还创建了一个新的实例,Entity其默认值在范围内foo.我希望这不会编译或至少崩溃.
如果我把foo正确的(foo(*e)),一切工作为疑似,我看到的正确的价值观Entity中foo.
我使用Qt 4.7提供的mingw.
这是以下界面Entity:
class Entity : public QObject
{
Q_OBJECT
public:
Entity (QObject* parent = NULL);
long getId() const { return this->id; }
void setId(const long id) { this->id = id; }
QString …Run Code Online (Sandbox Code Playgroud) 这可能是不可能的,但我想知道是否有可能暂时不会超过其原始表达.我有一个指向父对象的对象链,以及一个将创建子对象的成员函数,这里有一个简化的例子
class person{
string name;
person * mommy;
public:
person(const string & nam, person * m = 0) : name(nam), mommy(m) {}
person baby(const string & nam){
return person(nam, this);
}
void talk() const{
if (mommy) mommy->talk();
cout << name << endl;
}
};
int main(){
person("Ann").baby("Susan").baby("Wendy").talk(); // fine
const person & babygirl = person("Julie").baby("Laura"); // not fine
babygirl.talk(); // segfault
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想要使用的方法person是将它传递给一个函数,如下所示:
void use(const person & p) {
p.talk();
}
use(person("Anna").baby("Lisa"));
Run Code Online (Sandbox Code Playgroud)
很好.
只要没有一个临时表现在原始表达式之后,这就可以正常工作,但是如果我把一个最后的临时工具绑定到一个const引用,那么它的父母就无法生存,我得到一个段错误.我可以隐藏person复制构造函数和赋值运算符,但有什么方法可以防止这种错误发生?如果可能的话,我想避免动态分配.
如何专门为参数提供const引用的可变参数模板函数?
例:
template<typename T, typename... Args>
T foo(Args... args) = delete;
template<> int foo(int a, const char* str, const Test& t) { .... } // Fails to compile
//template<> int foo(int a, const char* str, Test ) { .... } // Ok
int main() {
auto i = foo<int>(10, "test string!", t);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当使用声明的const Test&参数调用函数foo时,编译器无法看到已删除函数的专用函数和回退:
error: use of deleted function ‘T foo(Args ...) [with T = int; Args = {int, const char*, Test}]’
auto i = …Run Code Online (Sandbox Code Playgroud) c++ template-specialization const-reference variadic-templates
const-reference ×10
c++ ×9
reference ×3
performance ×2
qt ×2
temporaries ×2
c++11 ×1
java ×1
lvalue ×1
return ×1
rvalue ×1