Is it possible to overload a member variable?

Discussion in 'C++' started by Phlip, Sep 8, 2007.

  1. Phlip

    Phlip Guest

    Goran wrote:

    > Is it possible to overload a member variable?


    Yes; they are just two different variables. The working model here is that
    each class gives its data members scope and a storage slot. Deriving a
    class opens a new scope and a new storage region. Activity in that scope
    will look-up identifiers within its scope first, so the situation is
    exactly the same as this:

    int x = 42;
    { int x = 43; }
    assert( 42 == x );

    Nothing overloaded the first x. This is among the reasons to use accessors
    (virtual int get_x();) around raw data; you can override them.

    Otherwise, such an approach is not usual because you should not confuse your
    maintainers (including yourself), and you should not duplicate your
    program's design elements. If your two class's itsValue are the same value,
    they should also be the same variable. This improves design.

    New question; what do these do?

    int x = 42;
    { int &x = x; ++x; }
    assert( ?? == x );

    { int &x(x); ++x; }
    assert( ?? == x );

    { int x = x;
    assert( ?? == x ); }

    { int x(x);
    assert( ?? == x ); }

    --
    Phlip
    http://www.oreilly.com/catalog/9780596510657/
    ^ assert_xpath
    http://tinyurl.com/23tlu5 <-- assert_raise_message
     
    Phlip, Sep 8, 2007
    #1
    1. Advertising

  2. Phlip

    Goran Guest

    Hi @ all!

    Is it possible to overload a member variable?

    Example:

    class ClassA_t {

    [...]

    private:
    int itsValue;
    }

    class ClassB_t : public ClassA_t {

    [...]

    private:
    float itsValue;
    }

    Just want to know if such approach is usual...

    Thanks

    Goran
     
    Goran, Sep 8, 2007
    #2
    1. Advertising

  3. Phlip

    Goran Guest

    Thanks for answering...

    On Sep 8, 4:11 am, Phlip <> wrote:
    > [...]
    > New question; what do these do?
    >

    :)
    > int x = 42;
    > { int &x = x; ++x; }
    > assert( ?? == x );
    >

    42+1
    > { int &x(x); ++x; }
    > assert( ?? == x );
    >

    (42+1)+1
    > { int x = x;
    > assert( ?? == x ); }
    >

    -
    > { int x(x);
    > assert( ?? == x ); }
    >

    -
    > [...]


    Goran
     
    Goran, Sep 8, 2007
    #3
  4. Phlip

    Jerry Coffin Guest

    In article <>,
    says...
    > Hi @ all!
    >
    > Is it possible to overload a member variable?


    No.

    > Example:
    >
    > class ClassA_t {
    >
    > [...]
    >
    > private:
    > int itsValue;
    > }
    >
    > class ClassB_t : public ClassA_t {
    >
    > [...]
    >
    > private:
    > float itsValue;
    > }


    This is not overloading -- it's hiding. I.e. the name in the derived
    class hides the name in the base class.

    > Just want to know if such approach is usual...


    It's perfectly legal, but fairly uncommon to do it with normal member
    variables. OTOH, the whole point of local variables is that you can have
    two (or more) with the same name without them clashing. C doesn't allow
    (for example) local functions, so it doesn't have a lot of hierarchy
    where one local variable hides the same name in a parent. In C++ the
    class hierarchy allows a great deal more of that, but it can quickly get
    confusing.

    There's no such thing as a 'virtual variable', so looking up variable
    names is always done based on static type rather than dynamic type, so
    you get much the same effect as you do with non-virtual functions. If
    you refer to an object of the derived class via a pointer to the base
    class, you'll see the variable defined in the base class instead of the
    one in the derived class. E.g.:

    #include <iostream>

    class base {
    int itsValue;
    public:
    base(int init) : itsValue(init) {}
    void increment(int amount = 1) { itsValue += amount; }
    };

    class derived : public base {
    float itsValue;
    public:
    derived(float init) : base(0), itsValue(init) {}
    operator float() { return itsValue; }
    };

    int main() {
    derived value(10.0);
    value.increment(1);
    std::cout << value << "\n";
    return 0;
    }

    This prints out 10 because increment modified itsValue from the base
    object instead of the second itsValue we added in the derived object.
    Just to answer one obvious question: no, making increment virtual
    doesn't help at all either. In fact, it has absolutely no effect on this
    problem -- it would only help if we added a version of increment in the
    derived class, AND called increment via a pointer to the base object.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 8, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov

    Re: Template member function overload

    Victor Bazarov, Jul 31, 2003, in forum: C++
    Replies:
    0
    Views:
    546
    Victor Bazarov
    Jul 31, 2003
  2. Piotre Ugrumov
    Replies:
    3
    Views:
    395
    Nick Hounsome
    Jan 25, 2004
  3. Replies:
    11
    Views:
    964
  4. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    281
    Sherm Pendley
    Oct 11, 2004
  5. W Karas
    Replies:
    3
    Views:
    279
    Victor Bazarov
    Nov 30, 2012
Loading...

Share This Page