473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

Sep 8 '07 #1
3 17913
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
Sep 8 '07 #2
Thanks for answering...

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

Sep 8 '07 #3
In article <11**********************@w3g2000hsg.googlegroups. com>,
po**********@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.
Sep 8 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
7
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; }...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
7
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
2
by: sparks | last post by:
I had the bright idea of using if/else to call different constructors based on a web form and what the user entered. ======================================= IF I DECLARE this here everything...
18
by: skishorev | last post by:
Hi, Here I am taking two functions. void f(int,int) and another one is float f(int,int). Is it possible to overload with return values. Thx, kishore
14
by: Jess | last post by:
Hi, I read about operator overloading and have a question regarding "operator->()". If I have two classes like this: struct A{ void f(); }; struct B{
11
by: onkar | last post by:
#include<iostream> using namespace std; class Integer{ int i; public: Integer(int ii):i(ii){} const Integer operator+(const Integer& rv){ cout<<"1-operator+"<<endl; return Integer(i+rv.i); }
1
by: cedarmillxing215 | last post by:
The oddness is on the last line of code. This is stripped down as far as I could figure, and I provide several similar examples that work as expected. I have no idea whether this is a compiler...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.