Is it possible to overload a member variable?

P

Phlip

Goran said:
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 ); }
 
G

Goran

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
 
G

Goran

Thanks for answering...

[...]
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
 
J

Jerry Coffin

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top