2012-05-10 67 views
0
class c { 
    private: 
     int n[10]; 
    public: 
     c(); 
     ~c(); 
     int operator()(int i) { return n[i];}; 
}; 

class cc { 
    private: 

    public: 
     c *mass; 
     cc(); 
     ~cc(); 
     c& operator*() const {return *mass;}; 
}; 
int somfunc() { 
    c *c1 = new c(); 

    cc * cc1 = new cc(); 

    (*cc1->mass)(1); 



    delete c1; 
} 

我有一个类cc指针到类c的指针。如何摆脱丑陋的记录

有什么办法来摆脱记录是这样的:

(*cc1->mass)(1); 

和somethink写这样的:

cc1->mass(1); 

是不可能?

+1

为什么摆在首位这么多球?这可能是更有建设性的固定编码风格,而不是担心为什么一个十分可怕的一段代码看起来...真的丑。 –

回答

1

当我看到标签“C++”和“运算符重载”,我的心报警接通。

C++运算符重载是复杂的,一些运营商等“()”或“ - >”变得更加困难。

我建议,运算符重载,使得无论是全局函数或方法具有相同purpouse之前,测试它的工作原理,以及后来与运营商更换。

全球友元函数例如:

class c { 
    private: 
     int n[10];  

    public: 
     c(); 
     ~c(); 

     // int operator()(int i) { return n[i]; } 

     // there is a friend global function, that when receives a "c" object, 
     // as a parameter, or declares a "c" object, as a local variable, 
     // this function, will have access to the "public" members of "c" objects, 
     // the "thisref" will be removed, when turned into a method 
     friend int c_subscript(c thisref, int i) ; 
}; 

int c_subscript(c* thisref, int i) 
{ 
    return c->n[i]; 
} 

int main() 
{ 
    c* objC() = new c(); 
    // do something with "objcC" 

    int x = c_subscript(objC, 3); 
    // do something with "x" 

    return 0; 
} // int main(...) 

局部功能( “办法”),例如:

class c { 
    private: 
     int n[10];  

    public: 
     c(); 
     ~c(); 

     // int operator()(int i) { return n[i]; } 

     int subscript(int i) ; 
}; 

int c::subscript(int i) 
{ 
    return this.n[i]; 
} 

int main() 
{ 
    c* objC() = new c(); 
    // do something with "objcC" 

    int x = c->subscript(objC, 3); 
    // do something with "x" 

    return 0; 
} // int main(...) 

而且,最后使用重载操作:

class c { 
    private: 
     int n[10];  

    public: 
     c(); 
     ~c(); 

     int subscript(int i) ; 

     int operator()(int i) { return this.subscript(i); } 
}; 

int c::subscript(int i) 
{ 
    return this.n[i]; 
} 

int main() 
{ 
    c* objC() = new c(); 
    // do something with "objcC" 

    int x = c->subscript(3); 
    // do something with "x" 

    int x = c(3); 
    // do something with "x" 

    return 0; 
} // int main(...) 

注意,在最后一个例子,我保留了一个唯一标识符的方法。

干杯。

0

您可以与

(*(*cc1))(1) 

operator()因为被施加到一个对象,而不是指针。

+0

@Nim http://ideone.com/kEhU6 dani's不能编译。 –

+0

aww垃圾 - 这是一个指针! DOH .. – Nim

+0

@Nim究竟... –

0

您可以使用

(**cc1)(1); 

或者

cc1->mass->operator()(1); 
+0

不会编译的http:// ideone。com/kEhU6 –

+0

@LuchianGrigore:固定 – Dani

1

总是可以做到这一点:

class cc { 
    private: 
     c *_mass; 

    public: 
     c& mass() const {return *_mass;}; 
}; 

现在..

cc1->mass()(1); 
1

如果mass是一个对象,不是指针,你可以使用你想要的语法:

class cc { 
    private: 

    public: 
    c mass; 
    cc(); 
    ~cc(); 
    const c& operator*() const {return mass;}; 
}; 

… 

    cc1->mass(1); 
+0

我想用这样的东西:c * mass; – meteor

+0

如果你使'mass'成为一个指针,那么在使用函数语法之前,你必须对其进行取消引用。如果你把'mass'作为一个对象,那么你可以直接使用函数语法。你必须决定你想要更多的功能。 –