Double getters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Krice

    #1

    Double getters

    I'm wondering is there a way to avoid having "double" getters:

    class A
    {
    Compo *_C;
    public:
    int Get_From_C();
    ....

    int A::Get_From_C() { return _C->Get_Something( ); }

    This problem gets worse if A is derived from a base
    class and only some derived classes have Compo class.
    Then you need to write a virtual getter if some routine
    requires the base handle.

  • mlimber

    #2
    Re: Double getters

    Krice wrote:[color=blue]
    > I'm wondering is there a way to avoid having "double" getters:
    >
    > class A
    > {
    > Compo *_C;[/color]

    First identifiers starting with an underscore and a capital letter are
    reserved for the implementation. Change _C to _c or c_ or something
    else.
    [color=blue]
    > public:
    > int Get_From_C();
    > ...
    >
    > int A::Get_From_C() { return _C->Get_Something( ); }
    >
    > This problem gets worse if A is derived from a base
    > class and only some derived classes have Compo class.
    > Then you need to write a virtual getter if some routine
    > requires the base handle.[/color]

    That's just the price of encapsulation (see
    http://www.parashift.com/c++-faq-lit....html#faq-7.4),
    though you might improve things by refactoring your design. Compare
    this article:



    Cheers! --M

    Comment

    Working...