2011-01-19 61 views
1

编辑:为什么我不能在const参数函数/方法中传递const对象?

对不起,我试图理解的代码示例,它使用QList::indexOf方法,声明为here

其实我想弄清楚,为什么我需要在这种特殊情况下使用const_cast

int ProjTreeItem::row() const 
{ 
    if (parentItem) { 
      // instance of const object to test 
     const ProjTreeItem *item = new ProjTreeItem(QList<QVariant>(), NULL); 
      // Called indexOf here to test 
     parentItem->childItems.indexOf(item); 
      // This works fine 
     return parentItem->childItems.indexOf(const_cast<ProjTreeItem*>(this)); 
    } 
    return 0; 
} 

EDIT2:

我一直在寻找在错误的地方,然后我就开始怀疑这个问题与使用模板和const修饰符有关。我发现这个线程here。请看Jon的回答,我认为这个答案阐明了我达成的观点。抱歉我的问题有误导性。

+2

它应该工作正常。由于cbranch正确地指出了一个语法错误,并且你的发布代码没有错误,很明显你有一些真正的代码会给你带来问题。不要虚构虚假的代码,只要缩小你的问题并告诉我们,所以我们可以解决一个真正的问题,而不是一个假设的问题。 – GManNickG 2011-01-19 02:50:42

+3

你看起来很奇怪,你调用一个看似静态的方法,但也声明它为`const`。你没有向我们展示一些东西.... – JaredC 2011-01-19 02:56:12

回答

0

我想你指的是线

parentItem->childItems.indexOf(item); 

不能编译。 我也相信你在parentItem的类型(类)的定义写

QList< ProjTreeItem* > childItems; 


如果我理解正确的话,在该行

parentItem->childItems.indexOf(item) 

你要ProjTreeItem const*转化为ProjTreeItem*,并 这需要const_cast。 JaredC已经很好地回答了关于constness的问题。 我建议仔细阅读他的答案。

编辑:
我认为乔恩的答案适用于您的问题。
QList::indexOf的第一个参数是T const&,其中T是ProjTreeItem* 你的情况。
所以具体的参数类型是ProjTreeItem*const&
请注意,它不同于ProjTreeItem const*const&
ProjTreeItem*ProjTreeItem const*是不同的类型。
const_cast需要从后者转换为前者。
ProjTreeItem const*意味着ProjTreeItemconst
然而,ProjTreeItem*const&意味着指针constProjTreeItemconst

编辑2
您似乎误解了。

#include <typeinfo> 
#include <iostream> 
using namespace std; 

struct ProjTreeItem; 

template< class T > 
struct Test { 
    typedef T const type; 
}; 

int main() 
{ 
    cout<< boolalpha; 
    cout<< 
    (typeid(Test< ProjTreeItem* >::type) == typeid(ProjTreeItem const*)) 
    <<endl; 
    cout<< 
    (typeid(Test< ProjTreeItem* >::type) == typeid(ProjTreeItem*const)) 
    <<endl; 
} 

如果你的解释是正确的,上面的代码将打印true, 然后false
然而,代码打印false,然后true

3

我想你想这个代替:

void SomeClass::f(const MyClass*) const 
{ ... } 
+0

感谢您的评论cbranch。我编辑了我的问题。 – kaneda 2011-01-19 02:39:32

2

Hmmmm对我的作品与此代码:

class MyClass 
{ 
}; 

class SomeClass 
{ 
public: 
    void f(const MyClass *t) const 
    { 
    } 
}; 

int main() 
{ 
    SomeClass s; 

    const MyClass *myClass = new MyClass; 
    MyClass *myClass2 = new MyClass; 

    s.f(myClass); 
    s.f(myClass2); 

    return 0; 
} 
1

你做出错误的假设,即item类型等于类型this,我相信这会让你感到困惑。

const函数中,this的类型是ProjTreeItem const * const item。但是,你的指针,工程声明const ProjTreeItem * item

// `this` is a constant-pointer-to-a-constant-ProjTreeItem 
ProjTreeItem const * const this; // obviously not valid code, just illustrating type 

// `item` is simply a pointer-to-a-constant-ProjTreeItem 
const ProjTreeItem * item; 

它有助于阅读向左向右声明。

所以,声明你这样的项目指针,我怀疑你还需要一个强制转换。

const ProjTreeItem * const item = new ProjTreeItem(QList<QVariant>(), NULL); 
+0

非常感谢您的解释。我学到了更多的东西。但是,实际上,我的指针常量ProjTreeItem *项不起作用,它需要被const_casted或编译器失败。什么工作是const_casting指针或声明没有const的指针。 QList的indexOf方法接受参数类型indexOf(const T&value),T是模板类型,所以T等于ProjTreeItem *。 – kaneda 2011-01-19 03:50:45

相关问题