Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Is it possible to overload a member variable? (http://www.velocityreviews.com/forums/t535758-is-it-possible-to-overload-a-member-variable.html)

Phlip 09-08-2007 02:11 AM

Re: Is it possible to overload a member variable?
 
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

Goran 09-08-2007 03:41 AM

Is it possible to overload a member variable?
 
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 09-08-2007 01:56 PM

Re: Is it possible to overload a member variable?
 
Thanks for answering...

On Sep 8, 4:11 am, Phlip <phlip2...@gmail.com> 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


Jerry Coffin 09-08-2007 04:15 PM

Re: Is it possible to overload a member variable?
 
In article <1189222883.300472.138960@w3g2000hsg.googlegroups. com>,
postmaschine@gmail.com 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.


All times are GMT. The time now is 06:45 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.